How to Print Ansible Facts
Such insight toward the target systems is now very important for making intelligent decisions. This is where Ansible facts come into play. Ansible facts are all the information automatically gathered by Ansible about the infrastructure it is part of, from hardware details to network configurations, operating system specifics, and user data. These facts can then be used to make your playbooks dynamic and able to fit current infrastructure states. The following guide should help you go step by step toward being able to print out Ansible facts so that you are aware of how to use the power of facts in your automation workflows. This article is intended for beginners and advanced users who want to explore this tool, giving an overview of examples and a collection of FAQs.
Primary Terminologies
Facts about Ansible
- Ansible facts are variables that will be found or discovered by Ansible automatically upon connecting to the managed host. The information available covers vast details regarding the host: hardware, network settings, OS details, among others. These facts are necessary to derive dynamic decisions in a playbook.
Collecting Information:
- This is where Ansible gathers information about the hosts it manages. Ansible gathers facts, by default at the beginning of a play using the setup module. This behavior can further be controlled with the gather_facts directive.
Setup Module:
- Ansible compiles a collection of facts about the managed host by using a setup module. Once invoked, it will return a dictionary that contains all facts that were collected. It also supports collecting selected facts by use of parameters from the available ones.
Playbook:
- A playbook is an Ansible document in YAML containing a list of plays. These define sets of activities that can be carried out using hosts. Playbooks are where Ansible's orchestration really takes place.
Task:
- A task is an operation in a playbook. The task is the smallest unit of work inside a play. For this purpose, every task is an execution of a call to an Ansible module in order to do something, for example: Install a package, copy a file, collect facts.
Module:
- Modules are what Ansible executes on a target machine. Put another way: a module is a program used to do a specific work on target nodes. Modules can literally manage packages or cloud resources.
Host Variables:
- Host variables are facts or custom variables of a managed host. They can be general facts that Ansible is collecting, or they could be variables that you define on your own in inventory files or playbooks.
Inventory:
- An inventory is a file or directory that includes information on hosts that Ansible manages, it can provide lists of individual hosts, lists of hosts, and some variables related to them.
YAML:
- YAML Ain't Markup Language for writing its playbooks, variables files, and configuration files. It's a human-readable data serialization language with very low syntax, making it easy to read and write.
Debug Module:
- The debug module allows for print statements during the playbook execution, it's most useful for printing variables—possibly facts—to troubleshoot and confirm data as part of the playbook runs.
Step-by-Step Process
Step 1: Launch EC2 Instance
- Go to AWS Console and login with credentials
- Navigate to EC2 Dashboard and launch EC2 Instance

Step 2: Install Ansible
- Now, Install Ansible in our local machine by using following command
sudo amazon-linux-extras install ansible2 -y

Step 3: Setting Up Your Environment in inventory file or Host file
- Create Inventory file or edit host file to execute our task in host server

Step 4: Create Simple Playbook to Print Facts
- Here is the example playbook to print facts
- name: Print Ansible Facts Example
hosts: all # replace with your host name
gather_facts: yes
tasks:
- name: Print the OS type
debug:
msg: "The operating system is {{ ansible_facts['os_family'] }}"
- name: Print the IP address
debug:
msg: "The primary IP address is {{ ansible_facts['default_ipv4']['address'] }}"

Step 5: Run Playbook
- Now run the playbook to print facts
ansible-playbook print_facts.yml
- In below figure we can see our playbook was executed and printed facts of the system

Conclusion
Ansible facts are powerful, granular details about the managed host that can drive dynamic, informed decision-making in your playbooks, this ability to collect and use these facts is what enables you to gain higher flexibility and efficiency in carrying out your automation tasks.
This article was about the key terms in Ansible facts, about collecting and printing them, and provided practical examples for their use. In all three cases—when you're debugging an issue, verifying configurations, or dynamically adapting your tasks based on the host environment—facts in Ansible can be called one of the key knowledge areas for each automation engineer.
Remember, the ability to effectively collect and print facts will greatly increase your workflow and make your playbooks more resilient and flexible across environments.