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
Answer from Tom Manterfield on Stack Overflow
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.

🌐
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.
Discussions

How do I specify a version for a yum package in ansible playbook?
Most likely the version you want is not available via the repositories you have configured. It's possible the yum cache needs to be refreshed, try running "yum clean all" first, then check what is the latest version available. If it doesn't change, look at a better source repo for your packages. I'm assuming you have not locked the version of the package. Edit: I forget the "state: latest" option. If the package is already installed, "state: present" passes. Change it to "latest". More on reddit.com
🌐 r/ansible
3
3
November 18, 2022
Best way to check for installed yum package/rpm version in Ansible and use it - Stack Overflow
I've been getting my feet wet with Ansible (2.0.0.2) on CentOS 7. I'm trying to obtain a version from an installed rpm/yum package, but ran into a warning message when running the script. Ansible s... More on stackoverflow.com
🌐 stackoverflow.com
yum module unable to find a specific version of a package X
Note: in a normal installation without specifying docker_version works as expected and ansible just installs the latest packages. ... [user@host~]# yum --showduplicates list docker-ce | expand Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.advanced... More on github.com
🌐 github.com
2
September 10, 2019
Using yum module to install latest or defined version of a package - Ansible Project - Ansible
Hi all, I’m trying to work out the best way to either install the latest version or a specified version of a package dependent on a group_vars variable passed. That is, my group_vars/group contains: mypackage: 1.0 But it could be changed to mypackage: latest And I want package installation ... More on forum.ansible.com
🌐 forum.ansible.com
0
August 21, 2013
🌐
Google Groups
groups.google.com › g › ansible-project › c › tKVBel69OHc
Using yum module to install latest or defined version of a package
Software Engineer, AnsibleWorks, Inc. http://www.ansibleworks.com/ ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... Use state=latest and it will install the latest version. To install a specific version, ...
🌐
Spacelift
spacelift.io › blog › ansible-yum-module
Ansible Yum Module : Installing & Removing Packages
Version support: You can install specific versions of packages by adding the version number to the package name. Updating packages: This module can also update all installed packages or specific ones.
Published   October 17, 2025
🌐
Ansible
docs.ansible.com › ansible › 2.9 › modules › yum_module.html
yum – Manages packages with the yum package manager — Ansible Documentation
Unmaintained Ansible versions can contain unfixed security vulnerabilities (CVE). Please upgrade to a maintained version. See the latest Ansible community documentation . For Red Hat customers, see the Red Hat AAP platform lifecycle. ... Installs, upgrade, downgrades, removes, and lists packages and groups with the yum package manager.
🌐
Toptechskills
toptechskills.com › ansible-tutorials-courses › ansible-yum-module-tutorial-examples
Ansible yum Module Tutorial + Examples | TopTechSkills.com
May 18, 2024 - - name: ensure wget is installed yum: name: wget state: present update_cache: true become: true · Add the version and release information to the name parameter to install a specific version of a package, assuming the specific version is available ...
🌐
Ansible
docs.ansible.com › ansible › 2.3 › yum_module.html
Ansible Documentation — Ansible Community Documentation
June 29, 2022 - This documentation covers the version of Ansible noted in the upper left corner of this page. We maintain multiple versions of Ansible and of the documentation, so please be sure you are using the version of the documentation that covers the version of Ansible you’re using.
Find elsewhere
🌐
AnsiblePilot
ansiblepilot.com › articles › install-a-package-in-redhat-like-systems-ansible-module-yum
Install a package in RedHat-like systems - Ansible module yum
November 17, 2024 - For compatibility purpose you probably ended up using more the yum module than the DNF one, that is designed for modern operating systems. ... The parameter list is pretty wide but this four are the most important options. In the “name” parameter you are going to specify the name of the package or the specific version you would like to install.
🌐
GitHub
github.com › ansible › ansible › issues › 62081
yum module unable to find a specific version of a package X · Issue #62081 · ansible/ansible
September 10, 2019 - - name: Install desired version of docker yum: name: - "docker-ce={{ docker_version }}" - "docker-ce-cli={{ docker_version }}" - containerd.io state: present when: (docker_version != "") ... # ansible-playbook -i my-inventory my-playbook.yml ...
Author   tcarecolin
🌐
4sysops
4sysops.com › home › blog › articles › ansible yum module: install rhel/centos packages
Ansible yum module: Install RHEL/CentOS packages – 4sysops
August 30, 2023 - If it is already installed, Ansible will do nothing, as shown below: ... In this section, we will use state=latest to install the latest version of the package. Let's create a new playbook to install the latest version of Apache on the target server.
🌐
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 - Installs, upgrade, downgrades, removes, and lists packages and groups with the yum package manager. This module only works on Python 2. If you require Python 3 support see the ansible.builtin.dnf module. ... This module has a corresponding action plugin. The below requirements are needed on the host that executes this module. ... When used with a loop: each package will be processed individually, it is much more efficient to pass the list directly to the name option. In versions prior to 1.9.2 this module installed and removed each package given to the yum module separately.
🌐
FreeKB
freekb.net › Article
Ansible - Manage packages using the yum module
April 15, 2024 - If wget is already installed, wget will be updated to to the latest version. --- - hosts: all tasks: - name: update wget ansible.builtin.yum: name: wget state: latest ... ... You could also install or upgrade multiple packages at once, like this.
🌐
Ansible
forum.ansible.com › archives › ansible project
Using yum module to install latest or defined version of a package - Ansible Project - Ansible
August 21, 2013 - Hi all, I’m trying to work out the best way to either install the latest version or a specified version of a package dependent on a group_vars variable passed. That is, my group_vars/group contains: mypackage: 1.0 Bu…
🌐
LinuxBuz
linuxbuz.com › linuxhowto › ansible-yum-module
Ansible yum module: Install RHEL/CentOS Packages
July 11, 2025 - The Ansible yum module manages packages on systems that use the YUM package manager, such as CentOS, RHEL, RockyLinux, and Fedora. This module allows administrators to install, update, remove, or verify packages on target systems. Using the yum module, you can ensure specific packages and their versions ...
🌐
Ansible
docs.ansible.com › ansible › latest › collections › ansible › builtin › package_module.html
ansible.builtin.package module – Generic OS package manager — Ansible Community Documentation
- name: Remove the apache package ansible.builtin.package: name: "{{ apache }}" state: absent - name: Install the latest version of Apache and MariaDB ansible.builtin.package: name: - httpd - mariadb-server state: latest - name: Use the dnf package manager to install httpd ansible.builtin.package: name: httpd state: present use: dnf
🌐
Stack Exchange
devops.stackexchange.com › questions › 6404 › how-can-i-check-for-an-installed-packaged-version-on-multiple-linux-hosts
ansible - How can I check for an installed packaged version on multiple linux hosts? - DevOps Stack Exchange
--- - hosts: test2 become: true ... }}" ... I simple way would be to do ansible -i <inventory> <target group, lets' say some CentOS systems> -m shell -a "rpm -qa | grep <package name>"; that would display a list consisting of ...