Save time with Ansible without writing a line of code

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.

Save time with Ansible without writing a line of code
Image by : 

opensource.com

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

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!

Topics

About the author

Shawn Powers - 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. Shawn has been teaching IT for more than a decade. Early in his career, he started a Cisco Academy for a local school district where he taught networking (CCNA & CompTIA A+) to high... more about Shawn Powers