Restructured into a collection

This commit is contained in:
Emmanuel BENOîT 2022-10-07 22:29:53 +02:00
parent ae58080401
commit 3f38940a9c
No known key found for this signature in database
GPG key ID: 2356DC6956CF54EF
12 changed files with 128 additions and 78 deletions

View file

@ -59,74 +59,3 @@ is evaluated, but before the condition is. If these variables already existed,
their state will be saved and they will be restored after the instruction is their state will be saved and they will be restored after the instruction is
done executing. This is different from the core Ansible behaviour, which does done executing. This is different from the core Ansible behaviour, which does
not evaluate the `vars` unless they are used. not evaluate the `vars` unless they are used.
A somewhat silly example can be found in the `example` directory. Trying to
execute it using `ansible-inventory --graph` results in the following output.
```
[WARNING]: reconstructed - error on host evil-vm: evil-vm is obviously evil,
skipping.
@all:
|--@managed:
| |--@by_environment:
| | |--@env_dev:
| | | |--vm00
| | | |--vm01
| | | |--vm02
| | | |--vm03
| | | |--vm04
| | | |--vm09
| | |--@env_prod:
| | | |--vm05
| | | |--vm06
| | | |--vm07
| | | |--vm08
| |--@by_failover_stack:
| | |--@fostack_1:
| | | |--vm00
| | | |--vm02
| | | |--vm05
| | | |--vm07
| | |--@fostack_2:
| | | |--vm01
| | | |--vm03
| | | |--vm06
| | | |--vm08
| | |--@no_failover:
| | | |--vm04
| | | |--vm09
| |--@by_network:
| | |--@net_dev:
| | | |--vm00
| | | |--vm01
| | | |--vm02
| | | |--vm03
| | | |--vm04
| | |--@net_infra:
| | | |--vm05
| | | |--vm06
| | | |--vm07
| | | |--vm08
| | | |--vm09
| |--@by_service:
| | |--@svc_ldap:
| | | |--@svcm_ldap_back:
| | | | |--@svcm_ldap_ro:
| | | | | |--vm02
| | | | | |--vm03
| | | | | |--vm07
| | | | | |--vm08
| | | | |--@svcm_ldap_rw:
| | | | | |--vm04
| | | | | |--vm09
| | | |--@svcm_ldap_front:
| | | | |--vm00
| | | | |--vm01
| | | | |--vm05
| | | | |--vm06
|--@reedmably_evil:
| |--evil-but-nicer-vm
|--@ungrouped:
| |--evil-vm
| |--localhost
```

View file

@ -1,3 +0,0 @@
[defaults]
inventory = example/
jinja2_extensions = jinja2.ext.do

1
example/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
collections/ansible_collections

98
example/README.md Normal file
View file

