9

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67

2 Answers2

4

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • That 's just what I'm using now, but it doesn't seem a good solution to me. But your answer reassures me that most probably there's no better way. – Nick Volynkin Dec 14 '15 at 07:44
  • 1
    @NickVolynkin yes, I agree that this is not the best solution. May be you can try to automate the generation of package number so that developer does not need to update that manually. – taskinoor Dec 14 '15 at 09:20
  • sure, that seems to be the right place to solve the problem. – Nick Volynkin Dec 14 '15 at 09:21
  • 2
    Yes - a way to forcibly uninstalls and reinstalls something that looks like the same package in the same task goes against the philosophy of Ansible, which is that it looks at the state of a system, and only makes changes where the state differs from the one a playbook defines. – nikobelia Dec 14 '15 at 18:14
  • Agreed on package numbers - a forced, unconditional reinstall would still be performed always, thus it would not be possible to achieve a state even if such a feature was present in Ansible. – Richlv Jun 01 '18 at 07:29
  • This is very risky with kernel and other essential packages like Grub. – Onlyjob Jul 30 '21 at 01:51
2

Unfortunately I don't see any possibility for a "reinstall" with the apt package module.

The only possibility is the one already mentioned in the question via shell or better via command module.

- name: Reinstall of package by command.
  command: "apt --reinstall install package"

With the apt module you only have the possibility to do an uninstall (state=absent) followed by a new install (state=present). I don't see a problem with the idempotent approach of Ansible, if you use an appropriate condition via when. For example:

- name: Reinstall of package by uninstall and new install.
  apt:
    name: package
    state: "{{ item }}"
  with_items: ['absent', 'present']
  when: reinstall_is_required

But: An uninstall and a new install can be very risky depending on the package, also this is not the same as a reinstall like "apt --reinstall install package".

I faced the same problem of needing the option of a reinstall.

I installed an application and in the config of the application I changed the execution user for this application. Reinstall changes the owners for all files and folders accordingly.

  • Uninstall and new install: Uninstall removes all config files (including the one I changed) and the new install creates the package default config files.

  • Reinstall via apt: The config files remain unchanged (are not overwritten by the package default config) and the configuration of the application is applied with the customized config.

For this reason, the reinstall option of apt is the only option for me.

phanaz
  • 1,188
  • 8
  • 17