-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin): Centurion ERP inventory plugin
!2 closes #3
- Loading branch information
Showing
10 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__pycache__ | ||
build | ||
pages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Contirbution Guide | ||
|
||
We welcome contributions. | ||
|
||
|
||
## Inventory Plugin | ||
|
||
``` bash | ||
|
||
# to test use | ||
ansible-inventory -i nofusscomputing.itsm.centurion --list -vvv | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[defaults] | ||
collections_path= ~/git/ansible_collections | ||
|
||
[inventory] | ||
enable_plugins = nofusscomputing.centurion.centurion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
docs/projects/ansible/collection/centurion/plugins/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
title: Plugins | ||
description: No Fuss Computings Companion Ansible Collection Centurion Plugins. | ||
date: 2024-07-31 | ||
template: project.html | ||
about: https://gitlab.com/nofusscomputing/projects/ansible/collections/kubernetes | ||
--- | ||
|
||
Available plugins include: | ||
|
||
- [Inventory](./inventory.md) |
9 changes: 9 additions & 0 deletions
9
docs/projects/ansible/collection/centurion/plugins/inventory.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
title: Inventory | ||
description: No Fuss Computings Companion Ansible Collection Centurion Inventory Plugin. | ||
date: 2024-07-31 | ||
template: project.html | ||
about: https://gitlab.com/nofusscomputing/projects/ansible/collections/kubernetes | ||
--- | ||
|
||
This inventory plugin obtains the data required from [Centurion ERP](../../../../centurion_erp/index.md) to build up an ansible inventory for all devices. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import json | ||
|
||
from ansible.module_utils.common.text.converters import to_text | ||
from ansible.module_utils.urls import open_url | ||
from ansible.plugins.inventory import BaseInventoryPlugin | ||
|
||
|
||
DOCUMENTATION = ''' | ||
--- | ||
module: centurion | ||
plugin_type: inventory | ||
short_description: Centurion ERP Inventory | ||
description: | ||
- "An Inventory Plugin to fetch inventory from Centurion ERP." | ||
options: | ||
plugin: | ||
description: token that ensures this is a source file for the 'centurion' plugin. | ||
required: True | ||
choices: ['nofusscomputing.centurion.centurion'] | ||
api_endpoint: | ||
description: Endpoint of the Centurion API | ||
required: true | ||
env: | ||
- name: CENTURION_API | ||
token: | ||
required: false | ||
description: | ||
- NetBox API token to be able to read against NetBox. | ||
env: | ||
- name: CENTURION_TOKEN | ||
validate_certs: | ||
description: | ||
- Allows skipping of validation of SSL certificates. Set to C(false) to disable certificate validation. | ||
default: true | ||
type: boolean | ||
author: | ||
- jon @jon_nfc | ||
''' | ||
|
||
|
||
|
||
class InventoryModule(BaseInventoryPlugin): | ||
|
||
NAME = 'nofusscomputing.centurion.centurion' | ||
|
||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
self.headers = { | ||
'Authorization': 'Token 7d501c956363e60df0cfd770db36f54226f079d707346f776ab31d5f40d16542' | ||
} | ||
|
||
|
||
def verify_file(self, path): | ||
|
||
return True | ||
|
||
|
||
def fetch_devices(self) -> dict: | ||
|
||
self.display.v("Fetching Devices ") | ||
|
||
response = open_url( | ||
url = 'https://test.nofusscomputing.com/api/device/', | ||
headers = self.headers, | ||
validate_certs=False, | ||
) | ||
|
||
devices = json.loads(to_text(response.read()))['results'] | ||
|
||
|
||
return devices | ||
|
||
|
||
def fetch_device_config(self, url: str) -> dict: | ||
|
||
|
||
self.display.vv(f"Fetching Device Config {url} ") | ||
|
||
response = open_url( | ||
url = url, | ||
headers = self.headers, | ||
validate_certs=False, | ||
) | ||
|
||
config = json.loads(to_text(response.read())) | ||
|
||
return config | ||
|
||
|
||
def parse(self, inventory, loader, path, cache=True): | ||
super().parse(inventory, loader, path) | ||
|
||
devices = self.fetch_devices() | ||
|
||
self.display.v(f"Parsing returned devices") | ||
|
||
for device in devices: | ||
|
||
|
||
self.display.vv(f"Adding device {device['name']} to inventory") | ||
|
||
|
||
self.inventory.add_host(host=device['name']) | ||
|
||
config = self.fetch_device_config(device['config']) | ||
|
||
for key, val in config.items(): | ||
|
||
self.inventory.set_variable(device['name'], key, val) |