Ansible package module autodetect your OS default package manager (e.g yum, apt) from existing facts.

The fact environment variable which stores is "ansible_pkg_mgr".

Here is a command for same. ansible localhost -m setup | grep ansible_pkg_mgr.

If you are using multiple OS in your environment, then instead of specifying package manager you should use package over yum or apt.

Answer from deepak on Stack Overflow
🌐
Ansible
forum.ansible.com › archives › ansible project
Ansible when logic to determine whether to use yum or dnf to install packages on ansible_os_family='RedHat' - Ansible Project - Ansible
February 18, 2020 - Hi, Up until now I’ve been making a logic decision with my ansible roles based upon the ansible_os_family, to run the appropriate module for yum (‘RedHat’) or apt (‘Debian’). With the introduction of CentOS 8, dnf has …
Discussions

(newbie) why different package managers (apt: or yum:)
You can use the package module for that... But for heterogenous environments, you'll have to have a few conditionals anyway.. I think the most prominent example is the "apache2" package on the debian-like systems that is called "httpd" in the red hat-ish world. More on reddit.com
🌐 r/ansible
7
5
January 10, 2023
Yum/DNF detection depends upon RHEL major version
Summary When using a custom distro based on RHEL, the major version reported can differ from the yum/dnf availability. This breaks the ansible.builtin.package module. The offending check is at http... More on github.com
🌐 github.com
33
March 10, 2022
Why is my Ansible playbook using dnf even when yum is explicitly specified? - Stack Overflow
I am trying to run a playbook in a Python3 venv that should install a package using yum. I have explicitly specified the ansible_python_interpretor to be used as python2. However, during playbook r... More on stackoverflow.com
🌐 stackoverflow.com
yum module
The dnf module does support dnf modules. It both enables and installs them. There are even tests and examples for this functionality: - name: Install a modularity appstream with defined stream and profile ansible.builtin.dnf: name: '@postgresql:9.6/client' state: present https://docs.ansible.com/ansible/latest/collections/ansible/builtin/dnf_module.html#examples https://github.com/ansible/ansible/blob/devel/test/integration/targets/dnf/tasks/modularity.yml More on reddit.com
🌐 r/ansible
11
7
November 16, 2022
🌐
Red Hat
access.redhat.com › solutions › 5181971
Why the Ansible module yum is using /usr/bin/dnf instead of /usr/bin/yum? - Red Hat Customer Portal
Why the Ansible module yum is using /usr/bin/dnf instead of /usr/bin/yum? # ansible -m setup rhel6.example.com -a 'filter=ansible_pkg_mgr' rhel6.example.com | SUCCESS => { "ansible_facts": { "ansible_pkg_mgr": "dnf", "discovered_interpreter_python": "/usr/bin/python" }, "changed": false }
🌐
Reddit
reddit.com › r/ansible › (newbie) why different package managers (apt: or yum:)
r/ansible on Reddit: (newbie) why different package managers (apt: or yum:)
January 10, 2023 -

good afternoon,

why ansible designers choose to implement different modules for different package managers (ex: apt: for debian and yum: for redhat) and so force use to create different platbooks, instead of creating some sort of unique instruction in ansible to install a package, leaving to ansible the task to understand the guest os (debian, redhat...) and use the right pkg manager?

thank you for your time.

