Cache plugin implement a backend caching mechanism that allows Ansible to store gathered facts or inventory source data without the performance hit of retrieving them from source.
The default cache plugin is the memory plugin, which only caches the data for the current execution of Ansible. Other plugins with persistent storage are available to allow caching the data across runs.
You can use a separate cache plugin for inventory and facts. If an inventory-specific cache plugin is not provided and inventory caching is enabled, the fact cache plugin is used for inventory.
Only one fact cache plugin can be active at a time.
You can enable a cache plugin in the Ansible configuration, either via environment variable:
export ANSIBLE_CACHE_PLUGIN=jsonfile
or in the ansible.cfg
file:
[defaults] fact_caching=redis
If the cache plugin is in a collection use the fully qualified name:
[defaults] fact_caching = namespace.collection_name.cache_plugin_name
You will also need to configure other settings specific to each plugin. Consult the individual plugin documentation or the Ansible configuration for more details.
A custom cache plugin is enabled by dropping it into a cache_plugins
directory adjacent to your play, inside a role, or by putting it in one of the directory sources configured in ansible.cfg.
Inventory may be cached using a file-based cache plugin (like jsonfile). Check the specific inventory plugin to see if it supports caching. Cache plugins inside a collection are not supported for caching inventory. If an inventory-specific cache plugin is not specified Ansible will fall back to caching inventory with the fact cache plugin options.
The inventory cache is disabled by default. You may enable it via environment variable:
export ANSIBLE_INVENTORY_CACHE=True
or in the ansible.cfg
file:
[inventory] cache=True
or if the inventory plugin accepts a YAML configuration source, in the configuration file:
# dev.aws_ec2.yaml plugin: aws_ec2 cache: True
Similarly with fact cache plugins, only one inventory cache plugin can be active at a time and may be set via environment variable:
export ANSIBLE_INVENTORY_CACHE_PLUGIN=jsonfile
or in the ansible.cfg file:
[inventory] cache_plugin=jsonfile
or if the inventory plugin accepts a YAML configuration source, in the configuration file:
# dev.aws_ec2.yaml plugin: aws_ec2 cache_plugin: jsonfile
Consult the individual inventory plugin documentation or the Ansible configuration for more details.
Cache plugins are used automatically once they are enabled.
You can use ansible-doc -t cache -l
to see the list of available plugins. Use ansible-doc -t cache <plugin name>
to see specific documentation and examples.
See also
© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.9/plugins/cache.html