Every time you run an ansible command remotely, it starts by collecting a list fo facts about the system.
This facts are variables that can be used in the playbook.
collecting facts can be disabled to speed-up the playbook run
We can add custom facts to the target system by writing to /etc/ansible/facts.d
a INI
file with .fact
extension.
Use the setup
(ansible.builtin.setup
) or gather_facts
module to list all the facts:
root@f12d33c83ada:~# ansible webservers -i inventory.yaml -m setup | head
w1 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.17.0.3"
],
"ansible_all_ipv6_addresses": [],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
Create a simple playbook that writes a file to the target host:
---
- name: first
hosts: webservers
tasks:
- name: write file
command: "touch /tmp/test"
Run the playbook on the inventory:
root@f12d33c83ada:~# ansible-playbook -i inventory.yaml playbook.yaml
PLAY [first] ********************************************************************************************
TASK [Gathering Facts] ********************************************************************************
ok: [w1]
TASK [write file] **************************************************************************************
changed: [w1]
PLAY RECAP ******************************************************************************************
w1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Here you see how the playbook included only one task, but it ended up running two tasks, because it always start by collecting facts.
We can disable this by setting gather_facts: no
in the playbook:
---
- name: first
gather_facts: no
hosts: webservers
tasks:
- name: write file
command: "touch /tmp/test"
Then:
root@f12d33c83ada:~# ansible-playbook -i inventory.yaml playbook.yaml
PLAY [first] *******************************************************************************************************
TASK [write file] **************************************************************************************************
changed: [w1]
PLAY RECAP *********************************************************************************************************
w1 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0