Save time with Ansible without writing a line of code

Learning Ansible? Don't be too quick to download a playbook. Its ad hoc mode may be all you need.
548 readers like this.
The Opensource.com preview: April

Opensource.com

When most people start learning about Ansible, they begin by downloading a pre-baked playbook from Ansible Galaxy. Admittedly, it's a wonderful resource for accomplishing elaborate tasks with minimal effort. And, although I'm normally a big fan of learning by picking apart the work of others, in the case of Ansible, starting with roles and playbooks can be a big mistake. The ad hoc mode of Ansible is so powerful, it's all some people ever need.

For example, let's say you need to install Firefox on a lab full of Linux workstations. There are a few ways you could accomplish the task. You could SSH into each workstation one by one and type:

sudo apt-get update

sudo apt-get install firefox (type "y" when asked about dependencies)

exit

You could make that a one-liner by adding a few semicolons:

sudo apt-get update; sudo apt-get -y install firefox; exit

But that still requires a login to each computer. So assuming you have SSH keys set up, and sudo without password, you could write a quick Bash script to do this on the entire lab. Note: This is how I did my job for 20 years. Bash scripts were like the Swiss Army Knife of the sysadmin world.

#!/bin/bash

for i in {1..50};

do

ssh user@192.168.1.$1 "sudo apt-get update; sudo apt-get -y install firefox"

done

This method has many downsides: There's no error checking. There's no log of successes and failures. This exact example works only if the computers have incremental IP addresses. Compare that "hack" of a solution to accomplishing the same with Ansible:

ansible cadlab -b -m apt -a "update_cache=yes name=firefox state=latest"

The only preliminary work required is a simple group definition of what the "cadlab" group consists of based on hostnames or IP addresses. And that group definition is simply a text file with a list of those IP addresses!

Don't get me wrong—Ansible playbooks are incredibly powerful, and they can accomplish amazing things. If you need to create configuration files, execute commands based on conditional statements, or build a multi-step installation process, playbooks are the perfect vehicles. But for day-to-day operations on multiple servers, I find myself using Ansible constantly. The ad hoc mode is often overshadowed by playbooks and roles, but it is a powerful tool that can be fully implemented in minutes. So if you're interested in Ansible, don't skip over the ad hoc features it provides. Most days, they're all I use!

Tags
User profile image.
Shawn Powers has been an IT trainer for CBT Nuggets (www.cbtnuggets.com) since 2009, specializing in Linux, Chef, and integrating multiple platforms for larger networks. He recently released an advanced online Linux certification training course (LPIC-2) in December 2016.

1 Comment

Great article. I used to be a sysadmin and this would have been a big help.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.