Automate Unrar Installation Using Ansible

Automating the deployment of utility tools across your server fleet saves time and ensures environment consistency. This article provides a straightforward guide on writing and running an Ansible playbook to automate the installation of the unrar package across popular Linux distributions, including Debian, Ubuntu, and Red Hat-based systems.

Prerequisites

Before running the playbook, ensure you have:

  • Ansible installed on your control node.
  • SSH access configured to the target hosts with sudo privileges.
  • An inventory file containing your target host IP addresses or domain names.

The Multi-Distribution Playbook

Different Linux distributions require different repositories or package names to install unrar. For Debian-based distributions, unrar often resides in restricted or non-free repositories. On RHEL and CentOS distributions, it requires the EPEL (Extra Packages for Enterprise Linux) repository.

Create a file named install_unrar.yml with the following playbook:

---
- name: Automate Unrar Installation
  hosts: all
  become: true
  tasks:

    - name: Install unrar on Debian and Ubuntu
      ansible.builtin.apt:
        name: unrar
        state: present
        update_cache: yes
      when: ansible_os_family == "Debian"

    - name: Enable EPEL repository on RHEL/CentOS
      ansible.builtin.dnf:
        name: epel-release
        state: present
      when: ansible_os_family == "RedHat" and ansible_distribution != "Fedora"

    - name: Install unrar on RedHat/CentOS/Fedora
      ansible.builtin.dnf:
        name: unrar
        state: present
        update_cache: yes
      when: ansible_os_family == "RedHat"

    - name: Verify unrar installation
      ansible.builtin.command: unrar
      register: unrar_check
      failed_when: unrar_check.rc != 0 and unrar_check.rc != 7
      changed_when: false

Running the Playbook

Execute the playbook against your target inventory using the ansible-playbook command:

ansible-playbook -i inventory.ini install_unrar.yml

If you require privilege escalation with a password prompt, append -K to the command:

ansible-playbook -i inventory.ini install_unrar.yml -K

Verifying the Installation

To verify the installation across all hosts without logging in manually, run an Ansible ad-hoc command:

ansible all -i inventory.ini -m command -a "unrar"

A successful output will return the standard unrar usage help text, indicating the utility is properly installed and ready for extraction tasks.