BITESIZE ANSIBLE – ADHOC COMMANDS

Round 2 - FIGHT (with your network hosts no more!)

Welcome to the second in our series of blogposts about sharing bitesize pieces of Ansible knowledge. Today, I'm going to write about a question someone asked me earlier about finding out information from lots of remote systems quickly...

As before, any examples given below are available at https://github.com/evolvere-techand if you have any questions or comments, you can now post them here at the bottom of the page! If you want to get in touch with us directly, you can also email us at info@evolvere-tech.co.uk

You'll also need ansible installed too (pip install ansible should do it on most linux operating systems)

Please note, this demo is making use of Ansible 2.5 ( documentation here: Ansible Docs ) which has more enhanced networking features than previous versions

The needs of the many (and sometimes the few)

I was talking to a Network Engineer at a site I'm working at earlier today and he asked if I knew how to quickly get some information about a large number of network devices. Given that I'm working on a fairly large Ansible conversion project, you can guess what my first answer was..ANSIBLE!

The general gist was, that he needed to get some system information from a significant number of devices to audit what was in the estate. He had a list of the devices by hostname he wanted info on and access to a linux terminal (woahhh, he was half way there...)

And then I said...

Lets try using ansible adhoc! We decided as they were cisco devices to use the cisco ios_command module (http://docs.ansible.com/ansible/latest/modules/ios_command_module.html) so we needed to do some prep work;

We put the list of hosts into an inventory file; this was simple enough but we need it before we can run the command. As we use keys at this site, we created the necessary variables in the inventory example you can see below;

Screen Shot 2019-03-19 at 19.15.45.png

Then we constructed the command we wanted to execute. As I was showing someone else, I took them through the adhoc basics, like the common usage for ansible adhoc;

Screen Shot 2019-03-19 at 19.23.10.png

And now for the command;

Screen Shot 2019-03-19 at 19.24.05.png

Going through this item by item;

we specify the group in our inventory to operate on, in this case "switches"

-i specifies the inventory file to use

-c specifies the connection method - Its important to note that with version 2.5 of ansible, the network_cli connection method should be used as the default method to connect to network devices (more info here Ansible Network CLI)

-m is the name of the module we wish to use. Here, thats ios_commandn as we're connnecting to cisco devices

-e are any extra variables we want to pass. For the sake of a complete example, I've added the ansible_network_os variable here, but we could just as easily have put that into the inventory vars for the group switches

-a are arguments for the ios_command module (or whichever module you have chosen to use). Take a look through the appropriate module documentation for any required arguments as missing them out will cause the execution to fail. Also note that if you have spaces in any of the args (like we do here for show version) you will need to ensure they are in single quotes in order for them to be passed to the module correctly

 

I survived Ansible command line and all I got was this crazy output...

When you successfully run that command, you should see the results appearing in JSON format on your screen! The show version output can be quite verbose and not immediately readable. In this case, we simply piped the output to a file ( > version_output.txt at the end of the command on linux) and ran grep/searches in that file for the information we required. Heres some example output from our demo lab;

Obviously, we could be more programmatic in getting information out of the command output using a playbook, but as this was a very quick solution to an immediate and simple problem, it was enough for what we wanted.

 

So to sum up, Ansible ad-hoc commands are a great way of simplifying monotonous tasks quickly and easily. Don't forget there are a whole host of new network updates in the latest version of Ansible (2.5) which make automating networks so much easier.

If you'd like some help with any aspect of Ansible Automation large or small, reach out to us at info@evolvere-tech.co.uk or comment on this post and our certified experts will get back to you to arrange a chat.