Kevin C
Kevin C

Reputation: 5730

Ansible - Install package pinned to major versions

Actual package name in the repo is package-2.6.12-3.el7.x86_64.

The goal is to install a package with Ansible, to:

The repo can update packages from time to time, but I don't know when.

My thought was to install a package like this;

- name: Install package
  yum:
    name: package-2.6
    state: present

But the task fails, because package-2.6 is not in the repo. Whereas simply package works, but it is not future proof.


Update:

Seems wildcards * do work, eg name: "package-2.6*". Ensure to quote the wildcard.

Upvotes: 13

Views: 18041

Answers (2)

Zlemini
Zlemini

Reputation: 4963

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.

Upvotes: 1

Tom Manterfield
Tom Manterfield

Reputation: 7053

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

Upvotes: 8

Related Questions