cancel
Showing results for 
Search instead for 
Did you mean: 
cancel

ACI Automation Part 6 - ACI Configuration with Python and Arya

4003
Views
5
Helpful
0
Comments

 

 

I. Introduction

 

In the Previous articles of ACI Automation, we are using Postman/Newman as the Rest API tool to automate the ACI Configuration.

 

In this article I’m going to discuss on using Python programming language to automate. The benefit of using Python programming as the Rest API tool is when there is logic or condition that you need to apply while perform the ACI configuration. Python can do the same provisioning capability as Postman/Newman.

 

First, we’re going to discuss on how to setup Python and Arya. After that, we going to configure simple Tenant in ACI.

 

1. Verify Python Installed

 

I would assume that you already have Python installed. 

 

To verify that you have python installed:

# python -V

 

2. Download the acicobra and acimodel

 

Acicobra – This is the SDK

Acimodel – This includes the Python packages that model the ACI Management Information Tree (MIT)

 

To download this, open your APIC and add the following path

 

Example the APIC IP Address is 10.10.10.10 (change this to your APIC IP address):

http://10.10.10.10/cobra/_downloads/

 

You’ll find two .egg files that you can download.

acicobra-version-py2.7.egg

acimodel-version-py2.7.egg

 

Note: You will require to download the same .egg version as the ACI in order for the cobra SDK to work.

 

Please refer to your APIC URL for further information on Installing Cisco APIC Python SDK:

https://10.10.10.10/cobra/install.html

 

Alternatively, to get into this URL, you can click on ACI GUI > Settings > Documentation > Python SDK Documentation > Installing the Cisco APIC Python SDK

 

3. Install the acicobra and acimodel

 

acicobra

# easy_install -Z /path_to/acicobra-version-py2.7.egg

 

acimodel

# easy_install -Z /path_to/acimodel-version-py2.7.egg

 

 

4. Verify the installation of acicobra and acimodel

 

# pip show acimodel

 

Note: In future, if you upgraded the ACI to newer version, you would need to uninstall and re-install the matching version of acicobra and acimodel.  

 

 

III. Setting Up Arya

 

Arya is a tool that will convert ACI APIC object from XML or JSON format into Python code.

 

This is handy as this provides you a fast way to get your ACI object ready for your python code.

 

1. Arya Installation

 

# pip install arya

 

2. Verify Arya

 

# which arya

/usr/local/bin/arya

 

Please refer to the following URL on Arya:

https://github.com/datacenter/arya

 

 

IV. Provisioning ACI Tenant using Python and Arya

 

 

1. Create a Sample Tenant from APIC

 

This sample tenant will be the template for creating the tenant object.

 

After creating the sample tenant we can save this to the XML format.

 

1.png

 

2.png


 

2. Using Arya To Generate the Python File

 

-i  this is to enter the IP address of APIC

-u this is the APIC username

-p this is the APIC password

-t  this is the target directory

> this is to output to the file

 

# arya -f tn-rsantoso-test.xml -i 10.10.10.10 -u your_username -p your_password -t . > tn-rsantoso-test.py

 

 

3. Modify the Generated Python File

 

The generated Python file would contain the following at the beginning of the file:

 

'''

Autogenerated code using arya

Original Object Document Input:

<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><fvTenant descr="" dn="uni/tn-rsantoso-test" name="rsantoso-test" nameAlias="" ownerKey="" ownerTag=""><vnsSvcCont/><fvRsTenantMonPol tnMonEPGPolName=""/></fvTenant></imdata>

'''

raise RuntimeError('Please review the auto generated code before ' +

                    'executing the output. Some placeholders will ' +

                    'need to be changed')

 

This is not required, thus you can remove this. The aim for this code generated is that the author wants you to check the file before running it.

 

Also, near the bottom of the file you’ll find this line:

 

print toXMLStr(polUni)

 

You can commented this line.

 

4. Final Python File

 

#!/usr/bin/env python

 

# list of packages that should be imported for this code to work

import cobra.mit.access

import cobra.mit.request

import cobra.mit.session

import cobra.model.fv

import cobra.model.pol

import cobra.model.vns

from cobra.internal.codec.xmlcodec import toXMLStr

 

# log into an APIC and create a directory object

ls = cobra.mit.session.LoginSession('https://10.10.10.10', 'your_username', 'your_password')

md = cobra.mit.access.MoDirectory(ls)

md.login()

 

# the top level object on which operations will be made

polUni = cobra.model.pol.Uni('')

 

# build the request using cobra syntax

fvTenant = cobra.model.fv.Tenant(polUni, ownerKey='', name='rsantoso-test2', descr='', nameAlias='', ownerTag='')

vnsSvcCont = cobra.model.vns.SvcCont(fvTenant)

fvRsTenantMonPol = cobra.model.fv.RsTenantMonPol(fvTenant, tnMonEPGPolName='')

 

 

# commit the generated code to APIC

# print toXMLStr(polUni)

c = cobra.mit.request.ConfigRequest()

c.addMo(polUni)

md.commit(c)

 

5. Running the python file would generate you the Tenant

 

Note: in point 4 above, you can change the name of the tenant accordingly.

I have change this to tenant ‘rsantoso-test2’

 

# python tn-rsantoso-test.py

 

Tips: If you’re hitting the below error:

('Connection aborted.', error(54, 'Connection reset by peer'))

This is likely that you’re having issue with python openssl for https.

 

Workaround:

 

The workaround is to change this line in the point 4 python code with http:

 

ls = cobra.mit.session.LoginSession('https://10.10.10.10', 'your_username', 'your_password')

 

into

 

ls = cobra.mit.session.LoginSession('http://10.10.10.10', 'your_username', 'your_password')

 

You need to enable the http in your APIC GUI:

Fabric > Fabric Policies > Pod Policies > Policies > Management Access > default > Under HTTP > Under Admin State: Enabled

 

Screen Shot 2018-02-04 at 12.21.02 am.png

 

 

Fix:  

 

To install the pyopenssl. Please refer to the following url to install pyopenssl:

https://10.10.10.10/cobra/install.html#installing-pyopenssl

Change the IP address here to your APIC IP address.

 

Addionally, I was hitting the HTTPS issue and the below pip installation has help on getting the https working.

However different environment could be different.

 

# pip install pyopenssl ndg-httpsclient pyasn1

 

6. Result

 

Screen Shot 2018-02-04 at 12.00.33 am.png

 

The rsantoso-test is the tenant that was created manually from GUI

The rsantoso-test2 is the tenant that is created from Python script.

 

 

 

Content for Community-Ad