Ansible for Network Automation – Part 2

This blog is part of my series on Devops for Networking. In the previous blog, I covered basics of Ansible and how to get started with it. In this blog, I will cover a sample application that I wrote with Ansible. This Ansible application builds on UCS sdk utility that I covered in a previous blog. The UCS python utility displays the inventory of UCS system. I have made that utility as an Ansible module and extended the application to display the inventory of a list of UCS systems that are defined in the host list. This project is more to illustrate the usecase for Ansible.

The source code for the project can be found here. There are 3 files listed here:

getucs.yml - YAML file that defines the playbook
getucsinfo - New module that is defined. This file needs to be in "usr/share/ansible" - getucsinfo module uses functions in this library. This file needs to be in PYTHONPATH.The library provides utility functions to get UCS inventory.

To execute the playbook defined here, do:

ansible-playbook getucs.yml

Following is the output received:

PLAY [Get Existing UCS Information] ******************************************* 

TASK: [Pull generic data from UCS] ******************************************** 
ok: []
ok: []
ok: []
ok: []

PLAY RECAP ********************************************************************             : ok=1    changed=0    unreachable=0    failed=0             : ok=1    changed=0    unreachable=0    failed=0             : ok=1    changed=0    unreachable=0    failed=0             : ok=1    changed=0    unreachable=0    failed=0  
  •  In the playbook, “ucs” specifies the list of hosts. In this example, there are 4 hosts that are part of “ucs” variable is defined in “/etc/ansible/hosts”.
  • As part of script results, in the directory where the script is executed, there will be a file created with hostname for each UCS system which contains the inventory of that particular UCS system.
  • When more hosts are added, it is not necessary to change anything in the module, playbook or library. Only the hosts file needs to be changed.
  • This playbook is executed in the local system rather than in the remote system. The local system uses UCS sdk to communicate to the remote UCS system.

Ansible in combination with Jinja2 provides many usecases for Network automation and there is a lot more that can be explored in this area. The best part about Ansible is that it takes minimum time to get started with the tool and do something useful. I have not used Chef and Puppet, I have read few articles about it, it does not look to be as simple as Ansible.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s