Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Network automation concept
1. Network Automation Concept
1 - YAML
<< SHOW OF HANDS WHO HAS EXPERIENCE WITH YAML >>
Why YAML - it's simple, well known, and does the job
Great tool for representing data, no more (just want the data)
Similar-ish to XML or JSON but more useful to human beings than those two
First exercise is a list:
---
- Juniper
- Cisco
- Brocade
- VMware
Just an arbitrary list. No particular order, doesn't have to be unique.
I'll keep the Python light - for illustrative purposes only. Focus on YAML. We need to write a small
function to import the YAML data
import yaml
from pprint import pprint
def ly(filename):
with open(filename) as _:
return yaml.load(_)
Now we have a function we can use to quickly load the YAML files we write
Let's load the YAML file we wrote.
w body = ly('1-list.yml') print(type(body)) print(len(body)) print(body)
This is the representation of the data in Python
Items in list don't even have to be same type!! YAML mimics the flexibility of Python Notice that
strings (usually) don't have to be enclosed in quotes.
---
- Plants
- 12345
- False
- ['Hello', 'Workld', '!']
2. Let's run our function again
body = ly('2-mixedtypeslist.yml')
print(type(body))
print(len(body))
print(body)
Small loop to show individual types
for item in body:
print(type(item))
We can write dict of vendor to website mappings
---
Juniper: http://www.juniper.net/us/en/
Cisco: http://www.cisco.com/
Brocade: http://www.brocade.com/index.page
VMware: http://www.vmware.com/
Same as before, we can print type and length
body = ly('3-dictionary.yml')
print(type(body))
print(len(body))
We can call up a specific member of this dictionary by key
print(body['Juniper'])
Dictionaries can be mixed types
Also notice # for comment. Not part of data.
---
Juniper: Also a plant
Cisco: 6500 # They're still around, trust me.
Brocade: True
VMware: ['esxi', 'vcenter', 'nsx']
body = ly('4-mixedtypesdict.yml')
pprint(body)
3. If we specify values, iterates like a list
for v in body.values():
print(type(v))
Nesting is possible with lists and dictionaries
Here is a list OF dictionaries
---
- name: Juniper
products: ['vMX', 'vSRX', 'Contrail']
- name: Cisco
products: ['Nexus 7K', 'Catalyst 3750', 'ACI']
- name: Brocade
products: ['BVC', 'Vyatta 5400 vRouter', 'VDX 6740']
- name: VMware
products: ['vCenter', 'NSX', 'Virtual SAN']
Pretty print definitely comes in handy here.
body = ly('5-nesting.yml')
pprint(body)
As you can see, the root object is still a list
print(type(body))
However, we can loop through this list and see that there are dictionaries nested inside. This is
a list of dictionaries.
for vendor in body:
print(type(vendor))
Let's augment this loop a little bit to print the vendor name, and the first product in the list
for vendor in body:
print('First %s product is %s' % (vendor['name'], vendor['products'][0]))
You may have noticed YAML can store lists one of two ways I tend to use the one that looks
better (explicit is better than implicit).
---
- name: Juniper
products:
4. - vMX
- vSRX
- Contrail
- name: Cisco
products:
- Nexus 7K
- Catalyst 3750
- ACI
- name: Brocade
products:
- BVC
- Vyatta 5400 vRouter
- VDX 6740
- name: VMware
products: ['vCenter', 'NSX', 'Virtual SAN']
I'll run the same thing I ran in the last example on this new file. Same results.
body = ly('6-nesting2.yml')
for vendor in body:
print('First %s product is %s' % (vendor['name'], vendor['products'][0]))
We'll use Ansible in a bit - it does module arguments a little differently.
Either all on one line, or chunked up like this:
login_to_router:
user=root
passwd=Juniper
port=22
host=10.12.0.1
Python renders this as a single dictionary, all args just one big string
body = ly('7-sampleargs.yml')
print(body)
This implies that your Python is responsible for parsing this out. Fortunately, Ansible does
this for us.