# Reinstall package if config dir is missing or different?

**URL:** <https://forum.ansible.com/t/reinstall-package-if-config-dir-is-missing-or-different/28820>\
**Category:** Ansible Project\
**Created:** [October 18, 2018, 2:01pm UTC](https://forum.ansible.com/t/reinstall-package-if-config-dir-is-missing-or-different/28820 "2018-10-18T14:01:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [October 18, 2018, 2:01pm UTC](https://forum.ansible.com/t/reinstall-package-if-config-dir-is-missing-or-different/28820/1 "2018-10-18T14:01:59Z")

</div>

As you know some packages install their config files/dir during  
the normal package installation (the first examples that popped in my  
mind are postfix, bind, and apache). I understand that if the  
configuration file I am editing using ansible is different than the  
one I providing (as file or template), it will be overwritten back  
into the desired state next time ansible is run. But, let's say one of  
the other files in that config file is missing. Or I was bored and  
deleted the entire config dir. How would I detect it? I take if I do  
detect I would then want to reinstall said package and then run my  
customization, right?

---

<div class="post-metadata">

**Author:** ![S\_C\_Rigler](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@S\_C\_Rigler](https://forum.ansible.com/u/S_C_Rigler)\
**Post date:** [October 18, 2018, 2:27pm UTC](https://forum.ansible.com/t/reinstall-package-if-config-dir-is-missing-or-different/28820/2 "2018-10-18T14:27:38Z")

</div>

Assuming you're using RPM, files that are marked "config" files in the  
package will not be overwritten by package upgrades (that's how you  
end up with ".rpmnew" files). But if you have missing files you can  
file them by looking at the output of "rpm -V". Then think of some  
creative way to trigger a reinstall of the package based on that.

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [October 25, 2018, 6:20pm UTC](https://forum.ansible.com/t/reinstall-package-if-config-dir-is-missing-or-different/28820/3 "2018-10-25T18:20:36Z")

</div>

> Assuming you're using RPM, files that are marked "config" files in the  
> package will not be overwritten by package upgrades (that's how you  
> end up with ".rpmnew" files). But if you have missing files you can  
> file them by looking at the output of "rpm -V". Then think of some  
> creative way to trigger a reinstall of the package based on that.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Let's make sure I understand what you are saying. If I have something like

- name: Install package Foo  
&nbsp;&nbsp;package:  
&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ foo\_packages }}"  
&nbsp;&nbsp;&nbsp;&nbsp;update\_cache: yes  
&nbsp;&nbsp;&nbsp;&nbsp;state: latest

even though /etc/foo.conf is part of the package, deleting it will not  
trigger a reinstall because it will not be overwritten during an  
upgrade, right? If that is the case, I would need to do something like

- name: Check if the Foo config is installed  
&nbsp;&nbsp;stat:  
&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ Foo\_config }}"  
&nbsp;&nbsp;&nbsp;&nbsp;register: foo\_config

and then trigger an reinstall if foo\_config.stat.exists == False

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [November 2, 2018, 4:15pm UTC](https://forum.ansible.com/t/reinstall-package-if-config-dir-is-missing-or-different/28820/4 "2018-11-02T16:15:04Z")

</div>

> \>  
> \> Assuming you're using RPM, files that are marked "config" files in the  
> \> package will not be overwritten by package upgrades (that's how you  
> \> end up with ".rpmnew" files). But if you have missing files you can  
> \> file them by looking at the output of "rpm -V". Then think of some  
> \> creative way to trigger a reinstall of the package based on that.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Let's make sure I understand what you are saying. If I have something like
> 
> - name: Install package Foo  
> &nbsp;&nbsp;package:  
> &nbsp;&nbsp;&nbsp;&nbsp;name: "{{ foo\_packages }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;update\_cache: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;state: latest
> 
> even though /etc/foo.conf is part of the package, deleting it will not  
> trigger a reinstall because it will not be overwritten during an  
> upgrade, right? If that is the case, I would need to do something like
> 
> - name: Check if the Foo config is installed  
> &nbsp;&nbsp;stat:  
> &nbsp;&nbsp;&nbsp;&nbsp;path: "{{ Foo\_config }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;register: foo\_config
> 
> and then trigger an reinstall if foo\_config.stat.exists == False

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NVM, package  
([https://docs.ansible.com/ansible/latest/modules/package\_module.html](https://docs.ansible.com/ansible/latest/modules/package_module.html))  
can't be forced. Yeah, I am trying to abstract as much as possible. I  
guess I will try to uninstall and then reinstall package manually

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 3, 2018, 10:44pm UTC](https://forum.ansible.com/t/reinstall-package-if-config-dir-is-missing-or-different/28820/5 "2018-11-03T22:44:04Z")

</div>

Example playbook below will do that.  
Initially it used the 'stat' module for each file, but since it's very  
common for packages to have hundreds of files that became too slow.  
So as a workaround I replaced it with a single command task that calls  
stat on all files at once.  
The Debian/RedHat test is very basic.  
Pass the package name as command line option ( -e package=httpd).

- name: Ensure package is correctly installed  
&nbsp;&nbsp;become: true  
&nbsp;&nbsp;hosts: all  
&nbsp;&nbsp;tasks:

&nbsp;&nbsp;&nbsp;&nbsp;- name: Ensure package is installed  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;package:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ package }}"

&nbsp;&nbsp;&nbsp;&nbsp;- name: Find package's files  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: "{{ (ansible\_os\_family == 'Debian') | ternary( 'dpkg  
--listfiles', 'repoquery --list') }} {{ package }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: pkg\_files  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed\_when: false

&nbsp;&nbsp;&nbsp;&nbsp;- name: Stat all files  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: "stat {{ pkg\_files.stdout\_lines | join(' ') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed\_when: false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;failed\_when: false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: stat\_all\_files

&nbsp;&nbsp;&nbsp;&nbsp;- name: Force reinstall  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: "{{ (ansible\_os\_family == 'Debian') | ternary( 'apt-get  
install --', 'yum -y ') }}reinstall {{ package }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;warn: false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: stat\_all\_files.rc != 0

Dick
