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?
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With