Ansible Network Automation: Architecture & Playbook example
Ibrahim ElSawy
Telco Cloud Operation | IP Backbone Planning | Master of Engineering Telecom Networks | Red hat OpenShift Administration | Ansible Automation | CCNP Service Provider
I liked how ansible is being designed in a very simple architecture to operate with any network element without adding complexity in addition to its ability to perform idempotent execution, meaning that running the same playbook multiple times results in a consistent state. This allows for safe and predictable automation.
Ansible is composed of several key components that work together to automate IT infrastructure tasks.
Here are the main components of Ansible:
Control Node
The control node is responsible for hosting the ansible platform from which the network admin runs Ansible CLI tools (ansible, ansible-playbook, ansible-vault) and others. Ansible can be installed on top of any any machine meeting the required system capabilities operating system (e.g. windows, Linux, ..).
Inventory
A list of managed nodes. Inventory support in providing details of managed nodes like hostname, IP address, network connection (e.g. ssh), managed node operating system (ios, iosxr, nexus, junos, esxi, ..).
Inventory enable grouping similar hosts having the same attributes with several benefits
Inventory in .ini format
Hosts are listed under [group-name], and the corresponding variables are listed under [group-name:vars]
# group managed iosxr nodes
[core-routers-cisco]
RT_CO_01 ansible_ssh_host=10.1.1.1
RT_CO_02 ansible_ssh_host=10.1.1.2
# list variables of managed group core-routers-cisco, mentioning its variables
[core-routers-cisco:vars]
ansible_user=admin
ansible_password=admin123
ansible_connection=network_cli
ansible_network_os=iosxr
ansible_port=22
Inventory in .yml format
Hosts and corresponding variables are grouped in a simple format identifying the hostname, IP address, username, password, connection type, remote host OS and connection port number
---
# group of iosxr managed nodes, name core-routers-cisco, hosts and variables
core-routers-cisco
?hosts:
? RT_CO_01: 10.1.1.1
? RT_CO_02: 10.1.1.2
?vars:
? ansible_user: admin
? ansible_password: admin123
? ansible_connection: network_cli
? ansible_network_os: iosxr
? ansible_port: 22
Playbooks
Playbooks is the basic unit of Ansible platform, They contain Plays. Playbooks are written in YAML and are easy to read, write, share and understand.
An Ansible playbook defines a set of tasks to be executed on one or more hosts. Here are the common components of an Ansible playbook:
Below is a playbook is written in yaml format to extract the clock and uptime of remote iosxr routers listed in hosts file under group core-routers-cisco.
---
- name: backup config and look at device health indicators on iosxr devices
? hosts: core-routers-cisco
? gather_facts: no
? tasks:
? ?- name: run show commands on remote devices
? ? ?iosxr_command:
? ? ? commands:
? ? ? ?- show clock
? ? ? ?- show version | include uptime
? ? ?register: show_output
? ?- debug:
? ? ? msg: "{{ show_output.stdout }}"
Command to run playbook cisco-rtr-check01.yml
领英推荐
[root@black_diamond ~]# ansible-playbook /etc/ansible/playbooks/cisco-rtr-check01.yml
Playbook output
playbook runs two tasks
Task#1: run show commands on remote devices
This task will run and execute mentioned commands on the remote without logging the printing the output. this will be useless if we are gathering information from remote hots. printing the output will be achieved by saving the gathered information into variable show_output mentioned in line 11 of the playbook and printing this variable by the second task
Task#2: debug
In Ansible, the debug module is a powerful tool that allows you to print information and variables during playbook execution. It is used to display messages or the values of variables for debugging purposes. The debug module can be helpful in understanding the state of the system, troubleshooting issues, and verifying the values of variables at different stages of playbook execution.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [backup config and look at device health indicators on iosxr devices] ************************************************************
TASK [run show commands on remote devices] ********************************************************************************************
ok: [RT_CO_01]
ok: [RT_CO_02]
TASK [debug] **************************************************************************************************************************
ok: [RT_CO_01] => {
? ? "msg": [
? ? ? ? "22:59:23.845 EGY Fri Aug 4 2023",
? ? ? ? "R1RT_CO_01 uptime is 1 year, 22 weeks, 1 day, 18 hours, 7 minutes"
? ? ]
}
ok: [RT_CO_02] => {
? ? "msg": [
? ? ? ? "22:59:23.784 EGY Fri Aug 4 2023",
? ? ? ? "R1RT_CO_02 uptime is 1 year, 22 weeks, 17 hours, 27 minutes"
? ? ]
}
PLAY RECAP ****************************************************************************************************************************
RT_CO_01? ? ? ? ? ? ? ? ?: ok=2? ? changed=0? ? unreachable=0? ? failed=0? ? skipped=0? ? rescued=0? ? ignored=0
RT_CO_02? ? ? ? ? ? ? ? ?: ok=2? ? changed=0? ? unreachable=0? ? failed=0? ? skipped=0? ? rescued=0? ? ignored=0
Plugins
Plugins are pieces of code that augment Ansible’s functionality. Ansible uses a plugin to enable a rich, flexible and expandable feature set.
Collections
A format in which Ansible content is distributed that can contain playbooks, roles, modules, and plugins. You can install and use collections through?Ansible Galaxy.
Cisco IOSXR Collection is an example which includes a variety of Ansible content to help automate the management of Cisco IOSXR network appliances. it can be installed using ansible-galaxy command
ansible-galaxy collection install cisco.iosxr
Sample of module included in Cisco IOSXR Collection
Please share with us in the comments the use cases you have used or developed with Ansible to automate your IT infrastructure and how beneficial they have been.
References