Since ansible 2.5 there is an option update_only for yum (and since ansible 2.1 only_upgrade for apt) which installs latest version only if it was already installed on the system. So, instead of collecting a list of packages in another task, you can add the option.

- name: Update subset of packages.
  yum:
    name: "{{ item }}"
    state: latest
    update_only: yes
  with_items:
    - package1
    - package2

I am myself was searching the web and this article was found before I got to the official documentation. So I think it worse to be added here.

Answer from Dennis M. on serverfault.com
Discussions

yum module refuses to install package, saying it's already installed
I don't know if that would work on 1.9.1 or not. Details: Ansible version 1.9.2, target OS RedHat 6.0, internal repositories that mirror official ones. I don't know how they are set up exactly. The playbook, which is in roles/smbclient/tasks/main.yml, contains just: --- - name: "package" yum: name=samba-client state=present... More on github.com
🌐 github.com
49
November 26, 2015
yum state=latest doesn't install if not already installed
If I try to install a package which is not already installed using yum with state=latest: ansible -m yum -a 'pkg=hadoop-hdfs-namenode state=latest' box -Ksu$USER It does a yum update, which (stupid... More on github.com
🌐 github.com
9
April 27, 2012
php - Unable to install package in Ansible - Stack Overflow
Does Ansible's yum module install dependencies as well? ... So where does the claim "However, this package hasn't been installed on the server at all." come from? ... Save this answer. Show activity on this post. I suspect your issue is related to the difference in state: present vs state: latest. It looks like the yum module with state: present checks the rpmdb to see if the pkg exists and does nothing ... More on stackoverflow.com
🌐 stackoverflow.com
Ansible yum module to install a list of packages AND remove any other packages - Stack Overflow
I have to deal with new machines (same OS version on all) that have been previously managed manually by many different admins. The purpose is to use Ansible to make all these machines sharing the s... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › goldyfruit › f4f274be3144e6afca69
[ansible] Check via the yum module and a registered value if a package is installed or not · GitHub
... - name: check if "{{ package }}" is installed yum: list="{{ package }}" register: is_installed - name: install "{{ package }}" if not exist yum: name: "{{ package }}" state: latest when: (is_installed.results|length == 1) or (is_install...
🌐
Spacelift
spacelift.io › blog › ansible-yum-module
Ansible Yum Module : Installing & Removing Packages
- name: Check if multiple packages are installed debug: msg: "{{ item }} is already installed" loop: - httpd - vim - git when: item in ansible_facts.packages · Conditionals allow you to take action based on the outcome of a conditional statement. In the example below, the yum module will install nginx only if it is not already installed on the machine: - name: Install nginx only if is not installed yum: name: nginx state: present when: "'nginx' not in ansible_facts.packages"
Published   October 17, 2025
🌐
GitHub
github.com › ansible › ansible-modules-core › issues › 2559
yum module refuses to install package, saying it's already installed · Issue #2559 · ansible/ansible-modules-core
November 26, 2015 - I don't know if that would work on 1.9.1 or not. Details: Ansible version 1.9.2, target OS RedHat 6.0, internal repositories that mirror official ones. I don't know how they are set up exactly. The playbook, which is in roles/smbclient/tasks/main.yml, contains just: --- - name: "package" yum: name=samba-client state=present...
Author   bgdnlp
🌐
YouTube
youtube.com › waylon walker
Install package only if its missing | ansible - YouTube
Today we stop re-running ansible steps more than they need. Only if a package does not exist we will build or install it. I've set this up for MANY of my a...
Published   December 29, 2021
Views   1K
🌐
Ansible
docs.ansible.com › ansible › latest › collections › ansible › builtin › yum_module.html
ansible.builtin.yum module — Ansible Community Documentation
This redirect is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name yum even without specifying the collections keyword.
Find elsewhere
🌐
Ansible
docs.ansible.com › ansible › 2.9 › modules › yum_module.html
yum – Manages packages with the yum package manager — Ansible Documentation
- name: install the latest version ... state: present - name: Download the nginx package but do not install it yum: name: - nginx state: latest download_only: true · This module is guaranteed to have backward compatible interface changes going forward. [stableinterface] This module is maintained by the Ansible Core Team. [core] More information about Red Hat’s support of this module is available from this Red Hat Knowledge Base article. ... If you notice ...
🌐
Toptechskills
toptechskills.com › ansible-tutorials-courses › ansible-yum-module-tutorial-examples
Ansible yum Module Tutorial + Examples | TopTechSkills.com
May 18, 2024 - - name: ensure wget and ruby are installed yum: name: - wget - ruby state: present update_cache: true become: true · Set state: absent to remove a package. I suggest also setting autoremove: true to remove any dependencies that were installed originally, but are no longer required. - name: ensure wget is not installed yum: name: wget state: absent autoremove: true become: true
🌐
Ansible
docs.ansible.com › ansible › 9 › collections › ansible › builtin › yum_module.html
ansible.builtin.yum module – Manages packages with the yum package manager — Ansible Community Documentation
December 3, 2024 - The yum module does not support clearing yum cache in an idempotent way, so it was decided not to implement it, the only method is to use command and call the yum command directly, namely “command: yum clean all” https://github.com/ansible/ansible/pull/31450#issuecomment-352889579 · - name: Install the latest version of Apache ansible.builtin.yum: name: httpd state: latest - name: Install Apache >= 2.4 ansible.builtin.yum: name: httpd>=2.4 state: present - name: Install a list of packages (suitable replacement for 2.11 loop deprecation warning) ansible.builtin.yum: name: - nginx - postgre
🌐
GitHub
github.com › ansible › ansible › issues › 269
yum state=latest doesn't install if not already installed · Issue #269 · ansible/ansible
April 27, 2012 - Does contain only the package I'm trying to install. But then the logic goes · if not updates: cmd = "yum -c %s -d1 -y install '%s'" % (yumconf, pkgspec) else: cmd = "yum -c %s -d1 -y update '%s'" % (yumconf, pkgspec)
Author   jkleint
Top answer
1 of 2
6

Building up on @gary lopez answer to add security and performance.

First you will need to get an actual list of all packages you want to see installed on your final machine, including the default ones that come with the system. I assume that list will be in var yum_rpm

Once you have that, the next step is to get the list of currently installed packages on the machine. To create an actual list we can reuse:

  - name: Get installed packages
    yum:
      list: installed
    register: __yum_packages

  - name: Make installed packages a list of names
    set_fact:
      installed_packages: "{{ __yum_packages.results | map(attribute='name') | list }}"

From there, adding and removing is just a matter of making a difference on lists. The goal here is to avoid looping on the yum module package by package (because it is damn slow and listed as a bad practice on the module documentation page) and to make the install and remove operations in one go.

  - name: align packages on system to expected
    yum:
      name: "{{ item.packages }}"
      state: "{{ item.state }}"
    loop:
      - packages: "{{ yum_rpm | difference(installed_packages) }}"
        state: present
      - packages: "{{ installed_packages | difference(yum_rpm) }}"
        state: absent
    when: item.packages | length > 0

2 of 2
1

In the first task you need to use state: present. You could try this

vars:
  - yum_rpm:
    - tcpdump
    - tmux
    - psacct

tasks:
  - name: "Install all package in our list"
    yum:
      name: "{{ yum_rpm }}"
      state: present
      update_cache: no

  - name: Get packages installed
    yum:
      list: installed
    register: __yum_packages

  - name: "Remove any other unexpected package already installed"
    yum:
      name: "{{ item.name }}"
      state: absent
    with_items: "{{ __yum_packages.results }}"

But I recommend you validate packages to uninstall because you could uninstall some packages required for your OS.

🌐
DEV Community
dev.to › spacelift › ansible-yum-module-installing-removing-packages-4g04
Ansible Yum Module : Installing & Removing Packages - DEV Community
December 9, 2024 - - name: Check if multiple packages are installed debug: msg: "{{ item }} is already installed" loop: - httpd - vim - git when: item in ansible_facts.packages · Conditionals allow you to take action based on the outcome of a conditional statement. In the example below, the yum module will install nginx only if it is not already installed on the machine: - name: Install nginx only if is not installed yum: name: nginx state: present when: "'nginx' not in ansible_facts.packages"
Top answer
1 of 2
8

Short Generic Answer:

You should be able to just use wildcards *.

So just:

- name: Install package
  yum:
    name: package-2.6*
    state: latest 

Long Case Specific Answer:

I created a test server in AWS for your specific case and found that wildcards do indeed work (EC2 instance running CentOS 7, installing `mongodb-org-server-3.4.0*).

You do need to make sure you have properly configured the mongo repository first, but you said in the comments that you are able to download the package if you provide the full version number, which is unusual. Anyway, this is the minimal playbook I made and ran:

play.yml:

- hosts: all
  remote_user: centos
  tasks:
    - name: Add MongoDB repo for CentOS
      become: true
      copy:
        src: ./files/mongodb-org-3.4.repo
        dest: /etc/yum.repos.d/mongodb-org-3.4.repo
    - name: Install mongodb
      become: true
      yum:
        name: mongodb-org-server-3.4.0*
        state: latest # Works with 'present' too, but won't update versions

This playbook copies a local file for the repo config which looks like this (path is relative to the play.yml file):

files/mongod-org-3.4.repo:

[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
2 of 2
1

Not sure if applicable for your Yum package. But for Java Open JDK installations where both java-1.7.0 and java-1.8.0 packages are available for installation from my configured yum repos.

This will ensure the 1.7.x version is at the latest version, without ever installing 1.8.x.

- name: Install latest 1.7.x jdk
  yum:
    name: java-1.7.0-openjdk.x86_64
    state: latest

Actual version installed from the above is:

$ rpm -q java-1.7.0-openjdk.x86_64
  java-1.7.0-openjdk-1.7.0.121-2.6.8.1.el6_8.x86_64

In the case of MongoDB the package name is the same for the 2.x version and the 3.x version.

But there is one Yum repo file for the 2.x version and another for the 3.x version. https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/

So to ensure you get the latest 2.x version without ever moving to 3.x add the 2.x repo file to your target hosts and use the disable and enablerepo parameters in your ansible task for the install/update operation.

 - name: Ensure latest 2.x mongodb version is installed
   yum:
     name: mongodb-org
     disablerepo: "*"
     enablerepo: mongodb-org-2.6
     state: latest

Note: using disablerepo: "*" as mongodb packages also exist in other repos such as epel.

🌐
LinuxBuz
linuxbuz.com › devops › ansible-yum-module-examples
Ansible yum module: Install RHEL/CentOS Packages
July 11, 2025 - --- - name: Install Elasticsearch ... playbook configures all target hosts to use the Elasticsearch YUM repository by adding its URL and GPG key....
🌐
Readthedocs
ansible-tips-and-tricks.readthedocs.io › en › latest › os-dependent-tasks › installing_packages
Installing Software and Other Packages - Ansible Tips and Tricks
- name: install basic packages action: > {{ ansible_pkg_mgr }} name={{ item }} state=present update_cache=yes with_items: - vim - tmux - mosh · Please note that the modules for apt and yum have different options that make it impossible to use the method above of using the action: In order to do this, one must use the when: to perform their associated module per OS. yum does not have a update module option, so it basically check to see if ...