Use Ansible's Fact Caching
This article Ansible Advent Calendar 2014 ` _ is of the third day article.
Note
初出時まったくのデタラメ書いていました。申し訳ございません!
Fact Caching is a function introduced since Ansible 1.8, making it possible to use Fact from other hosts.
Let's say that you have two web servers and a db server, and the web server must know the address of the db server. For this purpose,
- hosts: db
tasks: []
- hosts: web
tasks:
- template: ...
And then, in a template for the web server {{hostvars['db']['ansible_os_family'] }} '] [ 'ansible_os_family']}} `` as you can use the address of the db.
If you do this, you have to do to the db server first though it is a playbook to the web server. Fact Caching eliminates the need to execute playbook for db server by caching Fact acquired for a certain period of time.
Try using Fact Caching
Since it is invalidated by default, the following setting is described in ansible.cfg.
[defaults]
fact_caching = redis
fact_caching_timeout = 86400 # seconds
Currently redis is the only place to store cached facts.
Save to redis
In order to use redis you need to put redis python library,
pip install redis
I need you.
By the way, the following two key seems to be registered in redis.
- "Ansible_cache_keys"
- "Ansible_factstargetmachine"
Save to file
It is not written in the document, but besides redis
- Jsonfile
- Memcached
- Memory
You can use. I will do this to use jsonfile.
[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/cache
fact_caching_timeout = 86400 # seconds
However, because the area around here is not official, it is fluid, and Japanese is included
https://github.com/ansible/ansible/blob/60b51ef6c3e4eeca5ee1170ba32bc3 9284db97ae/lib/ansible/utils/__init__.py#L232
I can not write out JSON at around, so please wait. ( Issue in up)