DEV Community

Spacelift team for Spacelift

Posted on • Originally published at spacelift.io

Ansible Yum Module : Installing & Removing Packages

Ansible is a widely used open-source automation tool designed to streamline infrastructure configurations and management. It is primarily known for simplifying repetitive day-to-day tasks such as configuration management, application deployment, orchestration, cloud provisioning, and ensuring security and compliance.

Ansible offers an extensive library of modules that you can utilize to accomplish your tasks. Modules are basically units of code capable of executing system commands or managing system resources. The modules can be run directly on remote hosts or through playbooks. You also have the flexibility to customize these modules to suit your own needs.

What is the Ansible yum module?

The Ansible yum module manages packages on Linux systems that use the yum package manager, such as CentOS, Red Hat Enterprise Linux (RHEL), and Fedora. This module can perform tasks you would typically run through the Linux command line, like installing, updating, and removing software packages. It helps ensure software management is consistent and properly configured across your environment.

Using this module in Ansible ensures consistent software versions, managed dependencies, and always up-to-date systems with the latest security patches. The module also supports advanced operations, such as enabling or disabling specific repositories, managing package versions, handling package groups, and providing full control over all the software installed on your Linux machines that use the yum package manager.

Key features of the Ansible yum module

  • Package management: The yum module allows you to install packages by specifying the package name and desired state (e.g., present, latest, or absent).
  • Version support: You can install specific versions of packages by adding the version number to the package name. 
  • Updating packages: This module can also update all installed packages or specific ones. Use the wildcard * to update all packages or specify individual packages as needed.
  • Conditional logic: You can apply conditional logic based on the operating system family to ensure compatibility across different distributions.
  • Repository management: While the yum module handles packages, Ansible provides a separate yum_repository module for managing yum repositories, which is essential for sourcing packages.
  • Idempotency: The module is designed to be idempotent, meaning that running it multiple times won't change the system if it's already in the desired state. For example, if a package is already installed, Ansible will recognize this and will not attempt to reinstall it.

Let's go through some example scenarios where you can use the Ansible yum module.

Installing a single package with the Ansible yum module

One of the most common tasks in an Ansible playbook is installing a single yum package. This is especially useful when you need to ensure a specific software package is installed on all the machines in your environment.

Let's install the latest version of a single package:

- name: Install Apache Package
  yum:
    name: httpd
    state: latest
Enter fullscreen mode Exit fullscreen mode

To install a specific version of a single package, run:

- name: Install Apache Package - Version 2.2.29
  yum:
    name: httpd-2.2.29-1.4.amzn1
    state: present
Enter fullscreen mode Exit fullscreen mode

Installing multiple packages with the Ansible yum module

The Ansible yum module also allows you to install multiple packages with a single task in a playbook. 

In the example below, we will install the Apache, Vim, and Git packages using a single task.

- name: Install Packages
  yum:
    name:
      - nginx
      - postgresql
      - postgresql-server
    state: present
Enter fullscreen mode Exit fullscreen mode

Removing a single package with the Ansible yum module

Removing packages with the yum module is straightforward and very similar to installing them. Simply change the state from present to absent. This instructs Ansible to uninstall the specified package from the target system.

- name: Remove Apache package
  yum:
    name: httpd
    state: absent
Enter fullscreen mode Exit fullscreen mode

Removing multiple packages with the Ansible yum module

To uninstall multiple packages using the Ansible yum module, you can create a playbook that lists the packages you want to remove. 

The following task allows you to easily uninstall multiple packages at once:

- name: Remove Multiple Packages
  yum:
    name:
      - nginx
      - postgresql
      - postgresql-server
    state: absent
Enter fullscreen mode Exit fullscreen mode

💡 You might also like:

Install a package from a remote repository

Using a remote repository to host your applications has become increasingly common over the years. You can use the enablerepo option to install specific packages from this repository. This feature allows you to temporarily enable a repository for the duration of the installation task, which is particularly useful when you want to install packages from a repository that is not enabled by default on the system.

To install a single package from the remote repository, use the following command:

- name: Install package from a single remote repository
  yum:
    name: httpd
    enablerepo: testing
    state: present
Enter fullscreen mode Exit fullscreen mode

You can take it a step further and install a package that could be in a couple of repositories with the following:

 

- name: Install package from multiple repositories
  yum:
    name: httpd
    enablerepo: "testing1,testing2"
    state: present
Enter fullscreen mode Exit fullscreen mode

When your package has multiple versions in the remote repository, you can specify the version number in the package name:

 

- name: Install a version of a package from a remote repository
  yum:
    name: httpd-2.2.29-1.4.amzn1
    enablerepo: testing
    state: present
Enter fullscreen mode Exit fullscreen mode

Installing packages with the yum module using conditionals

When using the Ansible yum module to install packages with conditionals, you can use the when clause to control when a task should be executed. 

