Bootstrap FreeKB - Ansible - Manage packages using the dnf module
Ansible - Manage packages using the dnf 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 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/epel.com, the yum_repository module can be used to create the .repo file.

---
- hosts: all
  tasks:
  - name: add the /etc/yum.repos.d/epel.repo
    yum_repository:
      name: epel
      description: EPEL repo
      baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
...

 

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: update wget
  dnf:
    name: wget
    state: latest

 


Install / Upgrade multiple packages

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

- name: update wget and bzip2
  dnf:
    name: ['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 'dnf update' command
  dnf:
    name: "*"
    state: latest

 

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

[root@server1 ~]# ps -ef | grep dnf 
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/AnsiballZ_dnf.py && 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/AnsiballZ_dnf.py
root      9172  9114 27 07:15 pts/0    00:00:27 /usr/bin/python /usr/bin/dnf -d 2 -y update
root     18075  9233  0 07:17 pts/1    00:00:00 grep --color=auto dnf

 


Install a Package Group

The dnf group list command with the -v or --verbose flag will return the availabe and installed package groups, listing the package group name and package group ID in parenthesis.

dnf group list --verbose

 

Something like this should be returned.

Available Groups:
   3D Printing (3d-printing)
   Administration Tools (admin-tools)
   Audio Production (audio)
   Authoring and Publishing (authoring-and-publishing)
   C Development Tools and Libraries (c-development)
   Cloud Infrastructure (cloud-infrastructure)
   Cloud Management Tools (cloud-management)
   Compiz (compiz)
   Container Management (container-management)
   D Development Tools and Libraries (d-development)
   Design Suite (design-suite)
   Development Tools (development-tools)
   Domain Membership (domain-client)
   Fedora Eclipse (eclipse)

 

The @ characeter is used to install a package group. Here is how you would install the Administration Tools package group.

- name: install or update the Administration Tools package group
  dnf:
    name: "@Administration Tools"
    state: latest

 


Clear DNF cache

The dnf module does not have an option to clear the dnf cache, which means to remove the files below /var/cache/dnf. If you want to clear the dnf cache, the shell or command modules can be used.

- name: clear dnf cache
  command: dnf clean all

 


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: [server1.example.com]

TASK [install or upgrade wget]
changed: [server1.example.com]

PLAY RECAP
server1.example.com   : 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: [server1.example.com]

TASK [install or upgrade wget]
ok: [server1.example.com]

PLAY RECAP
server1.example.com   : ok=2  changed=0  unreachable=0  failed=0

 


Become (root, sudo)

Typically, only root can use dnf 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



Comments


Add a Comment


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