'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:

  • Ensure the point release is installed, such as package-2.6
  • Doesn't install major releases, such as package-3.0
  • Updates for minor releases, such as package-2.6.13-4

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 package-2.6*. Ensure to quote the wildcard.



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1