Bootstrap FreeKB - Ansible - Manage packages using the yum module
Ansible - Manage packages using the yum module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

There are a few modules that can be used to manage packages.

It often make sense to use the ansible.builtin.package module because the package module should be able to be used against any Linux distribution. On the other hand, the other package management modules are can only be used against certain distributions. For example, the dnf module can only be used against Red Hat distributions (CentOS, Fedora, Red Hat).

However, be aware that the ansible.builtin.package module may fail fatal with the following if the target server does not have Python 2. Version 2 of Python is deprecated, and often modern Linux distributions only have Python 3, so for this reason, it's probably best to use the dnf module.

fatal: []: FAILED! => {"changed": false, "msg": "The Python 2 yum module is needed for this module. If you require Python 3 support use the `dnf` Ansible module instead."}


On a Red Hat distributions (CentOS, Fedora, Red Hat), if you need to create a .repo file, such as /etc/yum.repos.d/, the yum_repository module can be used to create the .repo file.

- hosts: all
  - name: add the /etc/yum.repos.d/epel.repo
      name: epel
      description: EPEL repo


In this example, if the wget package is not installed on the system, the latest version of wget will be installed. If wget is already installed, wget will be updated to to the latest version.

- name: yum update wget
    name: wget
    state: latest


Install / Upgrade multiple packages

You could also install or upgrade multiple packages at once, like this.

- name: yum install wget and bzip2
    name: ['wget', 'bzip2']
    state: latest


Or like this.

- name: yum install wget and bzip2
      - wget
      - bzip2
    state: latest


Update all packages

The wildcard character can be used to update all installed packages on the managed node.

- name: update all packages - this is like running the 'yum update' command
    name: "*"
    state: latest


On the managed node, the ps command could be used to see that the yum update command is being run.

[root@server1 ~]# ps -ef | grep yum
root      9111  9011  0 07:15 pts/0    00:00:00 /bin/sh -c /usr/bin/python /root/.ansible/tmp/ansible-tmp-1612617280.7864397-7023-219484007335662/ && sleep 0
root      9114  9111  0 07:15 pts/0    00:00:00 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1612617280.7864397-7023-219484007335662/
root      9172  9114 27 07:15 pts/0    00:00:27 /usr/bin/python /usr/bin/yum -d 2 -y update
root     18075  9233  0 07:17 pts/1    00:00:00 grep --color=auto yum


Package successfull installed or updated

If the package was successfully installed or updated, the task should have a status of "changed".

PLAY [all]

TASK [Gather Facts]
ok: []

TASK [install or upgrade wget]
changed: []

PLAY RECAP   : ok=2  changed=1  unreachable=0  failed=0


Lastest version of package already installed 

If the latest version of the package is already installed on the managed node (e.g. target system), the "install or upgrade wget" task should have a status of "ok".

PLAY [all]

TASK [Gather Facts]
ok: []

TASK [install or upgrade wget]
ok: []

PLAY RECAP   : ok=2  changed=0  unreachable=0  failed=0


Become (root, sudo)

Typically, only root can use yum to install packages. The become module can be used to become root.



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee


Add a Comment

Please enter 65f377 in the box below so that we can be sure you are a human.