syedur_rahman.vrf_command
vrf_command
This Ansible Network role allows you to send VRF-aware network commands to network devices in a dynamic way. It currently works with IOS, EOS, and NXOS devices.
This role improves the default behavior of the following modules:
ios_command
nxos_command
eos_command
Demonstration
Check Example Playbook 2 for details on how to use this role in a demonstration.
Requirements
None.
Role Variable
Update the following value based on your needs (see defaults/main.yml
):
vrf_command_list:
- "show ip route vrf <vrf>"
- "show version"
The vrf_command_list
should include all commands you want to run on your network devices. Use <vrf>
in the commands to make them VRF-aware.
The <vrf>
will be replaced with the available VRFs on the device, including the global table. For example, if a device has the VRFs dev
and prod
, the command show ip route vrf <vrf>
will expand to:
show ip route
show ip route vrf dev
show ip route vrf prod
You can also run regular commands that are not VRF-related.
Output Variable
The role creates a custom output variable called vrf_command_output
for user convenience.
The data structure looks like this:
vrf_command_output:
- command: "show ip route vrf dev"
failed: False
output: "...77.77.77.0 is directly connected, FastEthernet1/0\n..." (omitted)
- command: "show blah blah"
failed: True
output: "...% Invalid input detected at..." (omitted)
The data structure returns a list. Each item in the list has three keys:
command
- The command that was run on the network device.failed
- A boolean that shows if the command failed (true
) or succeeded (false
).output
- The output from the command.
Example Playbook 1
This example playbook shows how to use the role in a simple way.
- hosts: all
vars:
vrf_command_list:
- "show ip interface brief vrf <vrf>"
- "show ip route vrf <vrf>"
- "show interface status"
- "show version"
roles:
- syedur_rahman.vrf_command
Example Playbook 2
This example playbook shows how to use vrf_command_output
to save results to text files.
- hosts: all
vars:
vrf_command_list:
- "show ip route vrf <vrf>"
roles:
- syedur_rahman.vrf_command
- hosts: localhost
tasks:
- template:
src: "show_command.j2"
dest: "{{ item }}.txt"
loop: "{{ groups['all'] }}"
Here's the show_command.j2
template:
{% for show_command_info in hostvars[item]['vrf_command_output'] %}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{{ show_command_info['command'] }}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{{ show_command_info['output'] }}
{% endfor %}
This produces a text file per device containing the following format.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
show ip route vrf management
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
IP Route Table for VRF "management"
'*' denotes best ucast next-hop
'**' denotes best mcast next-hop
'[x/y]' denotes [preference/metric]
'%<string>' in via output denotes VRF <string>
192.168.12.0/24, ubest/mbest: 1/0, attached
*via 192.168.12.135, mgmt0, [0/0], 05:12:12, direct
192.168.12.135/32, ubest/mbest: 1/0, attached
*via 192.168.12.135, mgmt0, [0/0], 05:12:12, local
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
show ip route
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
IP Route Table for VRF "default"
'*' denotes best ucast next-hop
'**' denotes best mcast next-hop
'[x/y]' denotes [preference/metric]
'%<string>' in via output denotes VRF <string>
2.2.2.0/24, ubest/mbest: 1/0, attached
*via 2.2.2.2, Lo100, [0/0], 05:12:28, direct
2.2.2.2/32, ubest/mbest: 1/0, attached
*via 2.2.2.2, Lo100, [0/0], 05:12:28, local
2.3.4.0/24, ubest/mbest: 1/0, attached
*via 2.3.4.5, Lo23, [0/0], 05:12:28, direct
2.3.4.5/32, ubest/mbest: 1/0, attached
*via 2.3.4.5, Lo23, [0/0], 05:12:28, local
License
MIT
Author Information
This role was created by Syedur Rahman in 2020.
VRF-aware network commands.
ansible-galaxy install syedur_rahman.vrf_command