When you know in advance which interfaces you would like to bounce, you can bounce multiple interfaces based on a list provided by way of a variable that is set in a vars file.
In this exercise, you will automate the process of bouncing specified non-management interfaces on an IOS device.
Outcomes
You should be able to:
Compose NOS-specific device configuration templates to provide parameterized configuration statements.
Perform a play that bounces all interfaces on an IOS device.
Open a terminal window on the workstation VM and change to the ~/proj/ directory.
Compose NOS-specific device configuration templates to provide parameterized configuration statements.
Create a vars/ directory if it does not already exist.
[student@workstation proj]$mkdir -p vars
Download the layer3-branch-corp-cloud.yml file into the vars/ directory.
This file maps IPv4 and IPv6 protocol data to interfaces.
[student@workstation proj]$cd vars[student@workstation vars]$wget \>http://materials.example.com/full/vars/layer3-branch-corp-cloud.yml[student@workstation vars]$cd ..[student@workstation proj]$
Create a Jinja2 template file named j2/ios-if-shut-all.j2 that contains the following:
{% for intf in interface_data[inventory_hostname] %}
{% if not intf.name.startswith('Loopback') %}
interface {{ intf.name }}
shutdown
{% endif %}
{% endfor %}
Create a Jinja2 template file named j2/ios-if-noshut-all.j2 that contains the following:
{% for intf in interface_data[inventory_hostname] %}
{% if not intf.name.startswith('Loopback') %}
interface {{ intf.name }}
no shutdown
{% endif %}
{% endfor %}Perform a play that bounces all interfaces on an IOS device.
Compose a playbook named ios-bounce-if-all.yml that uses the templates.
[student@workstation proj]$cat ios-bounce-if-all.yml--- - name: bounce all interfaces on ios devices hosts: ios vars: shut_template: j2/ios-if-shut-all.j2 no_shut_template: j2/ios-if-noshut-all.j2 vars_files: - vars/layer3-branch-corp-cloud.yml tasks: - name: shut/no shut on all ios interfaces ios_config: src: "{{ shut_template }}" when: ansible_network_os == 'ios' - name: pause before enabling interfaces pause: seconds: 1 when: ansible_network_os == 'ios' - name: shut/no shut on all ios interfaces ios_config: src: "{{ no_shut_template }}" when: ansible_network_os == 'ios'
Check the playbook syntax and then perform the play.
[student@workstation proj]$ansible-playbook --syntax-check ios-bounce-if-all.ymlPlaybook: ios-bounce-if-all.yml[student@workstation proj]$ansible-playbook ios-bounce-if-all.yml
This concludes the guided exercise.