Ansible - yum module (install update package)

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.

  • apt module
  • dnf module
  • package module (preferred)
  • yum module

It almost always make sense to use the package module because the package module should be able to be used against any Linux distribution, such as a Debian distribution like Ubuntu or a Red Hat distribution like CentOS, Fedora, or Red Hat. 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).

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.



Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

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


Web design by yours truely - me, myself, and I   |   |