Reinstall package if config dir is missing or different?

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?

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.

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.

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

- name: Install package Foo
  package:
    name: "{{ foo_packages }}"
    update_cache: yes
    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
  stat:
    path: "{{ Foo_config }}"
    register: foo_config

and then trigger an reinstall if foo_config.stat.exists == False

>
> 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.

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

- name: Install package Foo
  package:
    name: "{{ foo_packages }}"
    update_cache: yes
    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
  stat:
    path: "{{ Foo_config }}"
    register: foo_config

and then trigger an reinstall if foo_config.stat.exists == False

      NVM, package
(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

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
  become: true
  hosts: all
  tasks:

    - name: Ensure package is installed
      package:
        name: "{{ package }}"

    - name: Find package's files
      command: "{{ (ansible_os_family == 'Debian') | ternary( 'dpkg
--listfiles', 'repoquery --list') }} {{ package }}"
      register: pkg_files
      changed_when: false

    - name: Stat all files
      command: "stat {{ pkg_files.stdout_lines | join(' ') }}"
      changed_when: false
      failed_when: false
      register: stat_all_files

    - name: Force reinstall
      command: "{{ (ansible_os_family == 'Debian') | ternary( 'apt-get
install --', 'yum -y ') }}reinstall {{ package }}"
      args:
        warn: false
      when: stat_all_files.rc != 0

Dick