@ -0,0 +1,98 @@
reconstructed example
=====================
The example in this directory generates a graph of hosts, organised based on
the contents of a variable called ``inv__data`` associated to each host
statically in the ``inventory/00-data.yml`` file. The main ``reconstructed``
script, located in ``inventory/01-test-reconstructed.yml``, is executed once
for each host. It creates the various groups at the appropriate locations,
defines additional facts, and adds hosts to the groups.
The example can be executed using :
```
ansible-galaxy collection install .. -p ./collections
ansible-inventory --playbook-dir . --graph
```
When executed, ``localhost`` will be skipped as it doesn't have the
``inv__data`` fact.
Two hosts in there, ``evil-vm`` and ``evil-but-nicer-vm``, are meant to
illustrate the ``fail`` action as well as error recovery.
The rest of the hosts correspond to two instances of a LDAP cluster, in which
two hosts are used as the frontend, two hosts as the read-only replicas, and
the last one as the master. Groups are generated based on the environment,
network, and failover stack, as well as service, components of the service,
and instances of a service.
Below is the output that should be obtained.
```
[WARNING]: reconstructed - error on host evil-vm: evil-vm is obviously evil,
skipping.
@all:
|--@managed:
| |--@by_environment:
| | |--@env_dev:
| | | |--vm00
| | | |--vm01
| | | |--vm02
| | | |--vm03
| | | |--vm04
| | | |--vm09
| | |--@env_prod:
| | | |--vm05
| | | |--vm06
| | | |--vm07
| | | |--vm08
| |--@by_failover_stack:
| | |--@fostack_1:
| | | |--vm00
| | | |--vm02
| | | |--vm05
| | | |--vm07
| | |--@fostack_2:
| | | |--vm01
| | | |--vm03
| | | |--vm06
| | | |--vm08
| | |--@no_failover:
| | | |--vm04
| | | |--vm09
| |--@by_network:
| | |--@net_dev:
| | | |--vm00
| | | |--vm01
| | | |--vm02
| | | |--vm03
| | | |--vm04
| | |--@net_infra:
| | | |--vm05
| | | |--vm06
| | | |--vm07
| | | |--vm08
| | | |--vm09
| |--@by_service:
| | |--@svc_ldap:
| | | |--@svcm_ldap_back:
| | | | |--@svcm_ldap_ro:
| | | | | |--vm02
| | | | | |--vm03
| | | | | |--vm07
| | | | | |--vm08
| | | | |--@svcm_ldap_rw:
| | | | | |--vm04
| | | | | |--vm09
| | | |--@svcm_ldap_front:
| | | | |--vm00
| | | | |--vm01
| | | | |--vm05
| | | | |--vm06
|--@reedmably_evil:
| |--evil-but-nicer-vm
|--@ungrouped:
| |--evil-vm
| |--localhost
```

4
example/ansible.cfg Normal file
View file

@ -0,0 +1,4 @@
[defaults]
inventory = inventory
collections_path = collections
jinja2_extensions = jinja2.ext.do

View file

View file

@ -1,5 +1,5 @@
--- ---
plugin: reconstructed plugin: tseeker.reconstructed.reconstructed
instructions: instructions:
# Check whether that host is managed # Check whether that host is managed

3
example/requirements.yml Normal file
View file

@ -0,0 +1,3 @@
collections:
- name: tseeker.reconstructed
version: 1.0.0

18
galaxy.yml Normal file
View file

@ -0,0 +1,18 @@
namespace: tseeker
name: reconstructed
version: 1.0.0
readme: README.md
authors:
- Emmanuel BENOÎT <tseeker@nocternity.net>
description: An inventory plug-in that can be used to generate groups programatically.
license:
- GPL-3.0-or-later
tags: [ inventory, group, plugin ]
repository: https://github.com/tseeker/reconstructed
issues: https://github.com/tseeker/reconstructed/issues
build_ignore:
- example/collections
- .pytest_cache
- .pre-commit-config.yaml
- .yamllint
- requirements-dev.txt

View file

@ -23,7 +23,7 @@ DOCUMENTATION = """
- Token that ensures this is a source file for the C(group_creator) - Token that ensures this is a source file for the C(group_creator)
plugin. plugin.
required: True required: True
choices: ['reconstructed'] choices: ['tseeker.reconstructed.reconstructed','reconstructed']
instructions: instructions:
description: description:
- The list of instructions to be executed in order to generate the - The list of instructions to be executed in order to generate the
@ -1042,7 +1042,7 @@ def parse_instruction(inventory, templar, display, record):
class InventoryModule(BaseInventoryPlugin): class InventoryModule(BaseInventoryPlugin):
"""Constructs groups based on lists of instructions.""" """Constructs groups based on lists of instructions."""
NAME = "reconstructed" NAME = "tseeker.reconstructed.reconstructed"
def verify_file(self, path): def verify_file(self, path):
return super().verify_file(path) and path.endswith((".yaml", ".yml")) return super().verify_file(path) and path.endswith((".yaml", ".yml"))

View file

@ -4,7 +4,7 @@ import sys
sys.path.insert( sys.path.insert(
0, 0,
os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "inventory_plugins")), os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "plugins/inventory")),
) )
import reconstructed import reconstructed