Merge branch '4-inventory-groups' into 'development'
feat(plugin): add config to inventory Closes #5 See merge request nofusscomputing/projects/ansible/collections/centurion_erp_collection!7
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import json
|
||||
|
||||
from ansible.inventory.group import to_safe_group_name
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
from ansible.module_utils.urls import open_url
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin
|
||||
@ -22,6 +23,11 @@ options:
|
||||
required: true
|
||||
env:
|
||||
- name: CENTURION_API
|
||||
organization_groups:
|
||||
description:
|
||||
- Create groups from organization names. Uses format C(organization_<organization.name>).
|
||||
default: true
|
||||
type: boolean
|
||||
token:
|
||||
required: false
|
||||
description:
|
||||
@ -50,6 +56,7 @@ api_endpoint: http://localhost:8000
|
||||
token: <token value here>
|
||||
validate_certs: false
|
||||
|
||||
organization_groups: true
|
||||
|
||||
# Example Ansible Tower credential Input Configuration:
|
||||
|
||||
@ -121,6 +128,22 @@ class InventoryModule(BaseInventoryPlugin):
|
||||
return config
|
||||
|
||||
|
||||
def fetch_groups(self) -> dict:
|
||||
|
||||
self.display.v("Fetching groups")
|
||||
|
||||
response = open_url(
|
||||
url = f'{self.api_endpoint}/api/configuration/',
|
||||
headers = self.headers,
|
||||
validate_certs = self.validate_certs,
|
||||
)
|
||||
|
||||
configuration = json.loads(to_text(response.read()))['results']
|
||||
|
||||
|
||||
return configuration
|
||||
|
||||
|
||||
def parse(self, inventory, loader, path, cache=True):
|
||||
super().parse(inventory, loader, path)
|
||||
|
||||
@ -130,6 +153,7 @@ class InventoryModule(BaseInventoryPlugin):
|
||||
self.api_endpoint = self.get_option("api_endpoint").strip("/")
|
||||
self.token = self.get_option("token")
|
||||
self.validate_certs = self.get_option("validate_certs")
|
||||
self.organization_groups = self.get_option("organization_groups")
|
||||
|
||||
self.headers = {
|
||||
'Authorization': f'Token {self.token}'
|
||||
@ -147,8 +171,79 @@ class InventoryModule(BaseInventoryPlugin):
|
||||
|
||||
self.inventory.add_host(host=device['name'])
|
||||
|
||||
config = self.fetch_device_config(device['config'])
|
||||
if len(device['groups']):
|
||||
|
||||
for key, val in config.items():
|
||||
for group in device['groups']:
|
||||
|
||||
self.inventory.set_variable(device['name'], key, val)
|
||||
group_name = to_safe_group_name(
|
||||
name = str(group['name']).lower(),
|
||||
replacer = '_',
|
||||
force = True,
|
||||
)
|
||||
|
||||
self.inventory.add_group(
|
||||
group = group_name
|
||||
)
|
||||
|
||||
self.inventory.add_host(
|
||||
host = device['name'],
|
||||
group = group_name,
|
||||
)
|
||||
|
||||
if self.organization_groups:
|
||||
|
||||
organization_group_name = to_safe_group_name(
|
||||
name = 'organization_' + str(device['organization']['name']).lower(),
|
||||
replacer = '_',
|
||||
force = True,
|
||||
)
|
||||
|
||||
self.inventory.add_group(
|
||||
group = organization_group_name
|
||||
)
|
||||
|
||||
self.inventory.add_host(
|
||||
host = device['name'],
|
||||
group = organization_group_name,
|
||||
)
|
||||
|
||||
# see #5
|
||||
# config = self.fetch_device_config(device['config'])
|
||||
|
||||
# for key, val in config.items():
|
||||
|
||||
# self.inventory.set_variable(device['name'], key, val)
|
||||
|
||||
|
||||
groups = self.fetch_groups()
|
||||
|
||||
self.display.v(f"Parsing returned groups")
|
||||
|
||||
for group in groups:
|
||||
|
||||
self.display.vv(f"Adding group {group['name']} to inventory")
|
||||
|
||||
group_name = to_safe_group_name(
|
||||
name = str(group['name']).lower(),
|
||||
replacer = '_',
|
||||
force = True,
|
||||
)
|
||||
|
||||
self.inventory.add_group(
|
||||
group = group_name
|
||||
)
|
||||
|
||||
if group['parent']:
|
||||
|
||||
self.display.vv(f"Adding group {group['name']} to parent group {group['parent']['name']}")
|
||||
|
||||
parent_group_name = to_safe_group_name(
|
||||
name = str(group['parent']['name']).lower(),
|
||||
replacer = '_',
|
||||
force = True,
|
||||
)
|
||||
|
||||
self.inventory.add_child(
|
||||
parent_group_name,
|
||||
group_name,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user