# What's your favourite automation tool? [POLL] * Ansible * Shell script * Cron * systemd timers * Python * Something else 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 favourite? ## 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 desired end state of the host. You don't have to write the code performing 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. ## 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](https://www.redhat.com/sysadmin/terminals-shells-consoles) behave. The problem is worse when scripting different platforms entirely. 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 instrinsic to Linux. It's the default scheduling mechanism that comes with every Linux system, and its syntax is well 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 distributions 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 2 months (``*/2``), on each Sunday (day 0 of the week). The syntax gets 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. ## 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](https://opensource.com/article/20/7/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-anolyze 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 can 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 a 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, but 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 enable you to [write a custom API](https://opensource.com/article/19/9/web-apps-sysadmins) to help you with common tasks, Python utilities to help you navigate a network and to configure hosts and containers, and much more. Whether you're just converting from shell scripts or custom C code, Python can meet you half way 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 to you wrangle your network, cluster, or odd assortment of devices?