Ansible Networking Modules - Managing Configurations
Ansible Networking Modules - Managing Configurations
Modules
Ivan Pepelnjak (ip@ipSpace.net)
Network Architect
ipSpace.net AG
This material is copyrighted and licensed for the sole use by Mikel Maeso (mikel.maeso@gmail.com [85.87.178.33]). More information at http://www.ipSpace.net/Webinars
Who is Ivan Pepelnjak (@ioshints)
Past
• Kernel programmer, network OS and web developer
• Sysadmin, database admin, network engineer, CCIE
• Trainer, course developer, curriculum architect
• Team lead, CTO, business owner
Present
• Network architect, consultant, blogger, webinar and book author
Focus
• SDN and network automation
• Large-scale data centers, clouds and network virtualization
• Scalable application design
• Core IP routing/MPLS, IPv6, VPN
More @ ipSpace.net/About
2 This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Ansible Networking Modules (Ansible 2.2)
Switches and routers:
• Arista EOS
• Cisco IOS, IOS-XR, NX-OS
• Cumulus Linux
• Dell OS6, OS9, OS10
• Junos
• OpenSwitch
• Nokia SR OS
• Vyos
Load balancers: A10, Citrix, F5
Other:
• Open vSwitch
• NETCONF config
This material is copyrighted and licensed for the sole use by Mikel Maeso (mikel.maeso@gmail.com [85.87.178.33]). More information at http://www.ipSpace.net/Webinars
Changing Network Device Configuration with Ansible
Generic solutions:
• Execute individual configuration commands (Arista, Cisco, Juniper, Vyos)
• Push template-generated configuration to the device (Arista, Cisco,
Juniper)
Device-specific tasks:
• Cisco ASA: manage ACLs
• Cumulus Linux: Configure bonds (LAGs), bridges, interfaces
• Junos: manage NETCONF and packages
• Nexus OS: manage features, interfaces, IP interfaces, switchports,
VLANs, VRF and VRRP, BGP and OSPF, VXLAN and EVPN, IGMP…
This material is copyrighted and licensed for the sole use by Mikel Maeso (mikel.maeso@gmail.com [85.87.178.33]). More information at http://www.ipSpace.net/Webinars
Enable Nexus OS Features (Nexus 9000 only)
---
- hosts: nxos
tasks:
- nxos_feature:
feature: "{{item}}"
state: enabled
provider: "{{cli}}"
with_items: [ ospf,bgp,nxapi,lacp]
---
- hosts: nxos
tasks:
- nxos_nxapi:
provider: "{{cli}}"
state: "{{API|default('started')}}"
$ ansible-playbook -v nexus-enable-api.yml
$ ansible-playbook -v nexus-enable-api.yml
--extra-vars "API=stopped"
Demo 1
10This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Create VLANs on Nexus OS: Data Model
# all.yml
…
#
# List of VLANs present on all switches
# VLAN 1 must be present and have name default
#
vlans:
- { id: "1", name: "default" }
- { id: "100", name: "mgmt", subnet: "172.16.1.0/24"}
- { id: "101", name: "web", subnet: "192.168.201.0/24"}
- { id: "110", name: "db", subnet: "192.168.202.0/24"}
# s1.lab.local.yml
---
interfaces:
- { vlan: "100", ip: 172.16.1.101 }
- { vlan: "101", ip: 192.168.201.3 }
- { interface: "loopback0", prefix: "192.168.0.1/32"}
---
- hosts: nxos
name: configure VLANs
tags: VLAN
tasks:
- nxos_vlan:
provider: "{{nxapi}}"
vlan_id: "{{item.id}}"
state: "{{item.state | default('present') }}"
admin_state: "{{ item.admin | default('up') }}"
name: "{{item.name}}"
with_items: "{{vlans}}"
Parameters:
• VLAN ID or range, VLAN name
• State, administrative status
Demo 2
12This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Creating Nexus OS VLANs
$ ansible-playbook -v nexus-vlan-set.yml
---
- hosts: nxos
name: configure VLANs
tags: VLAN
tasks:
- nxos_vlan: …
with_items: "{{vlans}}"
register: vlan_state
- set_fact:
vlans_list: "{{vlan_state.results[0].
existing_vlans_list}}"
- set_fact:
target_list: "{{ vlans|map(attribute='id')|list }}"
- fail: msg="Extra VLAN configured on {{inventory_hostname}}"
when: "{{ vlans_list | difference(target_list) }}"
…
- nxos_vlan: …
with_items: "{{vlans}}"
register: vlan_state
- set_fact:
vlans_list: "{{vlan_state.results[0].
existing_vlans_list}}"
…
- set_fact:
target_list: "{{ vlans|map(attribute='id')|list }}"
…
- fail: msg="Extra VLAN configured on {{inventory_hostname}}"
when: "{{ vlans_list | difference(target_list) }}"
Demo 3
18This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Create Interfaces on Nexus OS
---
Parameters:
- hosts: nxos • Interface name
name: configure interfaces • State (present, absent)
tags: interface • Administrative state
tasks: (up, down)
- nxos_interface:
provider: "{{nxapi}}"
interface: "Vlan{{item.vlan}}"
admin_state: up
with_items: "{{interfaces}}"
when: "{{item.vlan}}"
- nxos_interface:
provider: "{{nxapi}}"
interface: "{{item.interface}}"
admin_state: up
with_items: "{{interfaces}}"
when: "{{item.interface}}"
Demo 4
19This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Checking Configuration Changes
$ ansible-playbook -v nexus-interfaces.yml
---
- hosts: nxos
name: configure interface IP addresses
tags: interface for VLAN interfaces
tasks:
- nxos_ip_interface:
provider: "{{nxapi}}"
interface: "Vlan{{item.vlan}}"
version: v4
addr: "{{item.ip}}"
mask: "{% set v = vlans|selectattr('id','equalto',item.vlan)
|first %}{{ v.subnet|ipaddr('prefix') }}"
with_items: "{{interfaces}}"
when: "{{ item.vlan is defined }}"
Parameters: interface name, IPv4 or IPv6 address, subnet mask, state (present|absent)
vlans:
- { id: "100", name: "mgmt", subnet: "172.16.1.0/24"}
- { id: "101", name: "web", subnet: "192.168.201.0/24"}
Required operation:
• Find prefix for VLAN item.vlan
First steps
• Start with the vlans list of dictionaries
• Select all dictionaries from the list where the id key has value equal to item.vlan
(use selectattr, which is a generator)
• Select the first item from the generator
• Set Jinja2 variable v to the first item returned from the selectattr generator
vlans:
- { id: "100", name: "mgmt", subnet: "172.16.1.0/24"}
- { id: "101", name: "web", subnet: "192.168.201.0/24"}
Demo 5
25This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Execute IP Address Configuration Changes
$ ansible-playbook -v nexus-interfaces.yml
This material is copyrighted and licensed for the sole use by Mikel Maeso (mikel.maeso@gmail.com [85.87.178.33]). More information at http://www.ipSpace.net/Webinars
Managing Devices with Block Indent Configuration Syntax
---
- hosts: ios
tasks:
- name: "Configure SNMP on IOS devices"
ios_config:
provider: "{{cli}}"
lines:
- "snmp-server community {{snmp_community}} RO"
- "snmp-server host {{snmp_host}} {{snmp_community}}"
- hosts: nxos
tasks:
- name: "Configure SNMP on Nexus OS devices"
nxos_config:
provider: "{{cli}}"
lines:
- "snmp-server user {{snmp_community}} network-operator"
- "snmp-server host {{snmp_host}} traps version 2 {{snmp_community}}"
- "snmp-server community {{snmp_community}} group network-operator"
Demo 6
30This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Make Configuration Changes
$ ansible-playbook -v config-simple.yml
$ ansible-playbook -v config-simple.yml
---
- hosts: ios
tasks:
- name: "Configure BGP"
ios_config:
provider: "{{cli}}"
parents:
- "router bgp 65000"
lines:
- "neighbor 172.16.1.101 remote-as 65001"
Demo 7
34This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Check BGP Configuration Changes
$ ansible-playbook -v config-bgp-ios.yml
---
- hosts: ios
tasks:
- name: "Check BGP AS number"
ios_command:
provider: "{{cli}}" One of the few commands When this one is not
that prints BGP AS number empty…
commands:
- "show ip protocol summary | include bgp"
- "show ip protocol summary | include bgp 65000" … this one shouldn’t
be empty either
register: protocols
- name: "Check BGP AS Number"
fail: msg="BGP AS number mismatch {{protocols.stdout[0]}}"
when: "{{ protocols.stdout[0] and not (protocols.stdout[1]) }}"
Hint:
• First command will return some printout if BGP runs on the box
• Second command will return one line if BGP AS number matches
• Replace AS number with a variable in real-life playbook
$ ansible-playbook -v config-bgp-ios-check.yml
Demo 8
38This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Configuring Order-
Sensitive Objects
This material is copyrighted and licensed for the sole use by Mikel Maeso (mikel.maeso@gmail.com [85.87.178.33]). More information at http://www.ipSpace.net/Webinars
Example: Configuring ACL
---
- hosts: ios
tasks:
- name: "Configure ACL on Cisco IOS"
ios_config:
provider: "{{cli}}"
parents:
- "ip access-list extended AllowedTraffic"
lines:
- "permit tcp any eq www any"
- "permit tcp any any eq www"
- "deny tcp any any log"
- "deny ip any any log"
Simplistic approach: let’s list the lines in the ACL and hope for the best
$ ansible-playbook -v config-acl-add.yml
Demo 9
42This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Extra Line Is Inserted at the Bottom of ACL
- ios_config:
provider: "{{cli}}"
parents:
- "ip access-list extended AllowedTraffic"
lines:
- "permit tcp any eq www any"
- "permit tcp any any eq www"
- "deny tcp any any log"
- "deny ip any any log“
match: exact
Demo 10
44This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Retrying with Exact Matching
$ ansible-playbook -v config-acl-add-exact.yml
- ios_config:
provider: "{{cli}}"
parents:
- "ip access-list extended AllowedTraffic"
lines:
- "permit tcp any eq www any"
…
match: exact
before:
- "no ip access-list extended AllowedTraffic"
Demo 11
47This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Remove and Recreate the ACL
$ ansible-playbook -v config-acl-add-before.yml
Second run:
This material is copyrighted and licensed for the sole use by Mikel Maeso (mikel.maeso@gmail.com [85.87.178.33]). More information at http://www.ipSpace.net/Webinars
Deploying Configuration Files to Network Devices
Ansible network device configuration modules take two types of arguments:
• lines potentially combined with parents
• Configuration file or Jinja2 template (src parameter) that is merged with
the current configuration
• Both methods are implemented for Cisco ASA, Dell OS, Arista EOS,
Cisco IOS, IOS-XR and Nexus OS, Junos, Nokia SR OS and Vyos
All modules implement:
• backup parameter (save previous config to Ansible host)
Most modules provide:
• config parameter to supply baseline configuration
• save parameter to save running configuration to startup configuration
---
- name: Enable command logging on Cisco IOS
hosts: ios
- name: Enable/Disable command logging
ios_config:
src: "enableLogging.cfg"
host: "{{ansible_host}}"
username: "{{ansible_user}}"
password: "{{ansible_ssh_pass}}"
register: results
- debug: var=results
Demo 12
53This material is copyrighted
© ipSpace.net 2016 and licensed for the sole use by Mikel Maeso
Ansible(mikel.maeso@gmail.com [85.87.178.33]).
for Networking Engineers – Managing More information at http://www.ipSpace.net/Webinars
Configurations
Deployment of Configuration Templates