Network Automation with Ansible – Ad-Hoc commands

Use of Ad-Hoc commands for fetching information from network devices for troubleshooting purpose

I am using Ad-Hoc commands for troubleshooting purposes and Playbooks for configuring purposes.
Ad-hoc commands are very useful and handy for troubleshooting and pulling out configurations or output of a command and then saving it to some files for future purpose

Show ip interface brief

Let’s say we are trying to pull out the interface details with “show ip interface brief” command.
If I use the same legacy method of pulling out the output, then I will have to follow the following steps.
Ssh to the device
Run show ip interface brief
Filter the output

And all this is on one device. Let’s say if we have 10, 50 or 100 devices and wanted to pull out the same output, then we will have to follow the same procedure on all the devices one by one. And if we need to make a note of the output then.

Now lets see how easily we can pull out that information from all the devices in one go with single command and if we want to save it to a file, even that can be done with a single command.
ansible all -m raw -a “show ip int br” -u cisco -k
ansible all -m raw -a “show ip int br” -u cisco -k | grep ‘SUCCESS|Vlan’
SSH password:
Core1 | SUCCESS | rc=0 >>
Vlan1 192.168.26.101 YES NVRAM up up
Core2 | SUCCESS | rc=0 >>
Vlan1 192.168.26.102 YES NVRAM up up
I ran it on 2 devices as my hosts file (all) includes only 2 devices. I can have 10, 50, 100 or more then that and one single command will be able to pull out the content.
We need to tweak it as per our time, which is again one time effort.

ansible all -m raw -a “show ip int br” -u cisco -k | grep ‘SUCCESS|up’
SSH password:
Core2 | SUCCESS | rc=0 >>
FastEthernet1/0 unassigned YES unset up up
FastEthernet1/1 unassigned YES unset up down
FastEthernet1/2 unassigned YES unset up down
FastEthernet1/3 unassigned YES unset up down
FastEthernet1/4 unassigned YES unset up down
FastEthernet1/5 unassigned YES unset up down
FastEthernet1/6 unassigned YES unset up down
FastEthernet1/7 unassigned YES unset up down
FastEthernet1/8 unassigned YES unset up down
FastEthernet1/9 unassigned YES unset up down
FastEthernet1/10 unassigned YES unset up down
FastEthernet1/11 unassigned YES unset up down
FastEthernet1/12 unassigned YES unset up down
FastEthernet1/13 unassigned YES unset up up
FastEthernet1/14 unassigned YES unset up up
FastEthernet1/15 unassigned YES unset up up
Vlan1 192.168.26.102 YES NVRAM up up
Core1 | SUCCESS | rc=0 >>
FastEthernet1/0 unassigned YES unset up up
FastEthernet1/1 unassigned YES unset up up
FastEthernet1/2 unassigned YES unset up up
FastEthernet1/3 unassigned YES unset up up
FastEthernet1/4 unassigned YES unset up down
FastEthernet1/5 unassigned YES unset up down
FastEthernet1/6 unassigned YES unset up down
FastEthernet1/7 unassigned YES unset up down
FastEthernet1/8 unassigned YES unset up up
FastEthernet1/9 unassigned YES unset up down
FastEthernet1/10 unassigned YES unset up down
FastEthernet1/11 unassigned YES unset up down
FastEthernet1/12 unassigned YES unset up down
FastEthernet1/13 unassigned YES unset up down
FastEthernet1/14 unassigned YES unset up down
FastEthernet1/15 unassigned YES unset up down
Vlan1 192.168.26.101 YES NVRAM up up

Show version

ansible all -m raw -a “show version” -u cisco -k
ansible all -m raw -a “show version” -u cisco -k | grep SUCCESS
SSH password:
Core1 | SUCCESS | rc=0 >>
Core2 | SUCCESS | rc=0 >>

ansible all -m raw -a “show version” -u cisco -k | grep ‘SUCCESS|UNREACHABLE’
SSH password:
Core2 | SUCCESS | rc=0 >>
Core1 | SUCCESS | rc=0 >>
Access1 | UNREACHABLE! => {
Access2 | UNREACHABLE! => {
Access3 | UNREACHABLE! => {

ansible all -m raw -a “show version” -u cisco -k | grep ‘SUCCESS|System returned to ROM|UNREACHABLE’
SSH password:
Core1 | SUCCESS | rc=0 >>
System returned to ROM by unknown reload cause – suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19
Core2 | SUCCESS | rc=0 >>
System returned to ROM by unknown reload cause – suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19
Access1 | UNREACHABLE! => {
Access2 | UNREACHABLE! => {
Access3 | UNREACHABLE! => {

ansible all -m raw -a “show version” -u cisco -k | grep ‘SUCCESS|System returned to ROM|uptime|UNREACHABLE’
SSH password:
Core1 | SUCCESS | rc=0 >>
Core1 uptime is 9 minutes
System returned to ROM by unknown reload cause – suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19
Access1 | UNREACHABLE! => {
Access2 | UNREACHABLE! => {
Access3 | UNREACHABLE! => {
Core2 | SUCCESS | rc=0 >>
Core2 uptime is 9 minutes
System returned to ROM by unknown reload cause – suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19

ansible all -m raw -a “show version” -u cisco -k | grep ‘SUCCESS|System returned to ROM|uptime|UNREACHABLE’ > uptime.txt
This will save the output in a file named uptime.txt

Show running-configuration

ansible all -m raw -a “show runn” -u cisco -k > show_runn.txt
I perform all the above commands on few devices, but that can be performed on 100s of devices at the same time. We just need to add all the devices in the hosts file and run the commands.
Here we are not logging to individual devices and fetching the information, but running the same legacy command from Ansible Control Server.

You may also like...

2 Responses

  1. Mohit Bhardwaj says:

    Hello sir,

    where dis you define device details ?

    will it get from Hosts ?

  2. Mayank says:

    Hi Mohit,
    you can define it on the host itself.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.