Skip to content

Commit

Permalink
Merge pull request #23 from device42/group_name_cleansing
Browse files Browse the repository at this point in the history
D42-18290 - Ansible inventory plugin and devices naming
  • Loading branch information
cscaglioned42 authored Feb 11, 2021
2 parents efdce05 + 2e54a1c commit cb24418
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -94,6 +95,7 @@ username: admin
password: password
ssl_check: False
debug: False
clean_device_name: True
keyed_groups:
- key: d42_service_level
prefix: ''
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace: "device42"
name: "d42"
version: "1.1.2"
version: "1.2.0"
readme: "README.md"
authors:
- "Will Tome (@willtome)"
Expand Down
19 changes: 18 additions & 1 deletion plugins/inventory/d42.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'''
Expand All @@ -61,6 +67,7 @@
password: password
ssl_check: False
debug: False
clean_device_name: True
keyed_groups:
- key: d42_service_level
prefix: ''
Expand Down Expand Up @@ -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()
Expand All @@ -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])

Expand Down
9 changes: 6 additions & 3 deletions plugins/lookup/d42.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
3 changes: 3 additions & 0 deletions tests/example.d42.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
Expand Down

0 comments on commit cb24418

Please sign in to comment.