Introduction

In the realm of IT automation, Ansible stands out as a powerful tool for configuring and managing systems with simplicity and efficiency. Ansible employs a declarative approach to automation, allowing administrators and engineers to define the desired state of their infrastructure without diving into complex scripting languages. In this article, we will dive into a specific Ansible playbook that showcases how to gather and display network information using Ansible’s intuitive playbook syntax.

Join 50+ hours of courses in our exclusive community

Step by step

The following Ansible playbook snippet Playbooknstrates how to gather and display network information from a set of target hosts. This is an example setup.yml Playbook that displays the IPv4 addresses of the machines:

---
- name: Display network information
  hosts: all
  tasks:
    - name: Update facts
      ansible.builtin.setup:
        gather_subset: all_ipv4_addresses
      register: machine_facts
    - name: Display facts
      ansible.builtin.debug:
        var: machine_facts.ansible_facts.ansible_all_ipv4_addresses

The provided code snippet is written in YAML and represents an Ansible playbook. Ansible is an open-source automation tool used for configuring and managing systems, deploying applications, and performing various IT tasks through a declarative approach. Let’s break down the code step by step:

  1. ---: This is a YAML document delimiter that indicates the start of a new YAML document.
  2. name: Display network information: This is a playbook task with a name describing what the task will do. In this case, it’s intended to display network information.
  3. hosts: all: This specifies the target hosts on which the playbook tasks will be executed. In this case, the tasks will be applied to all hosts defined in the Ansible inventory.
  4. tasks:: This keyword indicates the start of a list of tasks that will be executed sequentially on the target hosts.
  5. name: Update facts: This is the first task in the list. The name provides a description of the task. This task is designed to gather facts from the target hosts, which are essentially system-related information and variables.
  6. ansible.builtin.setup:: This is an Ansible module that collects various facts from the remote hosts. Facts include information about the hardware, operating system, networking, and more.
  7. gather_subset: all_ipv4_addresses: This specifies that the Ansible facts to be collected are related to IPv4 addresses. It will gather information about all IPv4 addresses configured on the target hosts.
  8. register: machine_facts: This is used to store the output of the ansible.builtin.setup module in a variable named machine_facts. This variable can then be used later in the playbook.
  9. name: Display facts: This is the second task in the list. It’s meant to display the facts that were collected in the previous task.
  10. ansible.builtin.debug:: This Ansible module is used to print debug information.
  11. var: machine_facts.ansible_facts.ansible_all_ipv4_addresses: This specifies the variable whose value should be printed using the ansible.builtin.debug module. It references the variable machine_facts that was registered in the previous task and extracts the IPv4 addresses from it.

In summary, this Ansible playbook performs the following steps:

  1. It collects IPv4 address-related facts from all hosts.
  2. It stores these facts in a variable called machine_facts.
  3. It then displays the collected IPv4 addresses from the facts using the ansible.builtin.debug module.

Execution

ansible-playbook -i inventory setup.yml

Output

PLAY [Display network information] ******************************************************

TASK [Gathering Facts] ******************************************************************
[WARNING]: Platform darwin on host localhost is using the discovered Python interpreter
at /opt/homebrew/bin/python3.11, but future installation of another Python interpreter
could change the meaning of that path. See https://docs.ansible.com/ansible-
core/2.15/reference_appendices/interpreter_discovery.html for more information.
ok: [localhost]

TASK [Update facts] *********************************************************************
ok: [localhost]

TASK [Display facts] ********************************************************************
ok: [localhost] => {
    "machine_facts.ansible_facts.ansible_all_ipv4_addresses": [
        "10.87.107.165",
        "192.168.64.1",
        "192.168.246.1"
    ]
}

PLAY RECAP ******************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Conclusion

The provided Ansible playbook exemplifies the elegance and efficiency of Ansible automation in action. Administrators and engineers can effortlessly gather and display network information across multiple hosts by leveraging a concise and human-readable syntax. Ansible’s modular approach and its extensive set of built-in modules empower IT professionals to streamline tasks and easily maintain infrastructure.

Incorporating Ansible into your automation toolkit can significantly enhance your efficiency and scalability, freeing you from repetitive manual tasks and allowing you to focus on higher-value activities. As you explore Ansible further, you’ll discover its potential for tackling various automation challenges across diverse IT environments.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza