014 Inventory cache¶
Extending example 010.
Use case¶
Enable and test inventory cache.
Tree¶
shell> tree .
.
├── ansible.cfg
├── iocage.yml
└── pb-vars-ip4.yml
Requirements¶
Configuration ansible.cfg¶
[defaults]
gathering = explicit
callback_result_format = yaml
[connection]
pipelining = true
Inventory 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_
strict: True
compose:
ansible_host: iocage_ip4
release: iocage_release | split('-') | first
release_major: iocage_release | split('-') | first | split('.') | first
release_minor: iocage_release | split('-') | first | split('.') | last
groups:
test: inventory_hostname.startswith('test')
keyed_groups:
- prefix: distro
key: iocage_release
- prefix: state
key: iocage_state
Playbook pb-vars-ip4.yml¶
- hosts: all
tasks:
- debug:
var: iocage_ip4
Playbook output¶
It takes 4s in this case to create the dynamic inventory and construct the variables.
(The cache is disabled in iocage.yml. cache=False)
(env) > date +%r; \
ANSIBLE_STDOUT_CALLBACK=community.general.timestamp \
ansible-playbook pb-vars-ip4.yml \
-i iocage.yml \
-l test_113
13:49:31
PLAY [all] ************************************************************ 13:49:35
TASK [debug] ********************************************************** 13:49:35
ok: [test_113] => {
"iocage_ip4": "10.1.0.113"
}
PLAY RECAP ************************************************************ 13:49:35
test_113: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Inventory iocage.yml¶
Enable cache
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_
strict: True
compose:
ansible_host: iocage_ip4
release: iocage_release | split('-') | first
release_major: iocage_release | split('-') | first | split('.') | first
release_minor: iocage_release | split('-') | first | split('.') | last
groups:
test: inventory_hostname.startswith('test')
keyed_groups:
- prefix: distro
key: iocage_release
- prefix: state
key: iocage_state
Hint
If you do not configure cache_plugin Ansible falls back to caching inventory with the fact cache plugin you configured. For example,
shell> grep fact_caching ansible.cfg
fact_caching = ansible.builtin.jsonfile
fact_caching_connection = /var/tmp/ansible_cache
fact_caching_timeout = 3600
fact_caching_prefix = ''
Playbook output¶
If the cache is enabled the inventory and variables are provided by the cache immediately
(env) > date +%r; \
ANSIBLE_STDOUT_CALLBACK=community.general.timestamp \
ansible-playbook pb-vars-ip4.yml \
-i iocage.yml \
-l test_113
13:55:40
PLAY [all] ************************************************************ 13:55:41
TASK [debug] ********************************************************** 13:55:41
ok: [test_113] => {
"iocage_ip4": "10.1.0.113"
}
PLAY RECAP ************************************************************ 13:55:41
test_113: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Cache¶
Take a look at the cache
shell> cat /var/tmp/inventory_cache/iocage_vbotka.freebsd.iocage_a5393s_6a9dd
{
"_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.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_ip4_dict": {
"ip4": [
{
"ifc": "em0",
"ip": "10.1.0.111",
"mask": "24"
}
],
"msg": ""
},
"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_ip4_dict": {
"ip4": [
{
"ifc": "em0",
"ip": "10.1.0.112",
"mask": "24"
}
],
"msg": ""
},
"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_ip4_dict": {
"ip4": [
{
"ifc": "em0",
"ip": "10.1.0.113",
"mask": "24"
}
],
"msg": ""
},
"iocage_ip6": "-",
"iocage_jid": "60",
"iocage_release": "14.1-RELEASE-p6",
"iocage_state": "up",
"iocage_template": "ansible_client",
"iocage_type": "jail"
}
}
}
}