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 region to create the groups region_EU and region_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:113 from the host iocage_02

    • test_131:133 from the host iocage_04

    and creates inventory groups test_02 and test_04

  • The inventory plugin ansible.builtin.constructed creates the inventory groups:

    • test comprising hosts starting 'test'

    • test_up comprising running hosts starting 'test'

    • region_EU comprising hosts where the variable region=EU

    • region_US comprising hosts where the variable region=US

Notes

  • The inventory files in the directory hosts are 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

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
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

hosts/group_vars/test_02/region.yml
region: EU
hosts/group_vars/test_04/region.yml
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_vars and host_vars

  • The playbook group_vars and host_vars will be silently ignored.

See Variable precedence. Where should I put a variable?

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