019 Inventory option use_vars_plugins
Extending example 016 Multiple inventory constructed.
Use case
The option use_vars_plugins, responsible for reading host_vars and group_vars
directories, is not available in the inventory plugin vbotka.freebsd.iocage because the
constructed fragment doesn’t provide it.
Use the inventory plugin ansible.builtin.constructed to read
group_vars.Use the variable
regionto create the groupsregion_EUandregion_US.
Tree
shell> tree .
.
├── ansible.cfg
├── hosts
│ ├── 02_iocage.yml
│ ├── 04_iocage.yml
│ ├── 99_constructed.yml
│ └── group_vars
│ ├── test_02
│ │ └── region.yml
│ └── test_04
│ └── region.yml
├── pb-test-all.yml
└── pb-test-EU.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 inventory groups
test_02andtest_04The inventory plugin ansible.builtin.constructed creates the inventory groups:
testcomprising hosts starting'test'test_upcomprising running hosts starting'test'region_EUcomprising hosts where the variableregion=EUregion_UScomprising hosts where the variableregion=US
Notes
The inventory files in the directory
hostsare evaluated in alphabetical order.
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 | - | - | - | yes |
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
| 67 | test_111 | off | up | jail | 14.3-RELEASE-p8 | epair0b|10.1.0.174 | - | ansible_client | yes |
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
| None | test_112 | off | down | jail | 14.3-RELEASE-p8 | DHCP (not running) | - | ansible_client | yes |
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
| None | test_113 | off | down | jail | 14.3-RELEASE-p8 | DHCP (not running) | - | 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 | - | - | - | yes |
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
| 46 | test_131 | off | up | jail | 15.0-RELEASE-p3 | epair0b|10.1.0.224 | - | ansible_client | yes |
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
| 47 | test_132 | off | up | jail | 15.0-RELEASE-p3 | epair0b|10.1.0.185 | - | ansible_client | yes |
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
| 48 | test_133 | off | up | jail | 15.0-RELEASE-p3 | epair0b|10.1.0.225 | - | 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
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')
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')
plugin: ansible.builtin.constructed
use_vars_plugins: true
groups:
test: inventory_hostname.startswith('test')
test_up: inventory_hostname.startswith('test') and iocage_state == 'up'
keyed_groups:
- prefix: region
key: region
group_vars
region: EU
region: US
Hint
Run the below command to see the complete inventory
shell> ansible-inventory -i hosts --list --yaml
Warning
In the inventory plugin ansible.builtin.constructed:
The option use_vars_plugins reads the inventory
group_varsandhost_varsThe playbook
group_varsandhost_varswill be silently ignored.
Playbook pb-test-all.yml
- hosts: all
tasks:
- debug:
var: iocage_ip4
- debug:
msg: |
{% for group in groups %}
{{ group }}: {{ groups[group] }}
{% endfor %}
run_once: true
Playbook output - List groups
(env) > ansible-playbook pb-test-all.yml -i hosts
PLAY [all] *********************************************************************
TASK [debug] *******************************************************************
ok: [ansible_client] =>
iocage_ip4: '-'
ok: [test_111] =>
iocage_ip4: 10.1.0.174
ok: [test_112] =>
iocage_ip4: '-'
ok: [test_113] =>
iocage_ip4: '-'
ok: [test_131] =>
iocage_ip4: 10.1.0.224
ok: [test_132] =>
iocage_ip4: 10.1.0.185
ok: [test_133] =>
iocage_ip4: 10.1.0.225
TASK [debug] *******************************************************************
ok: [ansible_client] =>
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']
test_up: ['test_111', 'test_131', 'test_132', 'test_133']
region_EU: ['test_111', 'test_112', 'test_113']
region_US: ['test_131', 'test_132', 'test_133']
PLAY RECAP *********************************************************************
ansible_client : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_111 : ok=1 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
Playbook pb-test-EU.yml
- hosts: region_EU
tasks:
- debug:
var: ansible_host
Playbook output - EU running hosts
Limit the EU region to running hosts
(env) > ansible-playbook pb-test-EU.yml -i hosts -l test_up
PLAY [region_EU] ***************************************************************
TASK [debug] *******************************************************************
ok: [test_111] =>
ansible_host: 10.1.0.174
PLAY RECAP *********************************************************************
test_111 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0