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 OverflowShort 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
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.
How do I specify a version for a yum package in ansible playbook?
Best way to check for installed yum package/rpm version in Ansible and use it - Stack Overflow
yum module unable to find a specific version of a package X
Using yum module to install latest or defined version of a package - Ansible Project - Ansible
Currently I am trying to use ansible to install maven on my TC agents. I do the following in my role.
- name: install maven
yum:
name: maven
state: present
update_cache: yes
This installs maven 3.0.5, but I need 3.2.5 or higher. How would I specify the version. I just copied this command and I am new to ansible so I am not sure what state:present means either.
I just want to update this old discussion to point out that there is now a package module that makes this more straightforward
- name: get the rpm or apt package facts
package_facts:
manager: "auto"
- name: show apache2 version
debug: var=ansible_facts.packages.apache2[0].version
I think more native ansible way would be:
- name: get package version
yum:
list: package_name
register: package_name_version
- name: set package version
set_fact:
package_name_version: "{{ package_name_version.results|selectattr('yumstate','equalto','installed')|map(attribute='version')|list|first }}"
Ansible yum module already provides a solution for this problem. The path to the local rpm file on the server can be passed to the name parameter.
From the Ansible yum module documentation:
You can also pass a url or a local path to a rpm file. To operate on several packages this can accept a comma separated list of packages or (as of 2.0) a list of packages.
The proper steps to do this would be something like this:
- name: Copy rpm file to server
copy:
src: package.rpm
dest: /tmp/package.rpm
- name: Install package.
yum:
name: /tmp/package.rpm
state: present
Actually the yum module can install an RPM directly from a given URL:
- name: Remote RPM install with yum
yum: name=http://example.com/some_package.rpm