Using Ansible to automate system maintenance

I have a home lab of around a dozen PCs (mostly physical, but some virtual) running Linux down in my basement. I like running lots of different Linux distros in my lab just to keep pace with the varying developments in the open source community, however constantly updating the machines to get all the latest bug fixes and security patches can be a tedious process. Here’s a short Ansible playbook I wrote a little while back to automate the update process across all my machines. Besides this file you also need an “inventory” file (a simple text file with all the hostnames or IPs of the machines to update), as well as the Ansible engine installed, which is available in virtually all Linux repos these days.

If you look at the playbook below you’ll see the main section of the code runs four different “Tasks”, each one corresponding with the command in Ansible to upgrade the Linux system software for a specific style of package manager (which tends to vary widely among mainstream Linux distros).

A link to this playbook as well as additional information on how to run it can also be found on my GitHub page here.

---
- hosts: all
  become: yes
  tasks:
    - name: install updates for Debian, Ubuntu, Mint
      apt: upgrade=dist update_cache=yes
      when: ansible_pkg_mgr == 'apt'
    - name: install updates for CentOS and RHEL 7
      yum: name=* state=latest update_cache=yes
      when: ansible_pkg_mgr == 'yum'
    - name: install updates for Fedora and RHEL 8
      dnf: name=* state=latest update_cache=yes
      when: ansible_pkg_mgr == 'dnf'
    - name: install updates for Arch and Manjaro
      pacman: upgrade=yes update_cache=yes
      when: ansible_pkg_mgr == 'pacman'
...

Leave a Reply

Your email address will not be published. Required fields are marked *