Let's say something like this is being returned.
fatal: [server1.example.com]: FAILED! => {"ansible_facts": {"pkg_mgr": "yum"}, "changed": false, "msg": ["Could not detect which major revision of dnf is in use, which is required to determine module backend.", "You should manually specify use_backend to tell the module whether to use the dnf4 or dnf5 backend})"]}
I got this when attempting to use the dnf module.
---
- hosts: all
tasks:
- name: update wget
ansible.builtin.dnf:
name: wget
state: latest
...
I SSH onto the server and found that it was using yum and did not have the dnf CLI.
~]$ which yum
/usr/bin/yum
~]$ which dnf
/usr/bin/which: no dnf in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin)
There are a few modules that can be used to manage packages.
- apt module
- dnf module
- package module
- package_facts module
- yum module
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 based on the package manager being used on the target server, and the version(s) of Python on the target server.
fatal: [server1.example.com]: 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."}
What I have found to be most stable is to use the ansible_pkg_mgr fact to determine if the package manager being used on the target server and to then use the module for the package manager being used on the target server.
---
- hosts: all
tasks:
- name: update wget using dnf
ansible.builtin.dnf:
name: wget
state: latest
when: ansible_pkg_mgr == 'dnf'
- name: update wget using yum
ansible.builtin.yum:
name: wget
state: latest
when: ansible_pkg_mgr == 'yum'
- name: update wget using apt
ansible.builtin.apt:
name: wget
state: latest
when: ansible_pkg_mgr == 'apt'
...
Did you find this article helpful?
If so, consider buying me a coffee over at