Network Automation With Ansible
Network Automation With Ansible
Network Automation With Ansible
Ansible
[ALL:children]
• Variables can be assigned to IOS
XR
hosts or groups
• Default groups:
• all
• ungrouped
Ansible Concepts: Modules
• Modules are the nuts and bolts
$ ansible IOS -m raw -a “show ip route sum”
of Ansible automation tasks
• Playbooks use Modules to
execute tasks on the managed $ cat ios_sh_ip_route_sum.yml
devices ---
gather_facts: false
• Example modules:
tasks:
• raw
- raw: sho ip route summary
• ios_command
• ios_config
Ansible Concepts: YAML
tasks: tasks:
- name: Collect Router Version and Config - name: Collect Router Version and Config
ios_command: iosxr_command:
authorize: yes commands:
commands: - show version
- show version - show ip int bri
- show run
register: value
register: value
- debug: var=value.stdout_lines
- debug: var=value.stdout_lines
Basic Playbooks: Register & Debug
• Basic Playbooks contain register and debug commands.
• Register
- The “register” statement is used to capture the output of a task into a variable.
- In previous example, we are saving the output of the show commands to the variable value.
- Refer: http://docs.ansible.com/ansible/latest/playbooks_conditionals.html#register-variables
• Debug
- The “debug” module prints statements during playbook execution.
- The ”debug” modules takes in a var parameter, which is the variable you want to print.
- Refer: http://docs.ansible.com/ansible/latest/debug_module.html
Basic Playbooks: ios & xr config module
• Module Names: ios_config & iosxr_config
• The config modules are used to configure the cisco routers.
• The modules uses parent and line options to structure the configuration in a
hierarchical way.
• Both modules require local connection execution method.
ios_config iosxr_config
--- ---
- name: IOS Module Router Config - name: XR Module Router Config
hosts: IOS hosts: XR
gather_facts: false gather_facts: false
connection: local connection: local
tasks: tasks:
- name: Configure Interface Setting - name: Configure Interface Setting
ios_config: iosxr_config:
parents: "interface Ethernet1” parents: "interface GigabitEthernet0/0/0/0”
lines: lines:
- "description test” - "description test”
- "ip address 172.31.1.1 255.255.255.0” - "ip address 172.31.1.1 255.255.255.0”
Basic Playbooks: Variables
• Ansible variables are used to store ---
With_items before Ansible Ver 2.5 Updated to loop in Ansible Ver 2.5
tasks: tasks:
- name: Collect Rtr Ver and Cfg - name: Collect Rtr Ver and Cfg
ios_command: ios_command:
authorize: yes authorize: yes
commands: "{{ item }}" commands: "{{ item }}"
with_items: loop:
- show version - show version
- show run - show run
Basic Playbooks: Conditionals
• Ansible conditionals are used in a statement to decide whether to run the task or
not.
• Ansible uses a when clause to dictate a conditional which needs to be true in order
for the task to be performed.
tasks:
- name: Collect Router Version
ios_command:
authorize: yes
commands:
- show ip int bri
when: ansible_user == "cisco"
Automating Network
Operations Tasks
Network Automation Exercises
• Exercise 1 – Configure OSPF on all routers
• Create Ansible playbook to configure OSPF on both IOS and XR router
• Setup pre and post checks to ensure OSPF is working correctly
import_playbook: basic_ios_cmd.yml
- name: xr config
import_playbook: basic_xr_cmd.yml
Roles
[roles/
• Organize a large playbook into reusable file ├── xr-ospf >> Name of this role
structures │ ├── defaults >> default variables for the role
│ │ └── main.yml
• Creates a separation of functions; │ ├── files >> contains files which can be deployed
variables, tasks, & templates in unique │ ├── handlers >> contains handlers
│ │ └── main.yml
directories │ ├── meta >> defines some meta data for this role
│ │ └── main.yml
• Expects files main.yml, and .j2 files in │ ├── README.md
respective folders │ ├── tasks >> contains the list of tasks
│ │ └── main.yml
• File structure can be created manually or │ ├── templates >> contains templates which can be deployed
automatically via ansible CLI – “ansible- │ └── vars>> contains variables used in this role
galaxy”
Roles Style Config
name: read config
iosxr_command:
register: OUT
# Variable defined in xr-config/vars/main.yml # Leverages j2 template for standard and variable config
hostname {{item.hostname}}
---
service timestamps log datetime msec
router_hostname:
service timestamps debug datetime msec
- { hostname: router1 }
clock timezone {{item.timezone}} {{item.timezone_offset}}
- { hostname: router2 }
clock summer-time {{item.timezone_dst}} recurring
- { hostname: router3 }
...
Jinja2 Template – For loop
• For Loop is invoked using {% for x in y %} syntax and ends with {% endfor %} syntax
# /template/template.j2
{% for INTF in interface_list %}
interface {{INTF}}
cost 1
!
{% endfor %}
!
# /vars/main.yml
Interface_list:
- GigabitEthernet0/0/0/0
- GigabitEthernet0/0/0/1
Hierarchical templates and Block configs
• Ansible Training
• Ansible for the Absolute Beginner @Udemy Click here
• Ansible for Network Engineers @Udemy Click here
• Kirk Byers Ansible training Jive page
• Dcloud lab Ansible for Cisco Nexus Switches v1
Acknowledgement
Acknowledgements