016 Multiple inventory constructed
Extending example 015 Multiple inventory cache.
Use case
Create inventory groups using the inventory plugin ansible.builtin.constructed after the two inventory plugin vbotka.freebsd.iocage configuration files.
Tree
shell> tree .
.
├── ansible.cfg
├── hosts
│ ├── 02_iocage.yml
│ ├── 04_iocage.yml
│ └── 99_constructed.yml
└── pb-test.yml
Synopsis
The inventory plugin vbotka.freebsd.iocage gets the jails (managed nodes):
test_111:113from the hostiocage_02test_131:133from the hostiocage_04
and creates the inventory groups
test_02andtest_04.The inventory plugin ansible.builtin.constructed creates the inventory groups:
testcomprising hosts starting'test'test_upcomprising running hosts starting'test'.
Requirements
Notes
The inventory files in the directory
hostsare evaluated in the alphabetical order.The inventory plugin vbotka.freebsd.iocage doesn’t provide the option use_vars_plugins.
Jails at iocage_02
[iocage_02]# iocage list -l
+------+----------------+------+-------+------+-----------------+-------------------+-----+----------------+----------+
| JID | NAME | BOOT | STATE | TYPE | RELEASE | IP4 | IP6 | TEMPLATE | BASEJAIL |
+======+================+======+=======+======+=================+===================+=====+================+==========+
| None | ansible_client | off | down | jail | 14.3-RELEASE-p8 | em0|10.1.0.199/24 | - | - | yes |
+------+----------------+------+-------+------+-----------------+-------------------+-----+----------------+----------+
| None | test_111 | off | down | jail | 14.3-RELEASE-p8 | em0|10.1.0.111/24 | - | ansible_client | yes |
+------+----------------+------+-------+------+-----------------+-------------------+-----+----------------+----------+
| None | test_112 | off | down | jail | 14.3-RELEASE-p8 | em0|10.1.0.112/24 | - | ansible_client | yes |
+------+----------------+------+-------+------+-----------------+-------------------+-----+----------------+----------+
| None | test_113 | off | down | jail | 14.3-RELEASE-p8 | em0|10.1.0.113/24 | - | ansible_client | yes |
+------+----------------+------+-------+------+-----------------+-------------------+-----+----------------+----------+
Jails at iocage_04
[iocage_04]# iocage list -l
+------+----------------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
| JID | NAME | BOOT | STATE | TYPE | RELEASE | IP4 | IP6 | TEMPLATE | BASEJAIL |
+======+================+======+=======+======+=================+=====================+=====+================+==========+
| None | ansible_client | off | down | jail | 15.0-RELEASE-p3 | vnet0|10.1.0.198/24 | - | - | yes |
+------+----------------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
| None | test_131 | off | down | jail | 15.0-RELEASE-p3 | vnet0|10.1.0.131/24 | - | ansible_client | yes |
+------+----------------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
| None | test_132 | off | down | jail | 15.0-RELEASE-p3 | vnet0|10.1.0.132/24 | - | ansible_client | yes |
+------+----------------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
| None | test_133 | off | down | jail | 15.0-RELEASE-p3 | vnet0|10.1.0.133/24 | - | ansible_client | yes |
+------+----------------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
ansible.cfg
[defaults]
gathering = explicit
callback_result_format = yaml
display_skipped_hosts = false
host_key_checking = false
[connection]
pipelining = true
Inventory hosts
hosts/02_iocage.yml
plugin: vbotka.freebsd.iocage
host: 10.1.0.73
user: admin
sudo: true
sudo_preserve_env: true
env:
CRYPTOGRAPHY_OPENSSL_NO_LEGACY: 1
compose:
ansible_host: iocage_ip4
groups:
test_02: inventory_hostname.startswith('test')
hosts/04_iocage.yml
plugin: vbotka.freebsd.iocage
host: 10.1.0.29
user: admin
sudo: true
compose:
ansible_host: iocage_ip4
groups:
test_04: inventory_hostname.startswith('test')
hosts/99_constructed.yml
plugin: ansible.builtin.constructed
groups:
test: inventory_hostname.startswith('test')
test_up: inventory_hostname.startswith('test') and iocage_state == 'up'
Playbook pb-test.yml
- hosts: test
tasks:
- debug:
var: iocage_ip4
- debug:
msg: |
{% for group in groups %}
{{ group }}: {{ groups[group] }}
{% endfor %}
run_once: true
Playbook output - Display groups
(env) > ansible-playbook pb-test.yml -i hosts
PLAY [test] ********************************************************************
TASK [debug] *******************************************************************
ok: [test_111] =>
iocage_ip4: 10.1.0.111
ok: [test_112] =>
iocage_ip4: 10.1.0.112
ok: [test_113] =>
iocage_ip4: 10.1.0.113
ok: [test_131] =>
iocage_ip4: 10.1.0.131
ok: [test_132] =>
iocage_ip4: 10.1.0.132
ok: [test_133] =>
iocage_ip4: 10.1.0.133
TASK [debug] *******************************************************************
ok: [test_111] =>
msg: |-
all: ['ansible_client', 'test_111', 'test_112', 'test_113', 'test_131', 'test_132', 'test_133']
ungrouped: ['ansible_client']
test_02: ['test_111', 'test_112', 'test_113']
test_04: ['test_131', 'test_132', 'test_133']
test: ['test_111', 'test_112', 'test_113', 'test_131', 'test_132', 'test_133']
PLAY RECAP *********************************************************************
test_111 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_112 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_113 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_131 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_132 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_133 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0