015 Multiple inventory cache

Extending example 014 Inventory cache.

Use case

Enabled cache in multiple inventory files.

Tree

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

Synopsis

At two managed nodes:

Requirements

ansible.cfg

[defaults]
gathering = explicit
callback_result_format = yaml
display_skipped_hosts = false
host_key_checking = false

[connection]
pipelining = true

Inventory hosts

Set unique cache_prefix

hosts/02_iocage.yml
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_
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
hosts/04_iocage.yml
plugin: vbotka.freebsd.iocage
host: 10.1.0.29
user: admin
get_properties: False
cache: True
cache_plugin: ansible.builtin.jsonfile
cache_connection: /var/tmp/inventory_cache
cache_timeout: 3600
cache_prefix: iocage_04_
compose:
  ansible_host: iocage_ip4
groups:
    test_04: inventory_hostname.startswith('test')
keyed_groups:
  - prefix: distro_04
    key: iocage_release
  - prefix: state_04
    key: iocage_state

Warning

Common cache_prefix would make the cache files to override each other repeatedly.

Playbook pb-vars-ip4.yml

- hosts: all

  tasks:

    - debug:
        var: iocage_ip4

Playbook output - Display iocage_ip4

(env) > ansible-playbook pb-vars-ip4.yml -i hosts
PLAY [all] *********************************************************************

TASK [debug] *******************************************************************
ok: [ansible_client] => 
    iocage_ip4: 10.1.0.198
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
ok: [test_131] => 
    iocage_ip4: 10.1.0.131
ok: [test_132] => 
    iocage_ip4: 10.1.0.132
ok: [test_133] => 
    iocage_ip4: 10.1.0.133

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

Note

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

  • The jail ansible_client from iocage_04 overrides the one from iocage_02

Cache

Look at the cache. For example,

shell> cat /var/tmp/inventory_cache/iocage_02_vbotka.freebsd.iocage_a5393s_8ea2a
{
    "_meta": {
        "hostvars": {
            "ansible_client": {
                "iocage_basejail": "yes",
                "iocage_boot": "off",
                "iocage_ip4": "10.1.0.199",
                "iocage_ip4_dict": {
                    "ip4": [
                        {
                            "ifc": "em0",
                            "ip": "10.1.0.199",
                            "mask": "24"
                        }
                    ],
                    "msg": ""
                },
                "iocage_ip6": "-",
                "iocage_jid": "None",
                "iocage_release": "14.2-RELEASE-p3",
                "iocage_state": "down",
                "iocage_template": "-",
                "iocage_type": "jail"
            },
            "test_111": {
                "iocage_basejail": "yes",
                "iocage_boot": "off",
                "iocage_ip4": "10.1.0.111",
                "iocage_ip4_dict": {
                    "ip4": [
                        {
                            "ifc": "em0",
                            "ip": "10.1.0.111",
                            "mask": "24"
                        }
                    ],
                    "msg": ""
                },
                "iocage_ip6": "-",
                "iocage_jid": "None",
                "iocage_release": "14.2-RELEASE-p3",
                "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_ip4_dict": {
                    "ip4": [
                        {
                            "ifc": "em0",
                            "ip": "10.1.0.112",
                            "mask": "24"
                        }
                    ],
                    "msg": ""
                },
                "iocage_ip6": "-",
                "iocage_jid": "None",
                "iocage_release": "14.2-RELEASE-p3",
                "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_ip4_dict": {
                    "ip4": [
                        {
                            "ifc": "em0",
                            "ip": "10.1.0.113",
                            "mask": "24"
                        }
                    ],
                    "msg": ""
                },
                "iocage_ip6": "-",
                "iocage_jid": "None",
                "iocage_release": "14.2-RELEASE-p3",
                "iocage_state": "down",
                "iocage_template": "ansible_client",
                "iocage_type": "jail"
            }
        }
    }
}
shell> cat cat /var/tmp/inventory_cache/iocage_04_vbotka.freebsd.iocage_a5393s_d0c35
{
    "_meta": {
        "hostvars": {
            "ansible_client": {
                "iocage_basejail": "yes",
                "iocage_boot": "off",
                "iocage_ip4": "10.1.0.198",
                "iocage_ip4_dict": {
                    "ip4": [
                        {
                            "ifc": "vnet0",
                            "ip": "10.1.0.198",
                            "mask": "24"
                        }
                    ],
                    "msg": ""
                },
                "iocage_ip6": "-",
                "iocage_jid": "None",
                "iocage_release": "14.3-RELEASE-p1",
                "iocage_state": "down",
                "iocage_template": "-",
                "iocage_type": "jail"
            },
            "test_131": {
                "iocage_basejail": "yes",
                "iocage_boot": "off",
                "iocage_ip4": "10.1.0.131",
                "iocage_ip4_dict": {
                    "ip4": [
                        {
                            "ifc": "vnet0",
                            "ip": "10.1.0.131",
                            "mask": "24"
                        }
                    ],
                    "msg": ""
                },
                "iocage_ip6": "-",
                "iocage_jid": "None",
                "iocage_release": "14.3-RELEASE-p1",
                "iocage_state": "down",
                "iocage_template": "ansible_client",
                "iocage_type": "jail"
            },
            "test_132": {
                "iocage_basejail": "yes",
                "iocage_boot": "off",
                "iocage_ip4": "10.1.0.132",
                "iocage_ip4_dict": {
                    "ip4": [
                        {
                            "ifc": "vnet0",
                            "ip": "10.1.0.132",
                            "mask": "24"
                        }
                    ],
                    "msg": ""
                },
                "iocage_ip6": "-",
                "iocage_jid": "None",
                "iocage_release": "14.3-RELEASE-p1",
                "iocage_state": "down",
                "iocage_template": "ansible_client",
                "iocage_type": "jail"
            },
            "test_133": {
                "iocage_basejail": "yes",
                "iocage_boot": "off",
                "iocage_ip4": "10.1.0.133",
                "iocage_ip4_dict": {
                    "ip4": [
                        {
                            "ifc": "vnet0",
                            "ip": "10.1.0.133",
                            "mask": "24"
                        }
                    ],
                    "msg": ""
                },
                "iocage_ip6": "-",
                "iocage_jid": "None",
                "iocage_release": "14.3-RELEASE-p1",
                "iocage_state": "down",
                "iocage_template": "ansible_client",
                "iocage_type": "jail"
            }
        }
    }
}