Sunday 28 February 2016

Ansible Facts Caching With Redis

Ansible is an amazing tool for automation of tasks. Facts are details about the host that Ansible gathers when it connect to the machine. It will gather a significant amount of useful information that can be used in Playbooks e.g. OS version, IPv4/IPv6 addresses. To make Ansible go faster we can store the facts in a cache the first time in connects to the hosts. On subsequent Playbook executions Ansible will only look up the facts in the cache instead of gathering them again. There is a cache option fact_caching_timeout that can be modified to define how long the cache should be valid for in seconds.

Setup Redis for Ansible Caching

On Arch Linux install redis and enable it
  $ pacman -S redis
  $ systemctl enable redis
  $ systemctl start redis
Install the Python Redis packages
$ pacman -S python2-redis python-redis
Check redis is responding
$ redis-cli ping
PONG
Update /etc/ansible/ansible.cfg in a text editor
gathering = smart
fact_caching = redis
fact_caching_timeout = 86400
fact_caching_connection = localhost:6379:0
After running a Playbook redis-cli can be used to view the keystore e.g.
$ redis-cli 
127.0.0.1:6379> keys *
1) "ansible_cache_keys"
2) "ansible_factsDB1"

3 comments: