Reinstall package if config dir is missing or different?

36 views
Skip to first unread message

Mauricio Tavares

unread,
Oct 18, 2018, 10:02:21 AM10/18/18
to ansible...@googlegroups.com
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?

S C Rigler

unread,
Oct 18, 2018, 10:28:01 AM10/18/18
to ansible...@googlegroups.com
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.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAHEKYV5iGg3cWk5NiwEXhystr1KMCBP3dQnWWKwiWUJt25qjEA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Mauricio Tavares

unread,
Oct 25, 2018, 2:21:00 PM10/25/18
to ansible...@googlegroups.com
On Thu, Oct 18, 2018 at 10:28 AM S C Rigler <rigl...@gmail.com> wrote:
>
> 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

> On Thu, Oct 18, 2018 at 9:02 AM Mauricio Tavares <raub...@gmail.com> wrote:
> >
> > 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?
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> > To post to this group, send email to ansible...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAHEKYV5iGg3cWk5NiwEXhystr1KMCBP3dQnWWKwiWUJt25qjEA%40mail.gmail.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAFbiokdVGZcQ9_m4zriZZKTQD1U0HTb_yegJBqh_njerFtpDuQ%40mail.gmail.com.

Mauricio Tavares

unread,
Nov 2, 2018, 12:15:25 PM11/2/18
to ansible...@googlegroups.com
On Thu, Oct 25, 2018 at 2:20 PM Mauricio Tavares <raub...@gmail.com> wrote:
>
> On Thu, Oct 18, 2018 at 10:28 AM S C Rigler <rigl...@gmail.com> wrote:
> >
> > 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

Dick Visser

unread,
Nov 3, 2018, 6:44:28 PM11/3/18
to ansible...@googlegroups.com
On Fri, 2 Nov 2018 at 17:15, Mauricio Tavares <raub...@gmail.com> wrote:

> (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
Reply all
Reply to author
Forward
0 new messages