Ansible Basic Commands

Here I have a collection of commands for reference. Ansible can be run using ad-hoc commands or playbooks.
Ad-hoc commands are all in the single command. Raw commands are entered in the Ad-hoc mode and are the commands that would usually be entered into the CLI of the network device or server.
Playbooks are a series of plays to commands that will be run and on which devices. They can have variables in and get fairly complex.

This post will be a quick reference guide for Ansible commands on my Cisco lab.
All of the playbooks can be found on my Github

Quick Contents

Ad-hoc Commands

My Ansible inventory file has 4 devices that are split into two groups. There are only IP addresses as I am not using any DNS for the lab. In a larger environment have DNS and device hostnames is a better idea.

Switch2 that has the IP address of 172.16.1.102 is on an older version of IOS that supports DH1 and SHA1 only. Meaning that I can SSH to it. The fix I found that worked for me was to use paramiko. This can be set when running the Ansible command “-c paramiko”.

Run commands for all devices in the inventory

Run commands for the group lab_access in the inventory

Run commands for a group, and grep for the “show version” uptime. Output included.

There is an option to redirect the output into a text file instead of displaying it to the terminal.

Playbooks

The playbooks are a way of running more than just single commands. There can be multiple plays, tasks and logic applied.
All of these playbooks can be found on my Github

Basic Playbook

I have tested a basic playbook that just runs the command “show mac address table”. This is a raw command as was performed in the ad hoc mode.
My router 172.16.1.104 does not have a MAC table as it is not a switch and so has an error.
This is where groups come in handy.

The playbook to get this output is show below

IOS Show Command Playbook

This playbook uses the IOS command module which needs to first be installed. Documentation can be found here.
This is for running “show” type commands only.
The install is run in the terminal.

I also need to update my inventory file to group all the devices together in a single group. This is done to use group variables. The variables can hold things such as credentials and device types.

Original Inventory

New Inventory

The name of the groups are used in the group variables is used in the group variable directory and the file name. This is very important otherwise it will not work. See the diagram.

Another change I have made is to the default inventory file. This is now pointing to my inventory file.
The change is made in the ansible.cfg file located in /etc/ansible.
These are the two line I added

The good thing is the the “-c paramiko” flag still works so access to all fours devices still. and the command is much smaller.

Magic Variable Playbook

A magic variable is a way of getting variables into the playbook. Jinja is used, this is a way of getting python like syntax. I have used this in flask for the HTML templates.

This playbook is going to connect to the devices, run “show version”, print the output to the console and then save that output as the inventory hostname. In my case the IP address of each device.

The output as expected…

Cisco Facts Playbook

Gathering facts is a default option that can be performed on Linux servers. For network devices they need an additional install for Ansible to know what to look for.
This is the Cisco facts module, use the examples to get different facts out. The examples go into a task. The playbooks below show examples of the output being saved as a JSON file for easy manipulation.

A second playbook to get the idea of using the examples to get different output. This playbook gets interface facts and running config. Everything but the hardware.

Playbook IOS Configuration – Single Device

This, like the IOS command playbook requires an Ansible module. This is the same as was installed previously.

The biggest hurdle to overcome for this is getting past the enable password. There are three methods I have used to do this.

The first is probably the most simple, configure SSH users logging in with privilege level 15 as part of the standard device config.

The second is to store the passwords in the group_vars file. This can be revisited later for Ansible vault.

The third is to ask for the credentials when running the Ansible playbook, and having the “become” modules in the playbook task. I have used this one as a demonstration. The playbook logins into R1 172.16.1.104 and configures OSPF for all addresses.

Playbook IOS Configuration – Multi Device

Each play in a playbook can be performed on a set of devices. These can be groups, single or multiple hosts.

Group named lab_core

Single device, as appears in the inventory

Multiple device, as they appear in the inventory

Playbook IOS Configuration – Multiple Tasks

There can be multiple tasks in a single playbook that performs different functions for the same devices or a subset of them. In this playbook there tare two tasks to configure basic commands on the switches of the lab topology.

Playbook IOS Configuration – Multiple Plays

There can be multiple plays in a single playbook. Each play has its own set of hosts to select and tasks associated with that play.

I have moved the enable password into the group_vars file for these plays.

In this example playbook I have two plays. The first is to enable OSPF on the router. And the second play is to create 4 VLANs on the selected switches.

Leave a Comment

Your email address will not be published. Required fields are marked *