Network Automation

Ansible is very powerful and yet simple tool which can be used to automate network.
Ansible modules support a wide range of vendors, network appliances and actions, which helps to manage your entire network with ease.
Ansible provides simple, powerful and agentless automation to automate network tasks such as device provisioning, data collection, and reporting.
Ansible network resource modules do not run on the managed hosts as these modules are written and executed in python and majority of network devices can not run python.


Install Ansible

There are many ways to install ansible. In this specific tutorial I will be using pip3 to install the ansible 2.9.9.

1. Install virtualenv package
This is optional step to setup an environment. If not required you can skip first three steps.
[fedora@ip-172-31-29-155 ~]$ sudo pip install virtualenv

2. Create the virtual environment
To create a virtual environment a path is required. i.e create one in the local directory called ‘ansible_env’. [fedora@ip-172-31-29-155 ~]$ virtualenv ansible_env

3. Activate virtual environment
To activate the virtual environment execute following command.
[fedora@ip-172-31-29-155 ~]$ source ansible_env/bin/activate

4. Install ansible with pip3
I would prefer python 3 as python 2.7 is deprecated now
(ansible_env)[fedora@ip-172-31-29-155 ~]$ sudo pip3 install ansible

5. Confirm ansible installed successfully
Execute the following command to confirm successfull installation
(ansible_env) [fedora@ip-172-31-29-155 ~]$ ansible --version


Inventory and Config

Inventory consist of managed hosts and also called as host file, you can specify information like IP address for each managed node and managed hosts could be grouped together inside inventory.
Config file can be created in order to override default settings in Ansible, in the [defaults] section of ansible.cfg path to inventory file could be specified.

1. Create ansible.cfg config file
Create a directory and move into that directory, create ansible.cfg file and add the following contents
[fedora@ip-172-31-29-155 ansi_work]$ vi ansible.cfg
[fedora@ip-172-31-29-155 ansi_work]$ cat ansible.cfg
[defaults] INVENTORY=./inventory


2. Create inventory file
Create ansible.cfg file and add the following contents
[fedora@ip-172-31-29-155 ansi_work]$ vi inventory
[fedora@ip-172-31-29-155 ansi_work]$ cat inventory

									#name of the group 
[vyos]
ec2-35-154-225-229.ap-south-1.compute.amazonaws.com
#www.example.com more host like these could be added
[vyos:vars]
#connection plugin used to connect to device
ansible_connection=network_cli
#OS used by appliance supported by Ansible
ansible_network_os=vyos
#user name
ansible_ssh_user=vyos
#in case password based authentication is required
#ansible_ssh_pass=yourpassword
#default port is 22
#ansible_ssh_port=22
ansible_ssh_private_key_file=./virtualkey.pem #path to key for key based authentication

3. Create a playbook with facts gathering task
Use the vyos l3_interfaces resource module to fetch configured interfaces ip address
[fedora@ip-172-31-29-155 ansi_work]$ vi gather_facts_l3_interfaces.yaml
[fedora@ip-172-31-29-155 ansi_work]$ cat gather_facts_l3_interfaces.yaml

									

---
- hosts: vyos
tasks:
- name: Gather the l3_interfaces from running configuration
vyos_facts:
gather_network_resources:
- l3_interfaces


4. Run playbook
Run ansible-playbook command and provide playbook name
[fedora@ip-172-31-29-155 ansi_work]$ansible-playbook gather_facts_l3_interfaces.yaml -vvv

Social