Simplify infrastructure self-service with Blueprints

→ Register for the webinar

Ansible

Ansible Yum Module : Installing & Removing Packages

ansible yum module

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 we will cover in this guide:

  1. What is the Ansible yum module?
  2. How to install a single package 
  3. How to install multiple packages
  4. How to remove a single package
  5. How to remove multiple packages
  6. How to install a package from a remote repository
  7. How to install packages with the yum module using conditionals
  8. How to use the Ansible yum module to upgrade packages
  9. How to check if packages are installed?

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

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

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

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

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

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

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

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

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

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"

“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"

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

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

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

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

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

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

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"

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 easily create custom workflows based on pull requests and apply any necessary compliance checks for your organization.

Another advantage of using Spacelift is that you can manage different infrastructure tools like Ansible, OpenTofu, Terraform, Pulumi, AWS CloudFormation, and even Kubernetes from the same place and combine their stacks with building workflows across tools. Spacelift greatly simplifies and elevates your workflow for all of these tools, and the ability to create dependencies between stacks and passing outputs enables you to make end-to-end deployments with a small change.

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.

Manage Ansible Better with Spacelift

Spacelift helps you manage the complexities and compliance challenges of using Ansible. It brings with it a GitOps flow, so your infrastructure repository is synced with your Ansible Stacks, and pull requests show you a preview of what they’re planning to change. It also has an extensive selection of policies, which lets you automate compliance checks and build complex multi-stack workflows.

Learn more

The Practitioner’s Guide to Scaling Infrastructure as Code

Transform your IaC management to scale

securely, efficiently, and productively

into the future.

ebook global banner
Share your data and download the guide