automation

Ansible

A lot of system administration can be automated using Ansible. Of course it’s also possible to just use shell scripts to do the same.

The difference is not that big and depends a lot on how you want to work.

Ansible has one advantage though. You can re-run your playbooks and they will perform the same steps again, skipping those changes not necessary. This makes it possible to use it for checking your system as well.
Shell scripts would just blindly do what’s defined without “thinking”, which could cause problems.

Of course Ansible playbooks can also wreak havoc if not designed correctly.

Installation

First let’s get ansible installed. On OpenSuse it’s like this:

sudo zypper install ansible

Check the installed version with:

ansible --version

It should give you an output similar to the following:

ansible [core 2.17.4]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/jdoe/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.11/site-packages/ansible
  ansible collection location = /home/jdoe/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.10 (main, Sep 09 2024, 17:03:08) [GCC] (/usr/bin/python3.11)
  jinja version = 3.1.4
  libyaml = True

Check Ansible

To Check if the installation was successful we can try to run a command for the local machine.

Let’s just make a really simple playbook. Don’t worry about any of this for now, just come along for the ride.

Create a file named playbook.yml with the content:

---
- name: test
  hosts: localhost
  tasks:
    - name: ping
      ansible.builtin.ping:

Run it with:

ansible-playbook playbook.yml

You should now get an output like this:

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'

PLAY [test] ***********************************************************************************

TASK [Gathering Facts] ************************************************************************
ok: [localhost]

TASK [ping] ***********************************************************************************
ok: [localhost]

PLAY RECAP ************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

Nicely done. You just successful ran your first playbook.