016 Multiple inventory constructed

Extending example 015.

Use case

Use the inventory plugin ansible.builtin.constructed after the two inventory plugin vbotka.freebsd.iocage configuration files.

Tree

shell> tree .
.
├── ansible.cfg
├── hosts
│   ├── 01_iocage.yml
│   ├── 02_iocage.yml
│   └── 99_constructed.yml
└── pb-test.yml

Synopsis

  • The iocage plugin 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 constructed plugin creates inventory groups:

    • test including all hosts starting ‘test’

    • test_up including running hosts starting ‘test’

Requirements

Notes

Configuration ansible.cfg

[defaults]
gathering = explicit
callback_result_format = yaml

[connection]
pipelining = true

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

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

(env) > ansible-playbook pb-test.yml -i hosts

PLAY [test] ***********************************************************************************

TASK [debug] **********************************************************************************
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: [test_101] => 
    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']
	test_up: ['test_102', 'test_103', 'test_112', 'test_113']

PLAY RECAP ************************************************************************************
test_101: ok=2    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