14
votes

I'm having trouble running a full playbook because some of the facts later plays depend on are modified in earlier plays, but ansible doesn't update facts mid-run.

Running ansible somehost -m setup when the whole playbook starts against a new VPS:

"ansible_selinux": {
    "status": "disabled"
}, 

My playbook contains a play that installs SELinux and reboots the server (while ansible wait_for's), and a later task uses the conditional when: ansible_selinux.status != 'disabled'. However even though SELinux is now installed and enforcing (which required the reboot) the facts for the system still show SELinux is disabled so that conditional fails and the task is skipped.

Running the playbook again of course works because facts are updated and now return:

"ansible_selinux": {
    "config_mode": "enforcing", 
    "mode": "enforcing", 
    "policyvers": 28, 
    "status": "enabled", 
    "type": "targeted"
}

Is there any way to make facts refresh mid-playbook? Maybe the hack is to set_fact on ansible_selinux.status myself after the reboot?

Update: Well that was too easy, thanks to BruceP I added this task to fetch updated facts at the end of my SELinux play

- name: SELinux - Force ansible to regather facts
  setup: filter='ansible_selinux'
2

2 Answers

22
votes

Add this in your playbook to use the setup module to update the facts.

For example I added another interface with DHCP now I want to know what address it has then do this:

- name: do facts module to get latest information
  setup:
7
votes

The setup module is what Ansible uses to gather facts. It implicitly runs it before running your playbook. You should be able to run it manually within your playbook to update your facts.