# 015 Multiple inventory cache.

Extending example 014.

## Tree

    shell> tree .
    .
    ├── ansible.cfg
    ├── hosts
    │   ├── 01_iocage.yml
    │   └── 02_iocage.yml
    └── pb-vars-ip4.yml

## Synopsis

Cache enabled in multiple inventory files.

## Inventory hosts/01_iocage.yml

Set unique cache_prefix

    plugin: vbotka.freebsd.iocage
    host: 10.1.0.18
    user: admin
    env:
      - CRYPTOGRAPHY_OPENSSL_NO_LEGACY=1
    get_properties: False
    cache: True
    cache_plugin: ansible.builtin.jsonfile
    cache_connection: /var/tmp/inventory_cache
    cache_timeout: 3600
    cache_prefix: iocage_01_
    strict: True
    compose:
      ansible_host: iocage_ip4
    groups:
        test_01: inventory_hostname.startswith('test')
    keyed_groups:
      - prefix: distro_01
        key: iocage_release
      - prefix: state_01
        key: iocage_state

## Inventory hosts/02_iocage.yml

Set unique cache_prefix

    plugin: vbotka.freebsd.iocage
    host: 10.1.0.73
    user: admin
    env:
      - CRYPTOGRAPHY_OPENSSL_NO_LEGACY=1
    get_properties: False
    cache: True
    cache_plugin: ansible.builtin.jsonfile
    cache_connection: /var/tmp/inventory_cache
    cache_timeout: 3600
    cache_prefix: iocage_02_
    strict: True
    compose:
      ansible_host: iocage_ip4
    groups:
        test_02: inventory_hostname.startswith('test')
    keyed_groups:
      - prefix: distro_02
        key: iocage_release
      - prefix: state_02
        key: iocage_state

Warning

Common cache_prefix would make the caches to override each other repeatedly.

## Playbook pb-vars-ip4.yml

    - hosts: all

      tasks:

        - debug:
            var: iocage_ip4

## Playbook output

    (env) > ansible-playbook pb-vars-ip4.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.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

    PLAY RECAP ******************************************************************************************
    ansible_client: ok=1    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

Note

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

- The jail ansible_client from iocage_02 overrides the one from iocage_01

## Cache

Take a look at the cache

    shell> cat /var/tmp/inventory_cache/iocage_01_vbotka.freebsd.iocage_a5393s_7eb74

    {
        "_meta": {
            "hostvars": {
                "ansible_client": {
                    "iocage_basejail": "yes",
                    "iocage_boot": "off",
                    "iocage_ip4": "10.1.0.198",
                    "iocage_ip6": "-",
                    "iocage_jid": "-",
                    "iocage_release": "13.4-RELEASE-p2",
                    "iocage_state": "down",
                    "iocage_template": "-",
                    "iocage_type": "jail"
                },
                "test_101": {
                    "iocage_basejail": "yes",
                    "iocage_boot": "off",
                    "iocage_ip4": "10.1.0.101",
                    "iocage_ip6": "-",
                    "iocage_jid": "-",
                    "iocage_release": "13.4-RELEASE-p2",
                    "iocage_state": "down",
                    "iocage_template": "ansible_client",
                    "iocage_type": "jail"
                },
                "test_102": {
                    "iocage_basejail": "yes",
                    "iocage_boot": "off",
                    "iocage_ip4": "10.1.0.102",
                    "iocage_ip6": "-",
                    "iocage_jid": "-",
                    "iocage_release": "13.4-RELEASE-p2",
                    "iocage_state": "down",
                    "iocage_template": "ansible_client",
                    "iocage_type": "jail"
                },
                "test_103": {
                    "iocage_basejail": "yes",
                    "iocage_boot": "off",
                    "iocage_ip4": "10.1.0.103",
                    "iocage_ip6": "-",
                    "iocage_jid": "-",
                    "iocage_release": "13.4-RELEASE-p2",
                    "iocage_state": "down",
                    "iocage_template": "ansible_client",
                    "iocage_type": "jail"
                }
            }
        }
    }

    shell> cat /var/tmp/inventory_cache/iocage_02_vbotka.freebsd.iocage_a5393s_5a95f

    {
        "_meta": {
            "hostvars": {
                "ansible_client": {
                    "iocage_basejail": "yes",
                    "iocage_boot": "off",
                    "iocage_ip4": "10.1.0.199",
                    "iocage_ip6": "-",
                    "iocage_jid": "None",
                    "iocage_release": "14.1-RELEASE-p6",
                    "iocage_state": "down",
                    "iocage_template": "-",
                    "iocage_type": "jail"
                },
                "test_111": {
                    "iocage_basejail": "yes",
                    "iocage_boot": "off",
                    "iocage_ip4": "10.1.0.111",
                    "iocage_ip6": "-",
                    "iocage_jid": "None",
                    "iocage_release": "14.1-RELEASE-p6",
                    "iocage_state": "down",
                    "iocage_template": "ansible_client",
                    "iocage_type": "jail"
                },
                "test_112": {
                    "iocage_basejail": "yes",
                    "iocage_boot": "off",
                    "iocage_ip4": "10.1.0.112",
                    "iocage_ip6": "-",
                    "iocage_jid": "None",
                    "iocage_release": "14.1-RELEASE-p6",
                    "iocage_state": "down",
                    "iocage_template": "ansible_client",
                    "iocage_type": "jail"
                },
                "test_113": {
                    "iocage_basejail": "yes",
                    "iocage_boot": "off",
                    "iocage_ip4": "10.1.0.113",
                    "iocage_ip6": "-",
                    "iocage_jid": "None",
                    "iocage_release": "14.1-RELEASE-p6",
                    "iocage_state": "down",
                    "iocage_template": "ansible_client",
                    "iocage_type": "jail"
                }
            }
        }
    }