Glauco Leme
Glauco Leme

Reputation: 23

Ansible: install package only if is the newest version

Introduction: The objetive is to install and configure a package into a on premise server (without internet connection).

I already achieved a task to install using a .deb file and the apt module, and also configure the service using the lineinfile module to edit the package configuration files.

The problem: if the server already has an old version!

I'm able to get the current version installed using the command:

dpkg-query -f '${Version}' -W packageName`

and I also developed an regex to get the number to compare.

But now I'm stuck! =/

Question: How can I use the regex to compare the current version number with the version number to be installed?

I already read the filter documentation, but it did not rang any bells. >.<

Uninstall and install is also an option but it doesn't seems the right one.

Upvotes: 0

Views: 2239

Answers (1)

G_G
G_G

Reputation: 153

I cannot post a comment due to <50 rep at this point so I'll try to elaborate as much as I can. I hope this does answer your question, please comment if not.

If the issue is with installing when an older version is already installed, you can use the "only_upgrade" option of the apt module:

shell: ansible-doc apt
- only_upgrade
        Only upgrade a package if it is already installed.
        [Default: no]
        type: bool
        version_added: 2.1

Then, when adding a register var to this action, you'll know if the packages was indeed upgraded, and assuming you need to push new config files due to version change, you have what you need. The flow would be:

- name: Attempt upgrade first
   apt: 
     deb: path/to/file.deb
   only_upgrade: True
   register: was_upgraded

- name: Install the package on a server that never had it
  apt:
    deb: path/to/file.deb
  when: was_upgraded.false

Upvotes: 4

Related Questions