Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force-reinstall a package with Ansible?

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?

like image 878
Nick Volynkin Avatar asked Dec 14 '15 05:12

Nick Volynkin


People also ask

How do I install Ansible packages?

Package installation is a relatively simple task and only requires two elements. The state option instructs Ansible to check whether or not some package is present on the system, and the name option lists which packages to look for. Ansible deals in machine state, so module instructions always imply change.

How do you check if a package is installed on a remote system using Ansible?

Have a self-written letsencrypt role (see the Prometheus: RTFM blog monitoring set up with Ansible – Grafana, Loki, and promtail post). Before running the Let's Encrypt client to obtain a new certificate – need to check if NGINX is installed on a remote host.

Which module is used to install a package in Ansible?

You can use the DNF module to manage the actual installation and a YAML-based Ansible playbook to distribute the installation instructions to your managed nodes.

How do I remove packages from Ansible playbook?

If possible, you can destroy the machine using vagrant destroy and create/provision a new one. If it is not possible to destroy the machine and provision a new one then you can add an ansible task to remove the installed package. Using absent as the state will remove the package. Save this answer.


2 Answers

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
like image 117
taskinoor Avatar answered Sep 20 '22 00:09

taskinoor


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.

like image 24
phanaz Avatar answered Sep 21 '22 00:09

phanaz