Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using Facts in Ansible

Introduction

Ansible facts are pieces of information derived from speaking to your remote systems. They are automatically gathered by Ansible when it connects to the remote systems and can be used to make your playbooks more dynamic and adaptable. In this tutorial, we will cover how to use these facts effectively in your Ansible playbooks.

Gathering Facts

By default, Ansible gathers facts at the beginning of each play. This behavior can be controlled using the gather_facts directive in your playbook. Here is an example:

- hosts: all
  gather_facts: yes

  tasks:
    - name: Print all facts
      debug:
        var: ansible_facts
                

In the above example, the gather_facts: yes directive ensures that facts are gathered from all hosts before executing tasks. The debug module is used to print all gathered facts.

Accessing Facts

Once gathered, facts can be accessed using the ansible_facts dictionary. Here is an example of accessing a specific fact:

- hosts: all
  gather_facts: yes

  tasks:
    - name: Print the IP address of the host
      debug:
        msg: "The IP address is {{ ansible_facts['default_ipv4']['address'] }}"
                

In the above example, the debug module is used to print the IP address of the host by accessing the default_ipv4.address fact.

Using Facts in Tasks

Facts can be used within tasks to make decisions or configure services dynamically. Here is an example of using facts to install a package based on the operating system:

- hosts: all
  gather_facts: yes

  tasks:
    - name: Install the appropriate package
      package:
        name: "{{ 'httpd' if ansible_facts['os_family'] == 'RedHat' else 'apache2' }}"
        state: present
                

In the above example, the package module installs httpd if the operating system family is RedHat, otherwise, it installs apache2.

Custom Facts

In addition to the built-in facts, you can also define custom facts. Custom facts can be placed in /etc/ansible/facts.d on the remote hosts and can be written in JSON, INI, or executable scripts that return JSON. Here is an example of a custom fact written in JSON:

{
  "custom_fact": "This is a custom fact"
}
                

Save the above JSON content in a file named /etc/ansible/facts.d/custom.fact. You can then access this custom fact in your playbook as follows:

- hosts: all
  gather_facts: yes

  tasks:
    - name: Print custom fact
      debug:
        msg: "The custom fact is {{ ansible_facts['custom_fact'] }}"
                

Conclusion

Using facts in Ansible allows you to create more dynamic and flexible playbooks by utilizing information about your remote systems. By gathering, accessing, and using both built-in and custom facts, you can tailor your configurations and actions to the specific needs of each host.

We hope this tutorial has provided a comprehensive introduction to using facts in Ansible. Happy automating!