FreeKB - Ansible playbook- facts
Ansible playbook- facts

Home > Search


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

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": "10.1.2.3"
"ansible_distribution": "CentOS"

 

Here is a playbook that print facts.

---
- hosts: all
  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'"

 

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 a9ff2 in the box below so that we can be sure you are a human.




Comments