Ansible Facts
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
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.
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:
Note that you must be in your project directory where you inventory file is located. For most of the course we have used the ~/code directory of our user account. For me, it's /home/paulh/code
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
:
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 ~/code/first_playbook.yml
Modify the playbook to change the path to /tmp/{{ ansible_hostname }}_ansible_created_this_file.txt
as shown below:
---
- hosts: all
tasks:
- name: Create an empty file
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:
Verification
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:
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:
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.