How do I force-reinstall a package with Ansible?

16,562

The proper way is definitely to use the correct version number. But if you don't want to enforce that then the easiest workaround is to first remove the package and then install it again. That is effectively same as reinstalling.

- name: remove package
  apt: name=package_name state=absent

- name: install package
  apt: name=package_name state=present update_cache=yes
Share:
16,562
Nick Volynkin
Author by

Nick Volynkin

Read my Telegram channel on technical communication, developer relations and knowledge management: @docops. Currently: Developer Experience lead at Tarantool Member of program committee at KnowledgeConf, the conference about knowledge management in IT. Moderator-Pirate ♦ at Stack Overflow in Russian. Earlier: Technical documentation lead at Tarantool Technical writer & documentation engineer at Plesk. Reviewer at the community localization team of Stack Overflow in Russian. Translator at nadmosq.ru. Test Engineer at 2GIS. Java/C# developer in game development. Owner of a pet shop.

Updated on August 11, 2022

Comments

  • Nick Volynkin
    Nick Volynkin over 1 year

    I'm using Ansible to deploy .deb packages from a custom repository.

    Sometimes a developer can forget to change the package number, so the repository will have the new package with the old version. This is unnecessary, so I would like to always reinstall the package. How do I do that?

    There is the force=yes option for apt module. Ansible documentation says:

    If yes, force installs/removes.

    But that seems to be about force-accepting any warnings. At least when I turn it off, Ansible gets blocked with a warning about an untrusted source. (Both the repository and the servers are in the same intranet, so that should not be an issue)

    I could use this:

    - name: force-reinstall myservice
      shell: apt-get --reinstall install myservice
    

    But this way I cannot use other options for apt module, and Ansible gets blocked on warnings the same way.

    Is there a way to always reinstall a package and avoid blocking on any interactivity?