• Netomate
  • Posts
  • Ansible Mastery Part 2:Install Ansible & Run Your First Ping

Ansible Mastery Part 2:Install Ansible & Run Your First Ping

Ansible Mastery Journey

Let’ s continue with our Ansible journey. Don’t miss to go through Ansible Post 1 .

In this post, i will go through how to :

  • Set up ansible in system

  • Set up lab in GNS/EVE/CML

  • Create a basic inventory

  • Run first ping command using ansible to test connectivity to network devices

Lets dive in.

Install Ansible

To get start, we will get ansible install in our system

sudo apt update
sudo apt install ansible

Once installed, confirm it by running: ansible --version

[anurudh@localhost ~]$ ansible --version
ansible [core 2.15.13]
  config file = None
  configured module search path = ['/home/anurudh/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
  ansible collection location = /home/anurudh/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.9.21 (main, Feb 10 2025, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-5)] (/usr/bin/python3)
  jinja version = 3.1.6
  libyaml = True
[anurudh@localhost ~]$ 

You should see Ansible's version and installation path.

It may be different command for you to install Ansible if you are using any other operating system apart from Linux(Ubuntu)

Refer below link for details

lets now create and go through different files to be used in ansible. Its very crucial to understand each file before actually going through ansible tasks.

Ansible Configuration (ansible.cfg)

Hope you remember the above structure from Ansible post 1.Below is the tree view of my directory .

ansible.cfg → Its ansible config file It will be applicable to current directory .It includes various settings including which inventory to use and the transport method for connecting to devices. Our ansible file content is as below:-

[anurudh@localhost network_automation_ansible]$ cat ansible.cfg 

[defaults]
inventory = hosts.ini
transport = network_cli
#host_key_checking = False  # Disables SSH host key checking
[anurudh@localhost network_automation_ansible]$ 
  • Explanation:

    • inventory: Points to your hosts.ini file.

    • transport: Specifies that Ansible will use network_cli to connect to network devices (like Cisco).

    • host_key_checking = False: Disables SSH key verification, which is useful if you're not using trusted SSH keys for the devices.

Inventory File (hosts.ini)

In Ansible, the inventory file lists the devices you want to manage. Here’s an example of an inventory file (hosts.ini) that connects to Cisco routers using network_cli.

[anurudh@localhost network_automation_ansible]$ cat hosts.ini 

[routers]
#1.1.1.1
#2.2.2.2
R1 ansible_host=1.1.1.1
R2 ansible_host=2.2.2.2

[ios_devices:children]
routers

[ios_devices:vars]
ansible_user=cisco
ansible_password=cisco
ansible_connection=network_cli
ansible_network_os=ios
[anurudh@localhost network_automation_ansible]$ 
  • Explanation:

    • [routers]: Defines the group of routers.

    • [ios_devices:children]: Groups devices under ios_devices which are listed as children of routers.

    • [ios_devices:vars]: Specifies global variables for all devices in the ios_devices group.

Test Connectivity Using the Ping Module

The first test you should run is the ping module to check if your Ansible is correctly communicating with the network devices.

We have got our Master node where we have install ansible and running our ping tasks using ping module for Node R1 and R2.

Make sure SSH reachability is enabled from master node to various router switches and all respective nodes. As discussed in post 1 that ansible is agent less and there is no need to install any agent(software ) on nodes. Ansible uses SSH to access nodes and perform various playbook tasks.

[anurudh@localhost network_automation_ansible]$ cat 01_ping_router_local.yml 
---
- name: Simple Ping Test
  hosts: localhost
  gather_facts: no

  tasks:
    - name: Ping localhost
      ansible.builtin.ping:
[anurudh@localhost network_automation_ansible]$ 

The first test you should run is the ping module to check if your Ansible is correctly communicating with the network devices.

Run the Ping Command:

[anurudh@localhost network_automation_ansible]$ ansible ios_devices -i hosts.ini -m ping
  • Explanation:

    • ios_devices: The group you defined in your hosts.ini file.

    • i hosts.ini: Specifies the inventory file.

    • m ping: Uses Ansible’s ping module to test connectivity.

  • Output

    If successful, you should see a pong response from each device:

[anurudh@localhost network_automation_ansible]$ ansible ios_devices -i hosts.ini -m ping
R1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
R2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
[anurudh@localhost network_automation_ansible]$ 

We have not yet created our first playbook .We will perform same task using playbook in next post.

Troubleshooting Tips

  • If the ping command doesn't work:

    • Check if SSH is accessible from your local machine to the routers.

    • Ensure the ansible_user and ansible_password are correct for your devices.

  • Make sure that the network_cli connection method is supported by your device and its OS.

    📌 Action Step for You Today

    Just one - Try out the steps and share experiences or questions in the comments.

     

    I’ll guide you, one simple post at a time.

We will discuss about execution of our first playbook in next post .

Smiles:

Anurudh


Reply

or to participate.