02-03-2018 05:42 AM - edited 03-01-2019 06:07 AM
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.
I would assume that you already have Python installed.
To verify that you have python installed:
# python -V
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
acicobra
# easy_install -Z /path_to/acicobra-version-py2.7.egg
acimodel
# easy_install -Z /path_to/acimodel-version-py2.7.egg
# 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.
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.
# pip install arya
# which arya
/usr/local/bin/arya
Please refer to the following URL on Arya:
https://github.com/datacenter/arya
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.
-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
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.
#!/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)
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
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
The rsantoso-test is the tenant that was created manually from GUI
The rsantoso-test2 is the tenant that is created from Python script.
Thanks for this explanation. It is usefull.
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: