FreeKB - Ansible gather_facts parameter
Ansible - gather_facts parameter

Facts are raw data about a system. On your control node (that's your Ansible server), the following command will display facts about your control node.

ansible localhost -m setup

 

For example, here are some facts.

"ansible_hostname": "server1"
"ansible_all_ip4v_addresses": "10.1.2.3"
"ansible_distribution": "CentOS"

 

By default, a playbook will gather facts. Here is a playbook that prints facts.

---
- hosts: all
  tasks:
  - debug: msg="{{ansible_hostname}}"
  - debug: msg="{{ansible_all_ipv4_addresses}}"
  - debug: msg="{{ansible_distribution}}"

 

Since facts are gathered by default, there is no need to use the gather_facts parameter. However, you can include the gather_facts parameter if you would like, as this could emphasize that facts are indeed being gathered. The gather facts parameter is a wrapper that actually invokes the setup module

The ansible-doc gather_facts command can be used to show the Ansible documention on the gather_facts parameter.

---
- hosts: all
  gather_facts: true
  tasks:
  - debug: msg="{{ansible_hostname}}"
  - debug: msg="{{ansible_all_ipv4_addresses}}"
  - debug: msg="{{ansible_distribution}}"

 

Running the playbook will produce the following.

PLAY [all]

TASK [Gathering Facts]
ok: [server1.example.com]

TASK [debug]
ok: [server1.example.com] => {
    "msg": "server1"
}

TASK [debug]
ok: [server1.example.com] => {
    "msg": "10.1.2.3"
}

TASK [debug]
ok: [server1.example.com] => {
    "msg": "CentOS"
}

PLAY RECAP
server1.example.com : ok=2  changed=0  unreacable=0  failed=0

 

Facts are often used with when to do something when a fact evaluates to true or false. For example.

- name: do something
  when: "ansible_distribution == 'CentOS'"

 

If you do not need to use facts, the gather_facts module can be set to "no" or "false" to not gather facts.

---
- hosts: all
  gather_facts: false

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 161dc in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |