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.

Links

  • https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html

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:

``yaml

---

  • 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.