# 016 Multiple inventory constructed.

Extending example 015.

## Tree

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

## Synopsis

- Use the inventory plugin ansible.builtin.constructed after the two iocage inventory files.

- The iocage plugin gets the jails(hosts):

  - test_01:03 from the host iocage_01

  - test_11:13 from the host iocage_02

  and creates inventory groups test_01 and test_02

- The constructed plugin creates inventory group test including all hosts starting ‘test’

## Inventory hosts/01_iocage.yml

    plugin: vbotka.freebsd.iocage
    host: 10.1.0.18
    user: admin
    env:
      - CRYPTOGRAPHY_OPENSSL_NO_LEGACY=1
    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
    env:
      - CRYPTOGRAPHY_OPENSSL_NO_LEGACY=1
    compose:
      ansible_host: iocage_ip4
    groups:
      test_02: inventory_hostname.startswith('test')

## Inventory hosts/03_constructed.yml

    plugin: ansible.builtin.constructed
    groups:
        test: inventory_hostname.startswith('test')

## 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.102
    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.112
    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']

    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

Note

- The inventory files in hosts are evaluated in alphabetical order.

- See 015 Multiple inventory cache.