LABACI 1011 Python Locked
LABACI 1011 Python Locked
Python
LABACI-1011
Prerequisites
Understanding of the ACI Policy Model
No programming experience needed
Its strongly recommended to go through LABACI-1001. This will give you a good foundation
of configuring ACI through the GUI so that you can see how powerful using Python can be
for configuring ACI.
1|Page
The ACI Policy Model and Logical constructs
The policy model manages the entire fabric, including the infrastructure, authentication, security,
services, applications, and diagnostics.
Infra – Tenant that covers the infrastructure for the ACI Fabric
Context (fvCtx): A context is a unique Layer 3 forwarding and application policy domain.
Bridge Domain(fvBD): represents a Layer 2 forwarding construct within the fabric. A bridge domain
must be linked to a context and have at least one subnet (fvSubnet) that is associated with it. The
bridge domain defines the unique Layer 2 MAC address space and a Layer 2 flood domain if such
flooding is enabled.
Application Profile(fvAp): is a convenient logical container for grouping EPGs that models application
requirements.
EPG: A managed object that is a named logical entity that contains a collection of endpoints.
Endpoints are devices that are connected to the network directly or indirectly. They have an address
(identity), a location, attributes (such as version or patch level), and can be physical or virtual.
Knowing the address of an endpoint also enables access to all its other identity details. EPGs are fully
decoupled from the physical and logical topology.
2|Page
Overview of the Cobra Software Development Kit (SDK)
The ACI Cobra SDK is a Python implementation of the API that provides native bindings for all the
REST functions in the APIC GUI. Cobra also has a complete copy of the object model so that data
integrity can be ensured, and provides methods for performing lookups and queries and object
creation, modification and deletion. As a result, policy created in the GUI can be used as a
programming template for rapid development.
There are several modules that you can use to implement python programs to configure and
manage an APIC. This lab covers some examples including building a full three tier application.
This lab leverages dCloud, which uses a virtual APIC and leaf/spine switches that runs on a simulator.
Those these have most of the functions of actual hardware based devices, there is no data plane
traffic.
We need to VPN into the dCloud environment in order to access devices to complete this lab.
Click on the Cisco AnyConnect client and check to see if the client is connected to another VPN
session. Go ahead and disconnect.
https://dcloud-sjc-anyconnect.cisco.com
For the credentials, input the username and password that is provided on the back of the card you
were given.
Once we are connected, we should be able to access the environment. Now we need to open
Remote desktop to get access to the environment.
3|Page
Then add the password on the next screen – C1sco12345
Now we are logged into our dCloud session. Click on Desktop and you will be presented with the
Windows desktop. For our lab, we will need to log into our environment as well the APIC. Let’s log
into the APIC first.
You should now see the APIC login come up in our Chrome browser.
4|Page
Go ahead and log in using the following credentials:
Password: C1sco12345
Mode: Advanced
Click on LOGIN. This will take you to the APIC GUI which we will use for monitoring the things we do
in this lab.
You may get a message regarding Warning messages. Just click on "NO". You are now in the system
health dashboard. Now we are ready to do our lab.
Now that we are logged into the dCloud environment, we can create our first ACI Python program.
Before we do that, we will need to install the SDK environment. This has been pre-loaded for you on
a linux server. This task will cover connectivity as well as the installation of the Cobra SDK.
1 – Select on the Putty desktop icon to open Putty. Putty is what we will use to log in to our
Linux host.
2 – Scroll through the saved sessions and select "tools" and click open.
5|Page
When prompted, enter user01/user01 or the username and password. Once you log in, you will
want to create the aci directory and “cd” to it.
3 – We need to install the Cobra egg files have already been uploaded to the server you are logged
into. Egg files are a way to bundle information needed for a Python project. These files are located in
the Demo_Scripts directory. We need to copy them to the newly created aci directory.
You should see the newly installed Egg files in the aci directory.
4 – Virtual environments allows us to run isolated Python environments. In ACI, this allows us to
have multiple virtual environments that correspond to the version of Python or the version of the
Cobra SDK that we are testing and developing on. This will help us if we want to test different
versions of the Cobra Egg files for different versions of ACI.
To create the virtual environment that we will work in, we will use the command “virtualenv” from
the command line in the ~/aci directory. Verify that we are in the aci directory beforehand
6|Page
Now To enable this virtual environment, we now use the source command to activate the virtual
environment:
We should now see our prompt change and we should see an “(aci)” at the beginning of our prompt.
5 – Now we can install the Cobra SDK environment and the Cobra egg files. We will need to use the
“easy_install” command to install the egg files.
To verify that the Cobra SDK was installed properly, we can run the following command:
Our development environment has been completed. We are ready to run our Python Scripts to
program the APIC.
Let’s look at our first python script, which just logs us into the APIC. To create this file, you can use a
number of text editors that are available to us on Linux. If you’re not familiar with any of them, we
can use a text editor called nano. If you are familiar with vim or emacs, please feel free to use those
as well.
To create our first script, we will call nano from the command line in Linux under the newly created
aci directory:
Once in the file, we will input the following text as we would in any text editor:
7|Page
#Disable unverfied HTTPS request messages
requests.packages.urllib3.disable_warnings()
Once we have completed adding this text, we will then need to save the file. To save it, we will hit
CTRL (^O) o (WriteOut). Once it gives you the file name, hit enter.
[Output]
File Name to Write: login.py
To exit from nano, hit CTRL and “x”. We are now back in our command prompt.
This script just logs us into the APIC similar to what you have already done through
the GUI. The script does not do anything other than log us in, which is why there is a
print statement at the end. Let's go ahead and run it:
If we received no errors, we know our script has run successfully without issues.
Now that we have successfully logged into the APIC, we can implement constructs in ACI. In this
example, we’re going to create a new Tenant. Remember from before, a Tenant is a logical container
for application policies that enable an administrator to exercise domain-based access control. Let’s
create a new script that will create a Tenant called CiscoLive.
1 – Create a new Python script called add_tenant.py using nano (or vim or emacs):
8|Page
import requests.packages.urllib3
url = 'https://apic1.dcloud.cisco.com'
apicUser = 'admin'
apicPassword = 'C1sco12345'
new_tenant = 'CiscoLive'
policy_universe = md.lookupByDn('uni')
Remember, to save the file in nano, you have to hit CRTL “o”. Hit CTRL ‘x’ to exit nano.
Now, before we run this script, let’s log into the APIC to see what the existing Tenants look like.
2 – Now we can look at the APIC to see the new Tenant created. Let’s log into the GUI. On the
windows desktop, open the Chrome web browser icon at the bottom at the screen:
https://apic1.dcloud.cisco.com/
9|Page
Username: admin
Password: C1sco12345
This will log you into the APIC and bring you to the System dashboard.
While logged into the APIC GUI, click on "Tenants". This will list out all the current Tenants. These
were the defaults that were loaded before our lab.
3 – Now, go back to our linux host and run the script we created – add_tenant.py
At the top of the GUI screen in the browser, select the Tenants tab. Under the list of Tenants, you
should now see the new ‘CiscoLive’ tenant.
It reports that the new Tenant was completed and if we check the GUI, we will see that
the new Tenant ‘CiscoLive’ has indeed been added:
Now let’s try building a 3 tier application with Python and the Cobra SDK.
Logging in and building simple structures such as tenants are easy in the GUI. But where
the real power of ACI is in this next task.
If you have taken LABACI-1001, you saw how to build a 3-Tiered Application using the
APIC GUI. We can do the same thing with one single Python script. Let’s look at some
requirements for our app:
10 | P a g e
b. We going to create a new Context/VRF called VRF1
c. We're going to create a new Bridge Domain called BD1
a. Within this BD we will create the following subnets which will have a scope of
private.
i. 10.1.1.1/24
ii. 10.2.2.1/24
iii. 10.3.3.1/24
d. The script will then configure the following End Point Groups (EPGs)
a. Client, Web, App, and DB
We can now enter the following script which will build our new Application environment. Its over
200 lines of code, so if you want to copy and paste, open the three_tier.txt file on the desktop:
"""
Create a full 3 Tier Application complete with contracts and filters
"""
# From Cobra, import the Managed Object Directory, Login capabilities, and Configuration
from cobra.mit.access import MoDirectory
from cobra.mit.session import LoginSession
from cobra.mit.request import ConfigRequest
# From Cobra, import Tenant, Application Profile, Contexts, and other ACI constructs
from cobra.model.fv import Tenant, Ap, Ctx, BD, RsProv, RsCtx, Subnet, AEPg, RsBd, RsCons
# From Cobra, import Filters, Subjects,
from cobra.model.vz import Filter, Entry, BrCP, Subj, RsSubjFiltAtt
11 | P a g e
print 'Login error'
exit(1)
return md
pass
12 | P a g e
configReq.addMo(vz_entry_db)
pass
configReq = ConfigRequest()
13 | P a g e
# Create the DB EPG
print 'Creating DB EPG...'
fv_aepg_db = AEPg(fv_ap, 'DB')
fv_rs_bd_db = RsBd(fv_aepg_db, tnFvBDName='BD1')
configReq.addMo(fv_aepg_db)
configReq.addMo(fv_rs_bd_db)
policy_universe = md.lookupByDn('uni')
fv_tenant = Tenant(policy_universe, tenant)
configReq = ConfigRequest()
14 | P a g e
vz_rs_subj_filt_att_web = RsSubjFiltAtt(vz_subj_web, 'http')
configReq.addMo(vz_ct_web)
configReq.addMo(vz_subj_web)
configReq.addMo(vz_rs_subj_filt_att_web)
pass
Once again, save the file (CTRL and “o”) and exit nano (CTRL and “x”). Now we can run our script.
From the command line, you can run the following:
15 | P a g e
Creating DB EPG...
Creating Client EPG...
Client EPG consumes WebContract
Web EPG provides WebContract and Consumes AppContract
App EPG provides App Contract and consumes DB Contract
DB EPG provides DB Contract to App
3 Tier App Created!
(aci) [user01@rhel7-tools aci]$
So now our new Tenant has been created, let’s look at the APIC GUI. Again, under "Tenants"
we can see the new Tenant was created. (note you may have to refresh the screen)
Now let’s look at the other ACI objects to ensure that they were created. Double click on the
'WISP' tenant. This will bring you into the WISP Tenant.
Click on "Application Profiles'. you should see the new Application Profile that the script
created called 3-TierApp. Click on arrow next to '3-TierApp' and also on 'Application EPGs'
and you should now see the newly created EPGs
16 | P a g e
Now if we click directly on 3-Tier App, we can see the following in the right hand
side of the GUI interface
You can move each EPG and Contract to make it a little clearer on the screen. In this case, I moved
AppCtrct and DbCtrct.
You can now click on any of the contracts in the window and it should show you what is
consuming and what is providing. For example, if we click on the App Contract (AppCtrct)
we see
17 | P a g e
In this case, it matches the original requirements, which was that the App EPG would provide
the App Contract and the Web EPG would consume it. Let’s look at the other constructs
that we built.
Click on "Networking", Bridge Domains, BD1, and Subnets. You can see that we have built our
Bridge domain and the associated subnets.
We can now also check the VRFs that we created by clicking on "VRFs"
Finally, we can look at the contracts and associated filters. To do this, we click on
"Security Policies" and "Contracts". Also, expand "Filters" as well.
18 | P a g e
We've completed and verified the building of our 3 Tier App. With the script that we used,
we could change a few variables such as ‘tenant’, ‘VRF’, BD, and others to create a different 3 Tier
app in a different Tenant. We could also automate this process so that the script can ask the user
what it wants to input. This saves a lot of time and effort over building this through the GUI.
There is a tool that you can use where you can convert JSON and XML files to
python code using the Cobra SDK. That tool is called ARYA - APIC REST pYthon Adapter -
which will take JSON and XML and convert it to Python code that can be used with the
Cobra SDK.
We will need to install the Arya tool before we can use it. Let's use pip to install the
tool:
Once again, we can run the "pip freeze" command and it should show arya as installed
Now let's create a new file and copy the following into it:
19 | P a g e
{"fvTenant":{"attributes":{"dn":"uni/tn-ARYA","name":"ARYA-JSON","rn":"tn-ARYA-
JSON","status":"created"},"children":[]}}
Don't forget to save this to the file. To save it, we will hit CTRL (^O) o (WriteOut).
Once it gives you the file name, hit enter to save and exit.
Now we are ready to convert this from JSON to python code using the ARYA tool. Now you can run
the following command to generate python code required. We have two ways to do this. We can
copy the output from the command to a new file or we can redirect the output to a new file name. I
use the redirect output option below:
So now we can see the code that has been generated. We will need to make some changes in order
to make this code useable. Let's look at the code that was generated:
#!/usr/bin/env python
'''
Autogenerated code using arya
Original Object Document Input:
{"fvTenant":{"attributes":{"dn":"uni/tn-ARYA","name":"ARYA-JSON","rn":"tn-ARYA-
JSON","status":"created"},"children":[]}}
'''
raise RuntimeError('Please review the auto generated code before ' +
'executing the output. Some placeholders will ' +
'need to be changed')
20 | P a g e
md.commit(c)
Now that we have the code generated, we will need to change the line for logins. Specifically
c.addMo(topMo)
To save it, we will hit CTRL (^O) o (WriteOut). Once it gives you the file name, hit enter to save and
exit.
If we now look in the APIC, we can see that the new Tenant that we created is now there.
Click on Tenants in the GUI and you should see the new Tenant created.
We can now use this tool if we want to create Python code from a JSON or XML.
Besides building out our ACI Fabric, we can also use scripts to query the APIC. In the next example,
we will use the script fabric_node.py to list all the nodes in the Fabric.
You can look at the topology of the Fabric by going to Fabric and Topology in the GUI.
21 | P a g e
Our script will query the Fabric and return the name of each node in the Fabric. Unlike some of the
previous scripts, this won’t change anything in the GUI and will instead, return the data straight to
the output.
url = 'https://apic1.dcloud.cisco.com'
apicUser = 'admin'
apicPassword = 'C1sco12345'
# Class Query
print 'Starting query for Fabric nodes and printing out the node names'
classQuery = ClassQuery('fabricNode')
for node in md.query(classQuery):
print node.name
md.logout()
print 'Log into the APIC completed. Logging out.'
If using nano, run the CTRL and “o” to save the script and CTRL and “x” to exit. Lets run our script:
22 | P a g e
(aci) [user01@rhel7-tools aci]$ python fabric_node.py
Spine1
Leaf2
apic2
Leaf1
Spine2
apic1
apic3
Log into the APIC completed. Logging out.
(aci) [user01@rhel7-tools aci]$
This should match up with what we had earlier when we looked at the topology.
Conclusion
Cisco ACI provides powerful tools for scripting and automation using the Cobra SDK and Python.
In this session, we briefly went over what the Cobra SDK was and saw some very basic scripts
to log into the APIC, create a basic Tenant, and build a full 3 Tier Application.
https://cobra.readthedocs.io/en/latest/index.html
https://www.cisco.com/go/aci
Please see the proctor to disconnect you from the lab and to reset it for the next
participant. We would love your feedback as we are always looking to add more information
and improve the content.
23 | P a g e