Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ansible fetch updated facts in middle of a playbook?

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'
like image 540
xref Avatar asked Feb 03 '16 19:02

xref


People also ask

How are facts collected in ansible?

Ansible facts are data gathered about target nodes (host nodes to be configured) and returned back to controller nodes. Ansible facts are stored in JSON format and are used to make important decisions about tasks based on their statistics. Facts are in an ansible_facts variable, which is managed by Ansible Engine.

What is gather facts in ansible playbook?

This module is automatically called by playbooks to gather useful variables about remote hosts that can be used in playbooks. It can also be executed directly by /usr/bin/ansible to check what variables are available to a host. Ansible provides many facts about the system, automatically.

How do I keep secret data in my playbook under ansible?

How do I keep secret data in my playbook? ¶ If you would like to keep secret data in your Ansible content and still share it publicly or keep things in source control, see Vault. This can be used to keep verbose output but hide sensitive information from others who would otherwise like to be able to see the output.

What parameter allows narrowing the facts returned in the setup module?

Use the filter parameter if you do not want to display some collected facts.


2 Answers

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:
like image 179
Brent Avatar answered Sep 20 '22 08:09

Brent


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.

like image 25
Bruce P Avatar answered Sep 18 '22 08:09

Bruce P