Don Branson
Don Branson

Reputation: 13707

Ansible not caching facts for later use

I'm using Vagrant and Ansible 1.9 to create a QA environment with an haproxy front-end (in a loadbalancer group) and two jetty backend servers (in a webservers group). The haproxy.cfg is a template with the following lines to display what Ansible is caching:

{% for host in groups['webservers'] %}
    server {{ host }} {{ hostvars[host] }}:8080 maxconn 1024
{% endfor %}

The final config would use something like:

server {{ host }} {{ hostvars[host]['ansible_eth1']['ipv4']['address'] }}:8080 maxconn 1024

to configure the ip address of each backend service.

Based on this: http://docs.ansible.com/guide_rolling_upgrade.html I'm using the following to try provoke Ansible to cache the ip address of each webserver:

- hosts: webservers
  user: vagrant
  name: Gather facts from webservers
  tasks: []

The webservers are built first, and the loadbalancer last. However, the webservers' ip addresses are not being cached. This is all that's there:

server qa01 {'ansible_ssh_host': '127.0.0.1', 'group_names': ['webservers'], 'inventory_hostname': 'qa01', 'ansible_ssh_port': 2222, 'inventory_hostname_short': 'qa01'}:8080 maxconn 1024

What do I need to do for those values to cache? It seems like Ansible knows the values, since:

- hosts: all
  user: vagrant
  sudo: yes
  tasks:
  - name: Display all variables/facts known for a host
    debug: var=hostvars[inventory_hostname]

will display the full collection of values, including the ip address I need.

Upvotes: 1

Views: 1995

Answers (1)

Don Branson
Don Branson

Reputation: 13707

I learned at http://jpmens.net/2015/01/29/caching-facts-in-ansible/ that cached facts are flushed between groups by default. But all the facts, including the info I need from the webservers, can be made available when provisioning the loadbalancer by configuring ansible to drop the cache on disk. Creating this ansible.cfg in the same folder as my Vagrantfile fixes the issue:

[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/mycachedir

This block was unnecessary and I removed it:

- hosts: webservers
  user: vagrant
  name: Gather facts from webservers
  tasks: []

Upvotes: 1

Related Questions