Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): add config to inventory #11

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 98 additions & 3 deletions plugins/inventory/centurion.py
Original file line number Diff line number Diff line change
@@ -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 @@
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 @@
token: <token value here>
validate_certs: false

organization_groups: true

# Example Ansible Tower credential Input Configuration:

@@ -121,6 +128,22 @@ def fetch_device_config(self, url: str) -> dict:
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 @@ def parse(self, inventory, loader, path, cache=True):
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 @@ def parse(self, inventory, loader, path, cache=True):

self.inventory.add_host(host=device['name'])

config = self.fetch_device_config(device['config'])
if len(device['groups']):

for group in device['groups']:

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']}")

for key, val in config.items():
parent_group_name = to_safe_group_name(
name = str(group['parent']['name']).lower(),
replacer = '_',
force = True,
)

self.inventory.set_variable(device['name'], key, val)
self.inventory.add_child(
parent_group_name,
group_name,
)