The Ansible Cisco example

Cisco's support in Ansible is categorized by the operating systems IOS, IOS-XR, and NX-OS. We have already seen a number of NX-OS examples, so in this section let's try to manage IOS-based devices.

Our host file will consist of two hosts, R1 and R2:

[ios_devices]
R1 ansible_host=192.168.24.250
R2 ansible_host=192.168.24.251

[ios_devices:vars]
username=cisco
password=cisco

Our playbook, cisco_5.yml, will use the ios_command module to execute arbitrary show commands:

    ---
- name: IOS Show Commands
hosts: "ios_devices"
gather_facts: false
connection: local

vars:
cli:
host: "{{ ansible_host }}"
username: "{{ username }}"
password: "{{ password }}"
transport: cli

tasks:
- name: ios show commands
ios_command:
commands:
- show version | i IOS
- show run | i hostname
provider: "{{ cli }}"

register: output

- name: show output in output["end_state"]["contact"]
debug:
var: output

The result is what we would expect as the show version and show run output:

    $ ansible-playbook -i ios_hosts cisco_5.yml

PLAY [IOS Show Commands]
*******************************************************

TASK [ios show commands]
*******************************************************
ok: [R1]
ok: [R2]

TASK [show output in output["end_state"]["contact"]]
***************************
ok: [R1] => {
"output": {
"changed": false,
"stdout": [
"Cisco IOS Software, 7200 Software (C7200-A3JK9S-M), Version
12.4(25g), RELEASE SOFTWARE (fc1)",
"hostname R1"
],
"stdout_lines": [
[
"Cisco IOS Software, 7200 Software (C7200-A3JK9S-M), Version
12.4(25g), RELEASE SOFTWARE (fc1)"
],
[
"hostname R1"
]
]
}
}
ok: [R2] => {
"output": {
"changed": false,
"stdout": [
"Cisco IOS Software, 7200 Software (C7200-A3JK9S-M), Version
12.4(25g), RELEASE SOFTWARE (fc1)",
"hostname R2"
],
"stdout_lines": [
[
"Cisco IOS Software, 7200 Software (C7200-A3JK9S-M), Version
12.4(25g), RELEASE SOFTWARE (fc1)"
],
[
"hostname R2"
]
]
}
}

PLAY RECAP
*********************************************************************
R1 : ok=2 changed=0 unreachable=0 failed=0
R2 : ok=2 changed=0 unreachable=0 failed=0

I wanted to point out a few things illustrated by this example:

  • The playbook between NXOS and IOS is largely identical
  • The syntax nxos_snmp_contact and ios_command modules follow the same pattern, with the only difference being the argument for the modules
  • The IOS version of the devices are pretty old with no understanding of API, but the modules still have the same look and feel

As you can see from the preceding example, once we have the basic syntax down for the playbooks, the subtle difference relies on the different modules for the task we would like to perform.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.50.198