Facts
Ansible facts are variables that are automatically discovered by Ansible on a managed host. Facts are a way to retrieve the state of a managed host.
One way to see what facts are gathered for your managed hosts is to run a short playbook that gathers facts and uses the debug module to print the value of the ansible_facts
variable.
Which displays the content of the ansible_facts
variable in JSON format.
When a fact is used in a playbook, Ansible dynamically substitutes the variable name for the fact with the corresponding value:
---
- hosts: all
tasks:
- name: Prints various Ansible facts
debug:
msg: >
The default IPv4 address of {{ ansible_facts.fqdn }}
is {{ ansible_facts.default_ipv4.address }}
You can turn off the old naming system by setting the inject_facts_as_vars
parameter in the [default]
section of the Ansible configuration file to false
.
To disable fact gathering for a play, set the gather_facts keyword to no:
Creating Custom
By default, the setup module loads custom facts from files and scripts in each managed host's /etc/ansible/facts.d
directory. The name of each file or script must end in .fact
in order to be used. Dynamic custom fact scripts must output JSON-formatted facts and must be executable.
Magic Variables
Some variables are not facts or configured through the setup module, but are also automatically set by Ansible. Magic variables can also be useful to get information specific to a particular managed host.
Common ones include:
- hostvars
- group_names
- groups
- inventory_hostname
Examples