🌐
Ansible
docs.ansible.com › ansible › latest › collections › ansible › builtin › dnf_module.html
ansible.builtin.dnf module – Manages packages with the dnf package manager — Ansible Community Documentation
Group removal doesn’t work if the group was installed with Ansible because upstream dnf’s API doesn’t properly mark groups as installed, therefore upon removal the module is unable to detect that the group is installed https://bugzilla.redhat.com/show_bug.cgi?id=1620324. While use_backend=yum and the ability to call the action plugin as ansible.builtin.yum are provided for syntax compatibility, the YUM backend was removed in ansible-core 2.17 because the required libraries are not available for any supported version of Python.
🌐
Techtransit
techtransit.org › home › exploring ansible.builtin.yum: managing package with ansible
Using Ansible Yum and DNF Modules for Package Management
August 22, 2025 - Use the yum module for Old CentOS and earlier or dnf for RHEL / CentOS 8+ or Stream Version. Both modules are used in Ansible playbooks to automate package installation across managed nodes.
🌐
Thenathan
thenathan.net › home › uncategorized › yum and dnf update and reboot with ansible
Yum and Dnf update and reboot with Ansible | TheNathan.Net
July 16, 2020 - Answer with 'YES'" default: "NO" private: no tasks: - name: Check Confirmation fail: msg="Playbook run confirmation failed" when: confirmation != "YES" - name: DNF update the system dnf: name: "*" state: latest when: ansible_facts['os_family'] == 'RedHat' and ansible_facts ['distribution_major_version'] >= '8' - name: Yum update the system yum: name: "*" state: latest when: ansible_facts['os_family'] == 'RedHat' and ansible_facts ['distribution_major_version'] <= '7' - name: Reboot required command: "/usr/bin/needs-restarting -r" register: reboot_required ignore_errors: True changed_when: Fals
🌐
Red Hat
learn.redhat.com › t5 › Automation-Management-Ansible › Can-Ansible-s-yum-dnf-modules-specify-a-repository › td-p › 19252
Solved: Can Ansible's yum/dnf modules specify a repository... - Red Hat Learning Community
July 28, 2021 - Hi, folks, I'm putting all our packages from non-Red Hat servers into Satellite rather than reach out to external repos, as a small security measure. As part of this effort, I'm also getting all the modules currently installed from (say) epel which should be coming from a Red Hat repo. (Ce...
Find elsewhere
🌐
OpenDev
opendev.org › openstack › tripleo-common › commit › da97fab4cb781b54405db52c7e00c89a8971bca1
ansible: replace yum module by package module when possible · da97fab4cb - tripleo-common - OpenDev: Free Software Needs Free Tools
From DNF release note: DNF is the next upcoming major version of yum, a package manager for RPM-based Linux distributions. It roughly maintains CLI compatibility with YUM and defines a strict API for extensions.
🌐
GitHub
github.com › ansible › ansible › issues › 77246
Yum/DNF detection depends upon RHEL major version · Issue #77246 · ansible/ansible
March 10, 2022 - Summary When using a custom distro based on RHEL, the major version reported can differ from the yum/dnf availability. This breaks the ansible.builtin.package module. The offending check is at http...
Author   mattchan-tencent
🌐
Stack Overflow
stackoverflow.com › questions › 75697131 › why-is-my-ansible-playbook-using-dnf-even-when-yum-is-explicitly-specified
Why is my Ansible playbook using dnf even when yum is explicitly specified? - Stack Overflow
If both dnf and yum are available, chances are it detected the first. In that case you need to force the backend (untested): Copy--- - name: Prepare vcsa installation hosts: localhost gather_facts: no connection: local pre_tasks: - name: Install 7z yum: name: - p7zip - p7zip-plugins state: latest use_backend: yum # (might be yum4 depending on your case) vars: ansible_python_interpreter: /usr/bin/python2
🌐
GitHub
github.com › ansible › ansible › issues › 80193
Unable to run dnf or yum modules in Python3 venv with Ansible 2.10 · Issue #80193 · ansible/ansible
March 10, 2023 - If you require Python 3 support use the `d nf` Ansible module instead.. The Python 2 yum module is needed for this module. If you require Python 3 support use the `dnf` Ansible module instead."} PLAY RECAP *************************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0 (python3) [root@TestVM-01 esxi-automation]# yum --version 4.7.0 Installed: dnf-0:4.7.0-11.el8.alma.noarch at Thu 26 Jan 2023 04:29:12 PM GMT Built : AlmaLinux Packaging Team <packager@almalinux.org> at Wed 12 Oct 2022 09:46:34 AM GMT Installed: rpm-0:4.14.3-24.el8_7.x86_64 at Fri 03 Feb 2023 01:10:22 AM GMT Built : AlmaLinux Packaging Team <packager@almalinux.org> at Wed 09 Nov 2022 03:50:55 PM GMT (python3) [root@TestVM-01 esxi-automation]#
Author   aeon-git
🌐
GitHub
gist.github.com › major › c5129ad2f94b9e85a6cf7d82f34cb207
Ansible: dnf vs yum · GitHub
August 11, 2017 - Ansible: dnf vs yum. GitHub Gist: instantly share code, notes, and snippets.
🌐
GitHub
github.com › ceph › ceph-ansible › issues › 520
use 'package' module instead of yum, apt and dnf · Issue #520 · ceph/ceph-ansible
May 2, 2016 - ceph / ceph-ansible Public · Notifications · You must be signed in to change notification settings · Fork 1k · Star 1.8k · New issueCopy link · New issueCopy link · Closed · Closed · use 'package' module instead of yum, apt and dnf#520 · Copy link · Labels ·
Author   leseb
🌐
Ansible
forum.ansible.com › archives › ansible project
Fedora 22: ansible_pkg_mgr: yum vs. dnf - Ansible Project - Ansible
July 24, 2015 - Hi, I'd like to get 'dnf' as an answer when asking a F22 system for ansible_pkg_mgr. What do you think about it? thanks, nusenu
🌐
TekNeed
tekneed.com › using-ansible-yum-module-others-to-manage-software
Using Ansible yum Module & Others To Manage Software & Sub
April 1, 2022 - [lisa@drsdev1 ansible]$ ...*************** ........................ The yum module can only be used if you are operating against managed hosts that are Red Hat Enterprise Linux....
🌐
SvennD
svennd.be › ansible-alma-dnf
Fix Ansible dnf/yum on RHEL8 - SvennD
May 12, 2025 - Ansible (-core) 2.17 and above requires a system Python version that supports future and respawn [1, 2]. However, the default Python 3.6 does not. Installing a newer Python version like 3.12 is possible, but it lacks the python3.12-dnf package. As a result, you can’t use dnf, package, or yum modules with newer Ansible versions on RHEL 8.
Top answer
1 of 2
5

You can install a module (which is essentially a group of packages) by adding a @ prior to the name of the module to the name parameter of the Ansible dnf module. It can be used like this:

- name: install the 'Development tools' package group
  dnf:
    name: '@Development tools'
    state: present
2 of 2
2

Semantics warning: Ansible uses the term "Module" to describe a specific command set or functionality. DNF/Yum4 uses the term "module" to describe groups of packages, features, and software vendors. These can easily confuse things in this question.

Per the official documentation for Ansible 2.9, there is no (documented) support for the DNF/Yum4 "modules" capabilities.

If you specifically need to use the 'modules' switch with DNF/Yum4, you can do so using the ansible command or shell modules, depending on how you use it. Some example code:

- name: Use dnf command to install postgresql 9.6 client
  command: dnf module install postgresql:9.6/client

This task will enable the postgreql module, 9.6 stream, and install the packages tagged as part of the 'client' profile.

Alternatively, you could try the following notation with the dnf module in Ansible; I have not tested it and cannot say if it will work or if Ansible can properly interpret the special characters.

- name: Use the ansible dnf_module to install postgresql 9.6 client
  dnf:
    name: '@postgresql:9.6/client'
    state: present

According to RHEL8 documentation, yum4/dns accepts this notation without explicitly calling the "module" switch. (Example near the bottom of section 4.6)