diff --git a/README.md b/README.md index de72a2a..d7cf212 100644 --- a/README.md +++ b/README.md @@ -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 done executing. This is different from the core Ansible behaviour, which does 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 -``` diff --git a/ansible.cfg b/ansible.cfg deleted file mode 100644 index e0e40f8..0000000 --- a/ansible.cfg +++ /dev/null @@ -1,3 +0,0 @@ -[defaults] -inventory = example/ -jinja2_extensions = jinja2.ext.do diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..5dcd75c --- /dev/null +++ b/example/.gitignore @@ -0,0 +1 @@ +collections/ansible_collections diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..d14d217 --- /dev/null +++ b/example/README.md @@ -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 +``` diff --git a/example/ansible.cfg b/example/ansible.cfg new file mode 100644 index 0000000..5e10708 --- /dev/null +++ b/example/ansible.cfg @@ -0,0 +1,4 @@ +[defaults] +inventory = inventory +collections_path = collections +jinja2_extensions = jinja2.ext.do diff --git a/example/collections/.empty b/example/collections/.empty new file mode 100644 index 0000000..e69de29 diff --git a/example/00-data.yml b/example/inventory/00-data.yml similarity index 100% rename from example/00-data.yml rename to example/inventory/00-data.yml diff --git a/example/01-test-reconstructed.yml b/example/inventory/01-test-reconstructed.yml similarity index 98% rename from example/01-test-reconstructed.yml rename to example/inventory/01-test-reconstructed.yml index c697f6c..3a465f5 100644 --- a/example/01-test-reconstructed.yml +++ b/example/inventory/01-test-reconstructed.yml @@ -1,5 +1,5 @@ --- -plugin: reconstructed +plugin: tseeker.reconstructed.reconstructed instructions: # Check whether that host is managed diff --git a/example/requirements.yml b/example/requirements.yml new file mode 100644 index 0000000..bafed81 --- /dev/null +++ b/example/requirements.yml @@ -0,0 +1,3 @@ +collections: + - name: tseeker.reconstructed + version: 1.0.0 diff --git a/galaxy.yml b/galaxy.yml new file mode 100644 index 0000000..db86ddc --- /dev/null +++ b/galaxy.yml @@ -0,0 +1,18 @@ +namespace: tseeker +name: reconstructed +version: 1.0.0 +readme: README.md +authors: + - Emmanuel BENOÎT +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 diff --git a/inventory_plugins/reconstructed.py b/plugins/inventory/reconstructed.py similarity index 99% rename from inventory_plugins/reconstructed.py rename to plugins/inventory/reconstructed.py index 30bcfa0..e7d16fe 100644 --- a/inventory_plugins/reconstructed.py +++ b/plugins/inventory/reconstructed.py @@ -23,7 +23,7 @@ DOCUMENTATION = """ - Token that ensures this is a source file for the C(group_creator) plugin. required: True - choices: ['reconstructed'] + choices: ['tseeker.reconstructed.reconstructed','reconstructed'] instructions: description: - 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): """Constructs groups based on lists of instructions.""" - NAME = "reconstructed" + NAME = "tseeker.reconstructed.reconstructed" def verify_file(self, path): return super().verify_file(path) and path.endswith((".yaml", ".yml")) diff --git a/tests/__init__.py b/tests/__init__.py index c37fb90..f031a47 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -4,7 +4,7 @@ import sys sys.path.insert( 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