0/1 Lessons

Course Introduction

• 19min

0 / 2 lessons complete

IT Lab Setup

• 31min

0 / 3 lessons complete

Installing Ansible

• 57min

0 / 4 lessons complete

Managing your Ansible Inventory

• 2hr 45min

0 / 8 lessons complete

Ansible Basics

• 2hr 54min

0 / 9 lessons complete

Ansible Roles

• 2hr 38min

0 / 8 lessons complete

Ansible Galaxy

• 2hr 28min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 3hr 20min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 2hr 23min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Ansible Facts


Q&A (0)

Notes (0)

Resources (0)

Saving Progress...


There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, you will learn about Ansible facts and how to use them in your playbooks. Ansible facts are data related to your nodes (or hosts) that Ansible gathers and makes available for use. This knowledge is crucial when you need to make decisions or perform operations based on specific node characteristics. You'll see how to view a node's facts and then use one of these facts - the hostname - in a playbook task to create a uniquely named file.

Viewing Node Facts

Before we modify a playbook, let's first understand how to view the facts of a node. Facts provide detailed information about the host, such as network interfaces, operating system, IP addresses, and more. This information can be incredibly valuable for dynamic playbook execution.

To view the facts of a node, you can use the setup module in Ansible. This module gathers facts about the hosts and can be run as an ad-hoc command. Here's how you can do it:

ansible managed-node-1 -m setup

This command will display a JSON output with all the facts collected from managed-node-1.

We can pipe this to the less command to search through this document rather than scrolling through the data:

ansible managed-node-1 -m setup | less

Next, press / and type in ansible_hostname before pressing enter. This will search the output for a matching string. In our case, it should show managed-node-1:

Searching Ansible Facts with the Less Utility
Searching Ansible Facts with the Less Utility

Let's use the fact ansible_hostname in our playbook. Press q to exit the Less utility and return to our bash prompt.

Modifying the Playbook to Use Hostname in File Name

Now, let's use these facts in a practical scenario. We will modify the playbook we created earlier, and instead creating a file with a fixed name, we'll include the hostname of the node in the filename. This makes the file easily identifiable and linked to the specific node.

Open our playbook with the command below:

nano ~/first_playbook.yml

Modify the playbook to change the path to /tmp/{{ ansible_hostname }}_ansible_created_this_file.txt as shown below:

- hosts: all
    - name: Create an empty file
        path: /tmp/{{ ansible_hostname }}_ansible_created_this_file.txt
        state: touch

In this playbook:

The file module creates an empty file. The filename is dynamically generated using the ansible_hostname fact, which contains the hostname of the node.

Execute the new playbook

Now let's execute our updated playbook:

ansible-playbook first_playbook.yml

You should see the change occur on each node:

image 32
image 32


After running this playbook, you can verify its effect by checking the /tmp directory on each node. You should find files named similarly to node1_ansible_created_this_file.txt, where node1 is the respective hostname of the node.

On the Ansible Controller, I see this:

Created file using Ansible Facts
Created file using Ansible Facts

Verify with Ad-hoc Shell Module:

Let's check on all our hosts to make sure the file was created as desired! To do this, we will run an Ad-hoc command that uses the shell module to execute the ls command on all hosts. We can reduce the output by adding a wildcard in the filename like so: ls /tmp/*ansible_created_this_file.txt. This will list only files that match the pattern provided.

ansible all -m shell -a "ls /tmp/*ansible_created_this_file.txt"

This will return output like so:

Ansible Ad-hoc Shell Command Output
Ansible Ad-hoc Shell Command Output

This is a bit easier than SSH-ing into each host separately and is appropriate since the ls command doesn't make any changes to the system.


You've now learned how to view Ansible facts and use them in a playbook to perform dynamic operations based on node-specific information. By incorporating the hostname into a file name, you've seen how Ansible allows for flexible and dynamic playbook designs. Keep exploring Ansible facts to discover more ways to tailor your automation tasks to your environment's specifics!

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Notify of
profile avatar
Inline Feedbacks
View all comments