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 package: 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 package: 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 package: name: "*" state: latest
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 package: name: "@Administration Tools" state: latest
Clear DNF cache
The package module does not have an option to clear the cache, which means to remove the files below /var/cache/dnf or /var/cache/yum. 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.