In the following example, we will use conditionals to install the yum package only when it detects a specific software is already installed:

 

- name: Install php-mysql if php is installed
  yum:
    name: php-mysql
    state: present
  when: ansible_facts.packages['php'] is defined
Enter fullscreen mode Exit fullscreen mode

Most of the time, your environment will include various Linux distributions. In such cases, you might want to use conditionals to ensure that the Ansible task doesn't run on the wrong Linux server with a different distribution. You can use the following example:

- name: Install firewalld on CentOS/RHEL only
  yum:
    name: firewalld
    state: present
  when: ansible_facts['os_family'] == "RedHat"
Enter fullscreen mode Exit fullscreen mode

"Typically, you organize your Linux servers based on their roles, such as web servers, databases, app servers, etc. In these scenarios, you might want to set up conditional dependencies to install specific applications on servers with particular roles. 

The example below demonstrates how to install the mysql-server package only on servers designated as databases:

 - name: Install MySQL on database servers only
   yum:
     name: mysql-server
     state: present
   when: "'database' in group_names"
Enter fullscreen mode Exit fullscreen mode

Sometimes, you may also need to install a package that requires a configuration file to run properly. 

In the example below, we use a conditional statement to check if the file exists in the directory before proceeding with the package installation:

- name: Install custom-software if config file exists
  yum:
    name: custom-software
    state: present
  when: ansible.builtin.stat(path='/etc/custom-config.conf').stat.exists
Enter fullscreen mode Exit fullscreen mode

We've only covered a few scenarios for using conditionals to install a yum package, but there are many other ways you can customize and adapt it to suit your needs.

Using the Ansible yum module to upgrade packages

The Ansible yum module offers a simple way to upgrade all the packages in your environment. Setting the state to latest will automatically upgrade either a single package or multiple packages. 

The example below shows how to upgrade a single package:

- name: Upgrade a single Apache package to the latest version
  yum:
    name: httpd
    state: latest
Enter fullscreen mode Exit fullscreen mode

With the following, you can upgrade multiple packages to the latest version:

- name: Upgrade multiple packages to the latest versions
  yum:
    name:
      - httpd
      - git
      - vim
    state: latest
Enter fullscreen mode Exit fullscreen mode

You can also upgrade all packages on your machine. However, this should be done with caution:

- name: Upgrade all packages to the latest versions
  yum:
    name: "*"
    state: latest
Enter fullscreen mode Exit fullscreen mode

How to check if the packages are installed?

You can use a combination of the yum module and the debug module to check if a package is installed across your environment using the yum module in Ansible.

In this example, we add the debug option to check if a single package is installed across your environment:

- name: Check if Apache is installed
  debug:
    msg: "httpd is installed"
  when: ansible_facts.packages['httpd'] is defined
Enter fullscreen mode Exit fullscreen mode

Use the following loop to check if multiple packages are installed:

- name: Check if multiple packages are installed
  debug:
    msg: "{{ item }} is already installed"
  loop:
    - httpd
    - vim
    - git
  when: item in ansible_facts.packages
Enter fullscreen mode Exit fullscreen mode

Conditionals allow you to take action based on the outcome of a conditional statement. In the example below, the yum module will install nginx only if it is not already installed on the machine:

- name: Install nginx only if is not installed
  yum:
    name: nginx
    state: present
  when: "'nginx' not in ansible_facts.packages"
Enter fullscreen mode Exit fullscreen mode

How can Spacelift help you with Ansible projects?

Spacelift's vibrant ecosystem and excellent GitOps flow can greatly assist you in managing and orchestrating Ansible. By introducing Spacelift on top of Ansible, you can then easily create custom workflows based on pull requests and apply any necessary compliance checks for your organization.

Another great advantage of using Spacelift is that you can manage different infrastructure tools like Ansible, Terraform, Pulumi, AWS CloudFormation, and even Kubernetes from the same place and combine their Stacks with building workflows across tools.

Our latest Ansible enhancements solve three of the biggest challenges engineers face when they are using Ansible:

  • Having a centralized place in which you can run your playbooks
  • Combining IaC with configuration management to create a single workflow
  • Getting insights into what ran and where

Provisioning, configuring, governing, and even orchestrating your containers can be performed with a single workflow, separating the elements into smaller chunks to identify issues more easily.

If you want to learn more about using Spacelift with Ansible, check our documentation, read our Ansible guide, or book a demo with one of our engineers.

Key points

The Ansible yum module is essential for automating package management on systems that use YUM, making your tasks more efficient and flexible compared to manual command-line execution.

The idempotent nature of this package manager module also ensures that tasks are only performed when necessary, preventing redundant operations from taking place and minimizing the risk of disrupting existing configurations. This feature is very helpful when you have a complex environment that requires you to maintain stability and uniformity.

Written by Faisal Hashem.

Top comments (0)