Ansible facts

Aakash Bhardwaj
2 min readJul 8, 2021


Ansible facts are data related to your remote systems, including operating systems, IP addresses, attached filesystems, and more. You can access this data in the ansible_facts variable. By default, you can also access some Ansible facts as top-level variables with the ansible_ prefix. You can disable this behavior using the INJECT_FACTS_AS_VARS setting. To see all available facts, add this task to a play:

- name: Print all available facts
var: ansible_facts

Facts include a large amount of variable data, in which we will use this:

fig: A key named ansible_distribution containing OS name as it’s value

Now, we will use this approach to retrieve the file containing the variables which has a user defined key which stores the name of the package to be downloaded. Like, For downloading apache webserver in Redhat we mention “httpd” ; for Ubuntu we mention “apache2” etc

Now, let’s get started with practical:

Ansible playbook :

fig. B

Running the playbook:

This will automatically fetch the vars file named RedHat. But how?, {{ ansible_facts[‘distribution’] }} will get replaced with Os name(as depicted in above fig. A) and since we saved our file with OS name it will replace.

This is our variable file:

Note: The name of the file should be same as OS specific name otherwise it will not work! It is case-sensitive RedHat, CentOS, Ubuntu and should be saved with these names only!!!(any other variations like Redhat/redhat, CentOs/centos, ubuntu etc will not work).

Here, is the output of our webpage showing us the content mentioned in fig. B