Abonnez-vous au flux

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. ]


À propos de l'auteur

Seth Kenlon is a Linux geek, open source enthusiast, free culture advocate, and tabletop gamer. Between gigs in the film industry and the tech industry (not necessarily exclusive of one another), he likes to design games and hack on code (also not necessarily exclusive of one another).

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

Parcourir par canal

automation icon

Automatisation

Les dernières nouveautés en matière d'automatisation informatique pour les technologies, les équipes et les environnements

AI icon

Intelligence artificielle

Actualité sur les plateformes qui permettent aux clients d'exécuter des charges de travail d'IA sur tout type d'environnement

open hybrid cloud icon

Cloud hybride ouvert

Découvrez comment créer un avenir flexible grâce au cloud hybride

security icon

Sécurité

Les dernières actualités sur la façon dont nous réduisons les risques dans tous les environnements et technologies

edge icon

Edge computing

Actualité sur les plateformes qui simplifient les opérations en périphérie

Infrastructure icon

Infrastructure

Les dernières nouveautés sur la plateforme Linux d'entreprise leader au monde

application development icon

Applications

À l’intérieur de nos solutions aux défis d’application les plus difficiles

Virtualization icon

Virtualisation

L'avenir de la virtualisation d'entreprise pour vos charges de travail sur site ou sur le cloud