Using dynamic Ansible inventories to manage CloudVision switches.

Ansible Dynamic libraries with CloudVision 

The common question when talking with customers about CloudVision is are we able to also use a configuration management tool such as Ansible along with CloudVision?  You can use CVP and Ansible to both manage your Arista devices.  This is a guide to dynamically pull CloudVision for its devices and automatically have Ansible use those CVP managed devices.  Arista has supported Ansible EOS modules for quite some time and are still innovating on new modules in the latest version of Ansible which at the time of writing is 2.3.

 Summary

Dynamic inventories are setup in Ansible so that when running a playbook can pull a list of structured data from an external source.  This is really popular in a lot of cloud environments where Ansible has to pull a list of VM’s from a external API.  In our case we are going to have Ansible talk to CloudVision’s API and pull back a list of CloudVision managed switches so that any task in Ansible can perform any task against the list of switches.  In all cases of Ansible dynamic inventories, there is a script that takes all the devices and puts it in structured data in this case JSON.

Cloud vision verion used: 2016.1.2.3
Ansible version used: 2.2
Operating system used: Ubuntu 16.04 

Cloud vision API 

Please copy the following files from CVP to you’re Python library files before moving forward.

 /usr/lib/python2.7/site-packages/cvp.py 
/usr/lib/python2.7/site-
packages/cvpServices.py
/usr/lib/python2.7/site-
packages/cvpConfigParser.py
/usr/lib/python2.7/site-
packages/errorCodes.py

To you’re python library folders.  Typically it us /usr/lib/python2.7/ or /usr/lib/python2.7/site-packages/

Once all the modules are copied you should be able to import the CVP python library

 Python script

Lets take a look at the python script to make this possible.  Please change the name of the host which is the IP address of the CVP server and change the strings passed into server.authenticate which are the username and password for CVP. 

#!/usr/bin/env python

import cvp
import json

host = '192.168.0.202'
server = cvp.Cvp(host)
server.authenticate('arista','arista')

#This is a function to find all devices within cvp

def findalldevices():

    deviceList = []

    for device in server.getDevices():

        deviceName = device.fqdn #calls the cvp api for device.fqdn

        deviceList.append(deviceName) #appens to the list each item

        dynamicList = [str(i) for i in deviceList]

    return dynamicList

dynamic = findalldevices()

def dynamic_inventory():


    print  {

            'arista': {

                'hosts': dynamic,

                'vars': {

                    'ansible_connection': "local",

                    'username': 'arista',

                    'password': 'arista',

                    'transport': 'cli',

   'use_ssl': 'true',

                }

    }

     }

dynamic_inventory()

In the later example this file is executable please make this executable with the chmod +x cvpapi.py in order for Ansible to run the script. 

 CVP

Let’s take a look at the switches currently being managed by CloudVision.

It looks like we have 6 switches in total.  2 spine switches and 4 leaf switches.  Let’s run the script without Ansible in python and see the list of results.  The script should return JSON results for all 6 switches.

 python cvpdynamic_api.py {'arista': 
{'hosts': ['l1.arista.test', 'leaf2.arista.test', 'leaf3.arista.test', 'leaf4.arista.test', 'spine1.arista.test', 'spine2.arista.test'],
'vars':
{'username': 'arista',
'use_ssl': 'true',
'password': 'arista',
'ansible_connection': 'local',
'transport': 'cli'}
}
}

So the script will show the CVP switches in JSON so that Ansible can use it as a dynamic library.  This simply means that any switch that is added will automatically present itself through the script and not manually adding it through a static inventory file. 

 Implementation

We will wrap everything together by calling the dynamic script through ansible.  

Most who are used to using Ansible the commands would typically look like the following.

ansible-playbook -i inventoryfile playbook.yaml 

The script is simply substituted for the inventory file

ansible-playbook -i cvpdynamic_api.py playbook.yaml

Lets run a simple test using the ping module. 

 

Great looks like we are working through a simple Ansible ping module.  This can be applied the same way to any Ansible-playbook and with any Ansible EOS modules.