{"id":58,"date":"2019-06-29T19:24:12","date_gmt":"2019-06-29T11:24:12","guid":{"rendered":"http:\/\/mayanknauni.com\/?p=58"},"modified":"2019-06-29T22:44:00","modified_gmt":"2019-06-29T14:44:00","slug":"network-automation-with-ansible-ad-hoc-commands","status":"publish","type":"post","link":"https:\/\/mayanknauni.com\/?p=58","title":{"rendered":"Network Automation with Ansible &#8211; Ad-Hoc commands"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Use of Ad-Hoc commands for fetching information from network devices for troubleshooting purpose<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I am using Ad-Hoc commands for troubleshooting purposes and Playbooks for configuring purposes.<br> 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<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span style=\"text-decoration: underline;\">Show ip interface brief<\/span><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s say we are trying to pull out the interface details with \u201cshow ip interface brief\u201d command.<br>If I use the same legacy method of pulling out the output, then I will have to follow the following steps.<br>     <strong>Ssh to the device<br>     Run show ip interface brief<br>     Filter the output<\/strong><br>And all this is on one device. Let&#8217;s<code> 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. <\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Now let<\/code>s 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.<br> <strong><em>ansible all -m raw -a &#8220;show ip int br&#8221; -u cisco -k<br> ansible all -m raw -a &#8220;show ip int br&#8221; -u cisco -k | grep &#8216;SUCCESS|Vlan&#8217;<br><\/em><\/strong> SSH password:<br> <strong>Core1 | SUCCESS | rc=0 &gt;&gt;<\/strong><br> Vlan1                     192.168.26.101 YES NVRAM up                   up     <br> Core2 | SUCCESS | rc=0 &gt;&gt;<br> Vlan1                     192.168.26.102  YES NVRAM up                   up     <br> 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.<br> We need to tweak it as per our time, which is again one time effort.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> <strong><em>ansible all -m raw -a &#8220;show ip int br&#8221; -u cisco -k | grep &#8216;SUCCESS|up&#8217;<\/em><\/strong><br> SSH password:<br> <strong>Core2 | SUCCESS | rc=0 &gt;&gt;<\/strong><br> FastEthernet1\/0           unassigned     YES unset up                   up<br> FastEthernet1\/1           unassigned     YES unset up                   down<br> FastEthernet1\/2           unassigned     YES unset up                   down<br> FastEthernet1\/3           unassigned     YES unset up                   down<br> FastEthernet1\/4           unassigned     YES unset up                   down<br> FastEthernet1\/5           unassigned     YES unset up                   down<br> FastEthernet1\/6           unassigned     YES unset up                   down<br> FastEthernet1\/7           unassigned     YES unset up                   down<br> FastEthernet1\/8           unassigned     YES unset up                   down<br> FastEthernet1\/9           unassigned     YES unset up                   down<br> FastEthernet1\/10          unassigned     YES unset up                   down<br> FastEthernet1\/11          unassigned     YES unset up                   down<br> FastEthernet1\/12          unassigned     YES unset up                   down<br> FastEthernet1\/13          unassigned     YES unset up                   up<br> FastEthernet1\/14          unassigned     YES unset up                   up<br> FastEthernet1\/15          unassigned     YES unset up                   up<br> Vlan1                     192.168.26.102        YES NVRAM up                   up     <br> <strong>Core1 | SUCCESS | rc=0 &gt;&gt;<\/strong><br> FastEthernet1\/0           unassigned     YES unset up                   up<br> FastEthernet1\/1           unassigned     YES unset up                   up<br> FastEthernet1\/2           unassigned     YES unset up                   up<br> FastEthernet1\/3           unassigned     YES unset up                   up<br> FastEthernet1\/4           unassigned     YES unset up                   down<br> FastEthernet1\/5           unassigned     YES unset up                   down<br> FastEthernet1\/6           unassigned     YES unset up                   down<br> FastEthernet1\/7           unassigned     YES unset up                   down<br> FastEthernet1\/8           unassigned     YES unset up                   up<br> FastEthernet1\/9           unassigned     YES unset up                   down<br> FastEthernet1\/10          unassigned     YES unset up                   down<br> FastEthernet1\/11          unassigned     YES unset up                   down<br> FastEthernet1\/12          unassigned     YES unset up                   down<br> FastEthernet1\/13          unassigned     YES unset up                   down<br> FastEthernet1\/14          unassigned     YES unset up                   down<br> FastEthernet1\/15          unassigned     YES unset up                   down<br> Vlan1                     192.168.26.101 YES NVRAM up                   up     <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span style=\"text-decoration: underline;\">Show version <\/span><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>ansible all -m raw -a &#8220;show version&#8221; -u cisco -k<br>ansible all -m raw -a &#8220;show version&#8221; -u cisco -k | grep SUCCESS<br><\/em><\/strong> SSH password:<br> Core1 | SUCCESS | rc=0 &gt;&gt;<br> Core2 | SUCCESS | rc=0 &gt;&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> <strong><em>ansible all -m raw -a &#8220;show version&#8221; -u cisco -k | grep &#8216;SUCCESS|UNREACHABLE&#8217;<br><\/em><\/strong> SSH password:<br> Core2 | SUCCESS | rc=0 &gt;&gt;<br> Core1 | SUCCESS | rc=0 &gt;&gt;<br> Access1 | UNREACHABLE! =&gt; {<br> Access2 | UNREACHABLE! =&gt; {<br> Access3 | UNREACHABLE! =&gt; {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ansible all -m raw -a &#8220;show version&#8221; -u cisco -k | grep &#8216;SUCCESS|System returned to<\/strong> ROM|UNREACHABLE&#8217;<br> SSH password:<br> Core1 | SUCCESS | rc=0 &gt;&gt;<br> System returned to ROM by unknown reload cause &#8211; suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19<br> Core2 | SUCCESS | rc=0 &gt;&gt;<br> System returned to ROM by unknown reload cause &#8211; suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19<br> Access1 | UNREACHABLE! =&gt; {<br> Access2 | UNREACHABLE! =&gt; {<br> Access3 | UNREACHABLE! =&gt; {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>ansible all -m raw -a &#8220;show version&#8221; -u cisco -k | grep &#8216;SUCCESS|System returned to<\/em><\/strong> ROM|uptime|UNREACHABLE&#8217;<br> SSH password:<br> Core1 | SUCCESS | rc=0 &gt;&gt;<br> Core1 uptime is 9 minutes<br> System returned to ROM by unknown reload cause &#8211; suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19<br> Access1 | UNREACHABLE! =&gt; {<br> Access2 | UNREACHABLE! =&gt; {<br> Access3 | UNREACHABLE! =&gt; {<br> Core2 | SUCCESS | rc=0 &gt;&gt;<br> Core2 uptime is 9 minutes<br> System returned to ROM by unknown reload cause &#8211; suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>ansible all -m raw -a &#8220;show version&#8221; -u cisco -k | grep &#8216;SUCCESS|System returned to ROM|uptime|UNREACHABLE&#8217; &gt; uptime.txt<\/em><\/strong><br><strong>This will save the output in a file named uptime.txt<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Show running-configuration<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>ansible all -m raw -a &#8220;show runn&#8221; -u cisco -k &gt; show_runn.txt<\/em><\/strong><br>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.<br>Here we are not logging to individual devices and fetching the information, but running the same legacy command from Ansible Control Server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[5],"class_list":["post-58","post","type-post","status-publish","format-standard","hentry","category-netdevops","tag-netdevops"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mayanknauni.com\/index.php?rest_route=\/wp\/v2\/posts\/58","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mayanknauni.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mayanknauni.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mayanknauni.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mayanknauni.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=58"}],"version-history":[{"count":2,"href":"https:\/\/mayanknauni.com\/index.php?rest_route=\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":61,"href":"https:\/\/mayanknauni.com\/index.php?rest_route=\/wp\/v2\/posts\/58\/revisions\/61"}],"wp:attachment":[{"href":"https:\/\/mayanknauni.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mayanknauni.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mayanknauni.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}