Skip to content

Commit

Permalink
Added node definition lookup plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonking3 committed May 1, 2024
1 parent 716c622 commit ea31e9e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
62 changes: 62 additions & 0 deletions plugins/lookup/cml_node_definition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

DOCUMENTATION = r"""
name: cml_node_definition
author: Jason King <[email protected]>
version_added: "1.2.2"
short_description: Get CML node definitions
description:
- This lookup returns a list of dictionaries containing the node definitions in a CML instance.
options:
cml_host:
description: The hostname or IP address of the CML instance
required: True
cml_username:
description: The username to authenticate to the CML instance
required: True
cml_password:
description: The password to authenticate to the CML instance
required: True
validate_certs:
description: Whether to validate SSL certificates
required: False
default: False
id:
description: The ID of the node definition to return (optional)
required: False
"""
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.plugins.lookup import LookupBase
from ansible.utils.display import Display
from ansible_collections.cisco.cml.plugins.module_utils.cml_utils import cmlPlugin

display = Display()

class LookupModule(LookupBase):

def run(self, terms, variables=None, **kwargs):
self.set_options(var_options=variables, direct=kwargs)

ret = []

host = self.get_option('cml_host')
username = self.get_option('cml_username')
password = self.get_option('cml_password')
validate_certs = self.get_option('validate_certs')

cml = cmlPlugin(host, username, password, validate_certs)

try:
nodedefs = cml.client.definitions.node_definitions()
except Exception as e:
raise AnsibleError("Failed to get node definitions: %s" % e)

if self.get_option('id') is not None:
for nodedef in nodedefs:
if nodedef['id'] == self.get_option('id'):
ret = [nodedef]
else:
ret = nodedefs

return ret
18 changes: 18 additions & 0 deletions plugins/module_utils/cml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,21 @@ def fail_json(self, msg, **kwargs):

self.result.update(**kwargs)
self.module.fail_json(msg=msg, **self.result)

class cmlPlugin(object):

def __init__(self, host, user, password, validate_certs=False):
self.host = host
self.user = user
self.password = password
self.validate_certs = validate_certs

self.client = None

if not HAS_VIRL2CLIENT:
raise AnsibleError("Missing_required_lib('virl2_client')")

self.login()

def login(self):
self.client = ClientLibrary('https://{0}'.format(self.host), self.user, self.password, ssl_verify=self.validate_certs)

0 comments on commit ea31e9e

Please sign in to comment.