diff --git a/README.md b/README.md index 5ac6684..a6532dc 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ D42_USER = 'device42 user' D42_PWD = 'device42 password' D42_URL = 'https:// device42 server IP address' D42_SKIP_SSL_CHECK = False +D42_CLEAN_DEVICE_NAME = True ``` For more information on installing collections please follow documentation here https://docs.ansible.com/ansible/latest/user_guide/collections_using.html @@ -94,6 +95,7 @@ username: admin password: password ssl_check: False debug: False +clean_device_name: True keyed_groups: - key: d42_service_level prefix: '' @@ -130,6 +132,7 @@ D42_PWD = 'device42 password' D42_URL = 'https:// device42 server IP address' D42_SKIP_SSL_CHECK = False D42_DEBUG = False +D42_CLEAN_DEVICE_NAME = True ``` ### How to run diff --git a/galaxy.yml b/galaxy.yml index e26d537..216b8b0 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,6 +1,6 @@ namespace: "device42" name: "d42" -version: "1.1.2" +version: "1.2.0" readme: "README.md" authors: - "Will Tome (@willtome)" diff --git a/plugins/inventory/d42.py b/plugins/inventory/d42.py index 1b2d24c..c64e11f 100644 --- a/plugins/inventory/d42.py +++ b/plugins/inventory/d42.py @@ -52,6 +52,12 @@ default: false env: - name: D42_DEBUG + clean_device_name: + description: group name cleaning option. + type: boolean + default: true + env: + - name: D42_CLEAN_DEVICE_NAME ''' EXAMPLES = r''' @@ -61,6 +67,7 @@ password: password ssl_check: False debug: False +clean_device_name: True keyed_groups: - key: d42_service_level prefix: '' @@ -90,6 +97,12 @@ def parse(self, inventory, loader, path, cache=False): strict = self.get_option('strict') try: + try: + clean_device_name = self.get_option('clean_device_name') + except Exception: + print("clean_device_name has not been defined in *.d42.yml. defaulting to True") + clean_device_name = True + objects = [] json_response = self.get_d42_inventory() @@ -98,7 +111,11 @@ def parse(self, inventory, loader, path, cache=False): objects = json_response['Devices'] for object_ in objects: - host_name = self.inventory.add_host(to_safe_group_name(object_['name'])) + if clean_device_name: + host_name = self.inventory.add_host(to_safe_group_name(object_['name'])) + else: + host_name = self.inventory.add_host(object_['name']) + for k in object_.keys(): self.inventory.set_variable(host_name, 'd42_' + k, object_[k]) diff --git a/plugins/lookup/d42.py b/plugins/lookup/d42.py index f3e1e4e..2dbcd1a 100644 --- a/plugins/lookup/d42.py +++ b/plugins/lookup/d42.py @@ -47,9 +47,12 @@ def get_list_from_csv(text): def run(self, terms, variables=None, **kwargs): conf = { - 'D42_URL': os.environ['D42_URL'], - 'D42_USER': os.environ['D42_USER'], - 'D42_PWD': os.environ['D42_PWD'] + 'D42_URL': os.environ.get('D42_URL'), + 'D42_USER': os.environ.get('D42_USER'), + 'D42_PWD': os.environ.get('D42_PWD'), + 'D42_SSL_CHECK': os.environ.get('D42_SSL_CHECK', True), + 'D42_DEBUG': os.environ.get('D42_DEBUG', False), + 'D42_CLEAN_DEVICE_NAME': os.environ.get('D42_CLEAN_DEVICE_NAME', True) } if terms[1] == "password": diff --git a/tests/example.d42.yml b/tests/example.d42.yml index 1fbeac6..fbd6f1a 100644 --- a/tests/example.d42.yml +++ b/tests/example.d42.yml @@ -3,6 +3,9 @@ plugin: device42.d42.d42 url: 'https://d42-4165.device42.net' username: admin password: password +ssl_check: False +debug: False +clean_device_name: True keyed_groups: - key: d42_service_level prefix: ''