FreeKB - gather_facts module
Ansible - gather_facts module

This assumes you have installed Ansible, are familiar with the ansible-playbook command and have created a playbook.

Facts are raw data about a server. On your Ansible server, the following command will display facts about your Ansible server.

ansible localhost -m setup


For example, here are some facts.

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


Here is a playbook that print facts.

- hosts: all
  - 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: []

TASK [debug]
ok: [] => {
    "msg": "server1"

TASK [debug]
ok: [] => {
    "msg": ""

TASK [debug]
ok: [] => {
    "msg": "CentOS"

PLAY RECAP : 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'"


By default, facts are gathered. However, if you do not need to use facts, you can tell your playbook 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 1c997 in the box below so that we can be sure you are a human.