019 Inventory option use_vars_plugins¶
Extending example 016.
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 region to create the groups region_EU and region_US.
Tree¶
shell> tree .
.
├── ansible.cfg
├── hosts
│ ├── 01_iocage.yml
│ ├── 02_iocage.yml
│ ├── 99_constructed.yml
│ └── group_vars
│ ├── test_01
│ │ └── region.yml
│ └── test_02
│ └── region.yml
├── pb-test-all.yml
└── pb-test-US.yml
Synopsis¶
The inventory plugin vbotka.freebsd.iocage gets the jails(hosts):
test_101:103 from the host iocage_01
test_111:113 from the host iocage_02
and creates inventory groups test_01 and test_02
The inventory plugin ansible.builtin.constructed creates the inventory groups:
test including all hosts starting ‘test’
test_up including running hosts starting ‘test’
region_EU including all hosts with the variable region=EU
region_US including all hosts with the variable region=US
Notes¶
The inventory files in hosts are evaluated in alphabetical order.
List all jails at iocage_01¶
[iocage_01]# iocage list -l
+------+----------------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
| JID | NAME | BOOT | STATE | TYPE | RELEASE | IP4 | IP6 | TEMPLATE | BASEJAIL |
+======+================+======+=======+======+=================+=====================+=====+================+==========+
| None | ansible_client | off | down | jail | 13.4-RELEASE-p2 | vnet0|10.1.0.198/24 | - | - | no |
+------+----------------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
| None | test_101 | off | down | jail | 13.4-RELEASE-p2 | vnet0|10.1.0.101/24 | - | ansible_client | no |
+------+----------------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
| 6 | test_102 | off | up | jail | 13.4-RELEASE-p2 | epair0b|10.1.0.245 | - | ansible_client | no |
+------+----------------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
| 5 | test_103 | off | up | jail | 13.4-RELEASE-p2 | vnet0|10.1.0.103/24 | - | ansible_client | no |
+------+----------------+------+-------+------+-----------------+---------------------+-----+----------------+----------+
List all 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.1-RELEASE-p6 | em0|10.1.0.199/24 | - | - | no |
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
| None | test_111 | off | down | jail | 14.1-RELEASE-p6 | em0|10.1.0.111/24 | - | ansible_client | no |
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
| 9 | test_112 | off | up | jail | 14.1-RELEASE-p6 | epair0b|10.1.0.147 | - | ansible_client | no |
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
| 8 | test_113 | off | up | jail | 14.1-RELEASE-p6 | em0|10.1.0.113/24 | - | ansible_client | no |
+------+----------------+------+-------+------+-----------------+--------------------+-----+----------------+----------+
Configuration ansible.cfg¶
[defaults]
gathering = explicit
callback_result_format = yaml
[connection]
pipelining = true
Inventory hosts/01_iocage.yml¶
plugin: vbotka.freebsd.iocage
host: 10.1.0.18
user: admin
sudo: true
compose:
ansible_host: iocage_ip4
groups:
test_01: inventory_hostname.startswith('test')
Inventory 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')
Inventory hosts/99_constructed.yml¶
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
hosts/group_vars/test_01/region.yml¶
region: US
hosts/group_vars/test_02/region.yml¶
region: EU
Hint
Run the below command to see the complete inventory
shell> ansible-inventory -i hosts --list --yaml
Warning
The option use_vars_plugins reads the inventory group_vars and host_vars
The playbook group_vars and host_vars will 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¶
(env) > ansible-playbook pb-test-all.yml -i hosts
PLAY [all] ******************************************************************************************
TASK [debug] ****************************************************************************************
ok: [ansible_client] =>
iocage_ip4: 10.1.0.199
ok: [test_101] =>
iocage_ip4: 10.1.0.101
ok: [test_102] =>
iocage_ip4: 10.1.0.245
ok: [test_103] =>
iocage_ip4: 10.1.0.103
ok: [test_111] =>
iocage_ip4: 10.1.0.111
ok: [test_112] =>
iocage_ip4: 10.1.0.147
ok: [test_113] =>
iocage_ip4: 10.1.0.113
TASK [debug] ****************************************************************************************
ok: [ansible_client] =>
msg: |-
all: ['ansible_client', 'test_101', 'test_102', 'test_103', 'test_111', 'test_112', 'test_113']
ungrouped: ['ansible_client']
test_01: ['test_101', 'test_102', 'test_103']
test_02: ['test_111', 'test_112', 'test_113']
test: ['test_101', 'test_102', 'test_103', 'test_111', 'test_112', 'test_113']
region_US: ['test_101', 'test_102', 'test_103']
test_up: ['test_102', 'test_103', 'test_112', 'test_113']
region_EU: ['test_111', 'test_112', 'test_113']
PLAY RECAP ******************************************************************************************
ansible_client: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_101 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_102 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_103 : ok=1 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
Playbook pb-test-US.yml¶
- hosts: region_US
tasks:
- debug:
var: ansible_host
Playbook output¶
Limit the US region to running hosts
(env) > ansible-playbook pb-test-US.yml -i hosts -l test_up
PLAY [region_US] ******************************************************************************
TASK [debug] **********************************************************************************
ok: [test_102] =>
ansible_host: 10.1.0.245
ok: [test_103] =>
ansible_host: 10.1.0.103
PLAY RECAP ************************************************************************************
test_102: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_103: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0