feat(plugin): Centurion ERP inventory plugin

!2 closes #3
This commit is contained in:
2024-07-31 14:51:07 +09:30
parent b8e42dab92
commit fab650bd66
10 changed files with 165 additions and 2 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
__pycache__
build
pages

13
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,13 @@
# Contirbution Guide
We welcome contributions.
## Inventory Plugin
``` bash
# to test use
ansible-inventory -i nofusscomputing.itsm.centurion --list -vvv
```

View File

@ -48,6 +48,7 @@ links:
- [Merge Requests (Pull Requests)](https://gitlab.com/nofusscomputing/projects/ansible/collections/centurion_erp_collection/-/merge_requests) - [Merge Requests (Pull Requests)](https://gitlab.com/nofusscomputing/projects/ansible/collections/centurion_erp_collection/-/merge_requests)
This Ansible collection is a companion collection for [Centurion ERP](https://nofusscomputing.com/projects/centurion_erp/)
## Contributing ## Contributing

5
ansible.cfg Normal file
View File

@ -0,0 +1,5 @@
[defaults]
collections_path= ~/git/ansible_collections
[inventory]
enable_plugins = nofusscomputing.centurion.centurion

View File

@ -11,7 +11,6 @@ about: https://gitlab.com/nofusscomputing/projects/ansible/collections/kubernete
![Project Status - Active](https://img.shields.io/badge/Project%20Status-Active-green?logo=gitlab&style=plastic) ![Project Status - Active](https://img.shields.io/badge/Project%20Status-Active-green?logo=gitlab&style=plastic)
![Gitlab build status - stable](https://img.shields.io/badge/dynamic/json?color=ff782e&label=Build&query=0.status&url=https%3A%2F%2Fgitlab.com%2Fapi%2Fv4%2Fprojects%2F59504579%2Fpipelines%3Fref%3Dmaster&logo=gitlab&style=plastic) ![Gitlab build status - development](https://img.shields.io/badge/dynamic/json?color=ff782e&label=Build&query=0.status&url=https%3A%2F%2Fgitlab.com%2Fapi%2Fv4%2Fprojects%2F59504579%2Fpipelines%3Fref%3Ddevelopment&logo=gitlab&style=plastic) ![Gitlab build status - stable](https://img.shields.io/badge/dynamic/json?color=ff782e&label=Build&query=0.status&url=https%3A%2F%2Fgitlab.com%2Fapi%2Fv4%2Fprojects%2F59504579%2Fpipelines%3Fref%3Dmaster&logo=gitlab&style=plastic) ![Gitlab build status - development](https://img.shields.io/badge/dynamic/json?color=ff782e&label=Build&query=0.status&url=https%3A%2F%2Fgitlab.com%2Fapi%2Fv4%2Fprojects%2F59504579%2Fpipelines%3Fref%3Ddevelopment&logo=gitlab&style=plastic)
@ -21,4 +20,9 @@ about: https://gitlab.com/nofusscomputing/projects/ansible/collections/kubernete
</span> </span>
This Ansible Collection is intended to compliement Centurion ERP. This Ansible Collection is intended to compliement [Centurion ERP](../../../centurion_erp/index.md).
## Features
- [Inventory plugin](./plugins/inventory.md)

View File

@ -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)

View File

@ -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.

View File

View File

@ -29,6 +29,12 @@ nav:
- projects/ansible/collection/centurion/index.md - projects/ansible/collection/centurion/index.md
- Plugins:
- projects/ansible/collection/centurion/plugins/index.md
- projects/ansible/collection/centurion/plugins/inventory.md
- Operations: - Operations:

View File

@ -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)