What's your favorite automation tool?
![What's your favorite automation tool?](/sysadmin/sites/default/files/styles/full/public/2020-08/tools-690038_1920%20Cropped.jpg?itok=NgBLi4Zk)
The eternal goal of every good sysadmin is to be replaced by a script.
It seems self-defeating, but sysadmins know that only when a technology is understood completely can it be automated, and new technology appears every year. By the time you've automated one thing, there's something new to learn and eventually automate.
Automation makes sense because computers are better and more reliable at repetition than humans. And because it's so popular a goal, there are dozens of tools to make automation possible, and most aren't mutually exclusive.
Which one's your favorite?
Ansible
Ansible provides IT automation through simple and consistent configuration files. It supports all the popular Linux package managers, Mac, and Windows.
The Ansible approach is a little different from custom automation you may have hacked together in the past. When you write a shell script to accomplish a task, you usually have to think about the steps, command by command. Ansible, however, looks after the machine state, abstracting away from you most of the steps required to reach a specific state.
Here's a simple example. Suppose you need a directory called example
to exist on each host. The old way, along with some very basic resiliency checks:
set -e
mkdir "${DEST}"/example || true
mkdir -p "${DEST}"/example/{doc,img,share,src} || true
[[ -d example ]] && chmod --recursive u=rwx,g=rwx,o=r example
for i in {img,doc,share,src}
do
[[ -d example/$i ]] || echo "example/$i was not created."
done
Compared to the Ansible way:
---
- name: "Create required directory structure"
hosts: localhost
tasks:
- name: "Create directory"
file:
path: "{{ item }}"
recurse: true
mode: "u=rwx,g=rwx,o=r"
state: directory
with_items:
- "doc"
- "img"
- "share"
- "src"
Ansible's version has a higher line count in this example, but all it does is describe the host's desired end state. You don't have to write the code to perform an action on a host, you only have to describe what the host looks like in the end. Ansible itself takes care of the details, regardless of what kind of host it's running on.
[ Need more on Ansible? Take a free technical overview course from Red Hat. Ansible Essentials: Simplicity in Automation Technical Overview. ]
Shell script
If you can accomplish a task by using a terminal, then you can automate it with a shell script. There's no conversion required between performing a task and automating that task when you use a shell script because one is just a transcript of the other. A shell script is an easy, approachable, and effective automation tool.
While shell scripts are perfect for providing instructions to computers, using them for automation can be difficult in a few scenarios.
For instance, if you're automating across different Linux and Unix distributions, then a shell script can require considerable modification. A command on one platform may work differently on another, an oddity that's often due to the difference between how GNU and BSD implement common utilities, or how differing shells behave.
The problem is worse when scripting entirely different platforms.
However, if you know your environment, and you know how to reliably proscribe the task you need to achieve, a shell script can be the quickest and easiest route to the solution.
Cron
Similar to shell scripts, the cron
subsystem is nearly intrinsic to Linux. It's the default scheduling mechanism that comes with every Linux system, and its syntax is standardized:
minute hour day month weekday task
In some implementations, there have been recent additions to this syntax, including shortcuts like @monthly
, @weekly
, @daily
, and @hourly
.
Regardless of what cron
binary your distribution provides, scheduling a task is as simple as editing your crontab
with this command:
$ crontab -e
And noting the time and day and task:
0 1 * */2 0 /usr/local/bin/rsync --rsh=ssh -av /opt example:/opt
This example specifies that an rsync
command runs on the 0-minute mark of 1 AM (01:00), regardless of day (*
), every two months (*/2
), on each Sunday (day 0
of the week).
The syntax takes some decoding, as your brain must process a system of increasing constraints, but it makes logical sense and is consistent across any platform that ships with a cron
application.
Of course, the integrity and resiliency of the task you schedule is up to you. The cron
system doesn't care whether a task succeeds or fails, but if you're looking for a central control panel to orchestrate any task across your network, this is an easy place to start.
[ Looking for more on system automation? Get started with The Automated Enterprise, a free book from Red Hat. ]
Systemd timers
Like cron
, systemd
is included in nearly any Linux distribution you install, making it an easy and obvious choice for scheduling tasks. You can use systemd timers to schedule and manage regular tasks on your local system, in containers, and across your network.
Unlike cron
, systemd
includes several utilities for linting and reporting on scheduled tasks. For example, you can get a report about tasks scheduled on Mondays:
$ sudo systemd-analyze calendar Mon
Original form: Mon
Normalized form: Mon 00:00:00
Next elapse: Mon 2020-08-10 00:00:00 NZDT
(in UTC): Sun 2020-08-09 12:00:00 UTC
Because it's part of systemd
, these timers also benefit from systemd
watchdogs and other systemd
features that monitor for success, safety, and overall stability.
Python
For Python hobbyists and programmers, using Python for automation provides all the benefits as a shell script does to longtime Linux admins. On top of that, it adds nearly universal support across platforms, because Python can run on practically anything and has code to account for system differences.
Of course, Python alone doesn't know to adjust for everything, so you still need a healthy dose of if
statements to verify which platform the script is running on. It does provide a robust level of abstraction you can use once you've determined what kind of system you're dealing with.
Python's flexibility also means you can invent new ways to manage systems well beyond the old framework of a Bash script scheduled with cron
.
There are Python tools to write a custom API to help you with common tasks, Python utilities to help you navigate a network, to configure hosts and containers, and much more.
Whether you're just converting from shell scripts or custom C code, Python can meet you halfway so you can manage your clients in whatever way you're most comfortable with.
Something else
Possible solutions for any given problem are nearly endless when it comes to computers and resourceful sysadmins.
How do you wrangle your network, cluster, or odd assortment of devices?
[ A free guide from Red Hat: 5 steps to automate your business. ]
![Factory](/sysadmin/sites/default/files/styles/card/public/2019-07/factory-conveyor-belt.jpg?itok=ACSE4z2d)
![Easing into automation with Ansible](/sysadmin/sites/default/files/styles/card/public/2019-09/DippingToes_0.jpg?itok=W4PY3_GV)
![Introducing your friends to automation](/sysadmin/sites/default/files/styles/card/public/2019-10/Handshake_LeGo.jpg?itok=4ZHQKcNJ)
![automatic car wash](/sysadmin/sites/default/files/styles/card/public/2019-09/auto_car_wash.jpg?itok=QzEIGKME)
![Author’s photo](/sysadmin/sites/default/files/styles/user_picture_square/public/pictures/2021-06/sethKenlon-headshot.jpg?itok=OyoGyrQS)
Seth Kenlon
Seth Kenlon is a UNIX geek and free software enthusiast. More about me