Host lists, facts and handlers

55 views
Skip to first unread message

Marc Haber

unread,
Mar 31, 2018, 2:35:54 PM3/31/18
to ansible...@googlegroups.com
Hi,

I do regularly have the problem that I want to manage directory
contents. To make things easier, we take an every day example such as
/etc/apt/sources.list.d on a Debian system. While I am aware that there
is a special apt module in ansible, I still chose to tackle this issue
"manually" because my ultimate goal is generic directory contents
management, not apt management. I chose apt as an example since that's
easier to reproduce for you.

I want:
- files written in the directory by ansible
- when a file ceases to be managed by ansible, the file should be
removed. This is made easier by the fact that the ansible-managed
files do match a certain glob, here: zda-*.
- If a file doesn't change, it should not be touched. This includes
that it is not desired that a file gets deleted and recreated with
the same contents in the same ansible run.
- Files placed into the directory manually by the local admin should
remain untouched.

Here is my code:
==> ./site.yml <==
---
- name: apply common configuration
hosts: all
remote_user: mh
become: "yes"
roles:
- common

- name: clean up after apt configuration
hosts: apt_action_hosts
remote_user: mh
become: "yes"
roles:
- zzhandleapt
==> ./roles/common/tasks/main.yml <==
---
- name: repos
import_tasks: repos.yml

==> ./roles/common/tasks/repos.yml <==
---
- name: search for sources.list.d files
find:
paths: "/etc/apt/sources.list.d"
patterns: "zda-*.list"
register: presentsourceslistfiles
- name: set up fact list for wanted sources.list.d entries
set_fact:
wantedsourceslistfiles:

- name: include repositories
tags:
repos
include_tasks:
"debian/sid/repos.yml"
==> ./roles/common/tasks/debian/sid/repos.yml <==
---
- name: create list of sid wantedsourceslistfiles
set_fact:
sidwantedsourceslistfiles:
- /etc/apt/sources.list.d/zda-sid-mc.list
- name: add sid files to wantedsourceslistfiles
set_fact:
wantedsourceslistfiles: "{{ wantedsourceslistfiles }} + {{ sidwantedsourceslistfiles }}"
- name: zda-sid-mc.list
tags:
- repos
- sid
copy:
dest: /etc/apt/sources.list.d/zda-sid-mc.list
owner: root
group: root
mode: 0644
content: |
deb http://debian.debian.zugschlus.de/debian/ sid main contrib
notify: apt update
==> ./roles/common/handlers/main.yml <==
---
- name: apt update
add_host:
name: "{{ inventory_hostname }}"
groups: apt_action_hosts
==> ./roles/zzhandleapt/tasks/main.yml <==
---
- name: repos
import_tasks: repos.yml

==> ./roles/zzhandleapt/tasks/repos.yml <==
---
- debug:
msg: "wantedsourcelistfiles {{wantedsourceslistfiles}}"
- name: delete sources.list.d files
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ presentsourceslistfiles.files }}"
when: item.path not in wantedsourceslistfiles

- name: apt update
command: apt update

==> ./hosts.yml <==
---
all:
vars:
dummy: dummy
children:
g_all:
hosts:
sid01:
sid02:

The idea is the following:
(1) build a list of all files that we might want to delete later
(2) roll out the files that we actually want, build a list of those
files
(3) later iterate through list (1) and delete all files that are not
in list (2).

Unfortunately, that does not seem to work. A possible reason might be
that the lists are not by-host, but per-ansible-run.

On both hosts, remove /etc/apt/sources.list.d/zda-sid-mc.list and touch
/etc/apt/sources.list.d/zda-stretch-mc.list. Expected is that ansible
removes the -stretch- files and places the -sid-files.

First ansible run:
[51/5036]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook --ask-become-pass --inventory=hosts.yml site.yml
SUDO password:

PLAY [apply common configuration] *******************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************
ok: [sid02]
ok: [sid01]

TASK [common : search for sources.list.d files] *****************************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : set up fact list for wanted sources.list.d entries] **********************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : include repositories] ****************************************************************************************************************
included: /home/mh/git/zgansibletest/roles/common/tasks/debian/sid/repos.yml for sid01, sid02

TASK [common : create list of sid wantedsourceslistfiles] *******************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : add sid files to wantedsourceslistfiles] *********************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : zda-sid-mc.list] *********************************************************************************************************************
changed: [sid01]
changed: [sid02]

RUNNING HANDLER [common : apt update] ***************************************************************************************************************
changed: [sid01]

PLAY [clean up after apt configuration] *************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************
ok: [sid01]

TASK [zzhandleapt : debug] **************************************************************************************************************************
ok: [sid01] => {
"msg": "wantedsourcelistfiles + [u'/etc/apt/sources.list.d/zda-sid-mc.list']"
}

TASK [zzhandleapt : delete sources.list.d files] ****************************************************************************************************
changed: [sid01] => (item={u'uid': 0, u'woth': False, u'mtime': 1522519373.161373, u'inode': 408337, u'isgid': False, u'size': 0, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'islnk': False, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/etc/apt/sources.list.d/zda-stretch-mc.list', u'xusr': False, u'atime': 1522519416.4131262, u'isdir': False, u'ctime': 1522519373.161373, u'isblk': False, u'wgrp': False, u'xgrp': False, u'dev': 65025, u'roth': True, u'isfifo': False, u'mode': u'0644', u'rusr': True})

TASK [zzhandleapt : apt update] *********************************************************************************************************************
changed: [sid01]

PLAY RECAP ******************************************************************************************************************************************
sid01 : ok=12 changed=4 unreachable=0 failed=0
sid02 : ok=7 changed=1 unreachable=0 failed=0

The first host is fine, but the -stretch- file is not removed on the second host.

Second ansible run:
[52/5037]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook --ask-become-pass --inventory=hosts.yml site.yml
SUDO password:

PLAY [apply common configuration] *******************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : search for sources.list.d files] *****************************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : set up fact list for wanted sources.list.d entries] **********************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : include repositories] ****************************************************************************************************************
included: /home/mh/git/zgansibletest/roles/common/tasks/debian/sid/repos.yml for sid01, sid02

TASK [common : create list of sid wantedsourceslistfiles] *******************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : add sid files to wantedsourceslistfiles] *********************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : zda-sid-mc.list] *********************************************************************************************************************
ok: [sid01]
ok: [sid02]
[WARNING]: Could not match supplied host pattern, ignoring: apt_action_hosts
PLAY [clean up after apt configuration] *************************************************************************************************************
skipping: no hosts matched

PLAY RECAP ******************************************************************************************************************************************
sid01 : ok=7 changed=0 unreachable=0 failed=0
sid02 : ok=7 changed=0 unreachable=0 failed=0


This is an obvious no-op, with ansible not doing anything on the second host,
but I would have expected it to remove the -stretch- file anyway

To force things, remove the -sid- file on the second host and invoke third ansible run:
[53/5037]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook --ask-become-pass --inventory=hosts.yml site.yml
SUDO password:

PLAY [apply common configuration] *******************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : search for sources.list.d files] *****************************************************************************************************
ok: [sid02]
ok: [sid01]

TASK [common : set up fact list for wanted sources.list.d entries] **********************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : include repositories] ****************************************************************************************************************
included: /home/mh/git/zgansibletest/roles/common/tasks/debian/sid/repos.yml for sid01, sid02

TASK [common : create list of sid wantedsourceslistfiles] *******************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : add sid files to wantedsourceslistfiles] *********************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : zda-sid-mc.list] *********************************************************************************************************************
ok: [sid01]
changed: [sid02]

RUNNING HANDLER [common : apt update] ***************************************************************************************************************
changed: [sid02]

PLAY [clean up after apt configuration] *************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************
ok: [sid02]

TASK [zzhandleapt : debug] **************************************************************************************************************************
ok: [sid02] => {
"msg": "wantedsourcelistfiles + [u'/etc/apt/sources.list.d/zda-sid-mc.list']"
}

TASK [zzhandleapt : delete sources.list.d files] ****************************************************************************************************
changed: [sid02] => (item={u'uid': 0, u'woth': False, u'mtime': 1522519445.3628857, u'inode': 409536, u'isgid': False, u'size': 0, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'islnk': False, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/etc/apt/sources.list.d/zda-stretch-mc.list', u'xusr': False, u'atime': 1522519449.9509423, u'isdir': False, u'ctime': 1522519445.3628857, u'isblk': False, u'wgrp': False, u'xgrp': False, u'dev': 65025, u'roth': True, u'isfifo': False, u'mode': u'0644', u'rusr': True})

TASK [zzhandleapt : apt update] *********************************************************************************************************************
changed: [sid02]

PLAY RECAP ******************************************************************************************************************************************
sid01 : ok=7 changed=0 unreachable=0 failed=0
sid02 : ok=12 changed=4 unreachable=0 failed=0

Now, things are as I want them.

However, the way to get there is unsatisfactory. Can somebody explain
what is happening and tell me how to do things right? If inserting debug
code helps, I'll happily do this, but I do not yet know too well how to
do this, so please suggest pasteable things. Thanks in advance!

Greetings
Marc







--
-----------------------------------------------------------------------------
Marc Haber | "I don't trust Computers. They | Mailadresse im Header
Leimen, Germany | lose things." Winona Ryder | Fon: *49 6224 1600402
Nordisch by Nature | How to make an American Quilt | Fax: *49 6224 1600421

Kai Stian Olstad

unread,
Mar 31, 2018, 3:39:48 PM3/31/18
to ansible...@googlegroups.com
On Saturday, 31 March 2018 20.35.46 CEST Marc Haber wrote:
> First ansible run:
> [51/5036]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook --ask-become-pass --inventory=hosts.yml site.yml

<snip />

> TASK [common : zda-sid-mc.list] *********************************************************************************************************************
> changed: [sid01]
> changed: [sid02]
>
> RUNNING HANDLER [common : apt update] ***************************************************************************************************************
> changed: [sid01]

Here is the problem, the task zda-sid-mc.list is changed on both host but only one of them run the handler.
Why this happen i don't know but at fist glance it looks like a bug.
What is your Ansible version?

You could also try more detailed logging to see if that gives more info:
ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook -vvv --ask-become-pass --inventory=hosts.yml site.yml


> PLAY [clean up after apt configuration] *************************************************************************************************************
>
> TASK [Gathering Facts] ******************************************************************************************************************************
> ok: [sid01]

Since the handler didn't run you only have one host in the second play.


> The first host is fine, but the -stretch- file is not removed on the second host.
>
> Second ansible run:
> [52/5037]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook --ask-become-pass --inventory=hosts.yml site.yml

<snip />

> TASK [common : zda-sid-mc.list] *********************************************************************************************************************
> ok: [sid01]
> ok: [sid02]
> [WARNING]: Could not match supplied host pattern, ignoring: apt_action_hosts
> PLAY [clean up after apt configuration] *************************************************************************************************************
> skipping: no hosts matched
>
> PLAY RECAP ******************************************************************************************************************************************
> sid01 : ok=7 changed=0 unreachable=0 failed=0
> sid02 : ok=7 changed=0 unreachable=0 failed=0
>
>
> This is an obvious no-op, with ansible not doing anything on the second host,
> but I would have expected it to remove the -stretch- file anyway

You have linked the removal of the files on the creating of zda-sid-mc.list, since this was created in you first run it's not created now, as you can see the ok status on zda-sid-mc.list task.
And because of that the handler will not be called so you second play doesn't run since there are no hosts in the group.


> However, the way to get there is unsatisfactory. Can somebody explain
> what is happening and tell me how to do things right? If inserting debug
> code helps, I'll happily do this, but I do not yet know too well how to
> do this, so please suggest pasteable things. Thanks in advance!

A mention above, it might be a bug in the notify/handler.

You could just run everything on all hosts all the time, and not create a runtime group that you later run a play against.
Because your solution now is not idempotent as you see on your second run when something fails in the play before.

--
Kai Stian Olstad

Kai Stian Olstad

unread,
Mar 31, 2018, 6:14:53 PM3/31/18
to ansible...@googlegroups.com
On Saturday, 31 March 2018 21.39.37 CEST Kai Stian Olstad wrote:
> On Saturday, 31 March 2018 20.35.46 CEST Marc Haber wrote:
> > First ansible run:
> > [51/5036]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook --ask-become-pass --inventory=hosts.yml site.yml
>
> <snip />
>
> > TASK [common : zda-sid-mc.list] *********************************************************************************************************************
> > changed: [sid01]
> > changed: [sid02]
> >
> > RUNNING HANDLER [common : apt update] ***************************************************************************************************************
> > changed: [sid01]
>
> Here is the problem, the task zda-sid-mc.list is changed on both host but only one of them run the handler.
> Why this happen i don't know but at fist glance it looks like a bug.

After reading the documentation on add_host I don't think this is a but, it's just how add_host works.

The documentation says
"This module bypasses the play host loop and only runs once for all the hosts in the play, if you need it to iterate use a with_ directive."

So this is probably the case when used in handlers to, it only run once.

--
Kai Stian Olstad

Marc Haber

unread,
Apr 2, 2018, 10:53:43 AM4/2/18
to ansible...@googlegroups.com
Hi Kai,

thanks for helping!

On Sat, Mar 31, 2018 at 09:39:37PM +0200, Kai Stian Olstad wrote:
> On Saturday, 31 March 2018 20.35.46 CEST Marc Haber wrote:
> > First ansible run:
> > [51/5036]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook --ask-become-pass --inventory=hosts.yml site.yml
>
> <snip />
>
> > TASK [common : zda-sid-mc.list] *********************************************************************************************************************
> > changed: [sid01]
> > changed: [sid02]
> >
> > RUNNING HANDLER [common : apt update] ***************************************************************************************************************
> > changed: [sid01]
>
> Here is the problem, the task zda-sid-mc.list is changed on both host but only one of them run the handler.
> Why this happen i don't know but at fist glance it looks like a bug.
> What is your Ansible version?

All systems in the test setup are Debian unstable, which ansible 2.5.0.

> You could also try more detailed logging to see if that gives more info:
> ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook -vvv --ask-become-pass --inventory=hosts.yml site.yml

That's 98 kB, too much for this list. I have uploadd to
http://q.bofh.de/~mh/stuff/ansible-log.txt

What confuses me is that no handling of the presentsourceslistfiles
variable is logged at all.

> You have linked the removal of the files on the creating of zda-sid-mc.list, since this was created in you first run it's not created now, as you can see the ok status on zda-sid-mc.list task.

Have I? I don't think so. From the creation of zda-sid-mc.list, I only
notify the apt update handler which only does apt update. Building the
present/wantedsourceslistfiles is always done in the common task, and
always executed in the zzhandleapt task.

At least that's what I wanted to write. Didn't I do that?

Marc Haber

unread,
Apr 2, 2018, 11:07:05 AM4/2/18
to ansible...@googlegroups.com
Hi Kai, again,

thanks for helping!

On Sun, Apr 01, 2018 at 12:14:38AM +0200, Kai Stian Olstad wrote:
> After reading the documentation on add_host I don't think this is a but, it's just how add_host works.
>
> The documentation says
> "This module bypasses the play host loop and only runs once for all the hosts in the play, if you need it to iterate use a with_ directive."
>
> So this is probably the case when used in handlers to, it only run once.

How would the code have to look like then? I am not yet proficient
enough in ansible to make immediate sense from the hint, and the
Examples only contain code building lists, not making use of them.

Kai Stian Olstad

unread,
Apr 3, 2018, 3:59:25 PM4/3/18
to ansible...@googlegroups.com
On 02.04.2018 16:53, Marc Haber wrote:
> On Sat, Mar 31, 2018 at 09:39:37PM +0200, Kai Stian Olstad wrote:
>> On Saturday, 31 March 2018 20.35.46 CEST Marc Haber wrote:
>> > First ansible run:
>> > [51/5036]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook --ask-become-pass --inventory=hosts.yml site.yml
>>
>> <snip />
>>
>> > TASK [common : zda-sid-mc.list] *********************************************************************************************************************
>> > changed: [sid01]
>> > changed: [sid02]
>> >
>> > RUNNING HANDLER [common : apt update] ***************************************************************************************************************
>> > changed: [sid01]
>>
>> Here is the problem, the task zda-sid-mc.list is changed on both host
>> but only one of them run the handler.
>> Why this happen i don't know but at fist glance it looks like a bug.
>> What is your Ansible version?
>
> All systems in the test setup are Debian unstable, which ansible 2.5.0.
>
>> You could also try more detailed logging to see if that gives more
>> info:
>> ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook -vvv
>> --ask-become-pass --inventory=hosts.yml site.yml
>
> That's 98 kB, too much for this list. I have uploadd to
> http://q.bofh.de/~mh/stuff/ansible-log.txt
>
> What confuses me is that no handling of the presentsourceslistfiles
> variable is logged at all.


The tasks run, but you don't see the content of the variable, you would
need to use debug for that.
And the log show it handles both notify but only one host is added by
add_host.


>> You have linked the removal of the files on the creating of
>> zda-sid-mc.list, since this was created in you first run it's not
>> created now, as you can see the ok status on zda-sid-mc.list task.
>
> Have I? I don't think so. From the creation of zda-sid-mc.list, I only
> notify the apt update handler which only does apt update. Building the
> present/wantedsourceslistfiles is always done in the common task, and
> always executed in the zzhandleapt task.
>
> At least that's what I wanted to write. Didn't I do that?

Your task "delete source.list.d files" is on role zzhandleapt so that
will only be run/deleted when the notify is run, and notify is on
zda-sid-mc.list task.

If the only go is to run apt update, why don't you just notify apt
module directly?

Create the handler as so.

- name: apt update
apt:
update_cache: yes

--
Kai Stian Olstad

Kai Stian Olstad

unread,
Apr 3, 2018, 4:07:27 PM4/3/18
to ansible...@googlegroups.com
On 02.04.2018 17:06, Marc Haber wrote:
> Hi Kai, again,
>
> thanks for helping!
>
> On Sun, Apr 01, 2018 at 12:14:38AM +0200, Kai Stian Olstad wrote:
>> After reading the documentation on add_host I don't think this is a
>> but, it's just how add_host works.
>>
>> The documentation says
>> "This module bypasses the play host loop and only runs once for all
>> the hosts in the play, if you need it to iterate use a with_
>> directive."
>>
>> So this is probably the case when used in handlers to, it only run
>> once.
>
> How would the code have to look like then? I am not yet proficient
> enough in ansible to make immediate sense from the hint, and the
> Examples only contain code building lists, not making use of them.

I would say don't use add_host it will just complicate thing for you.
You had a solution that work that you posted on this list back in
November, why not just use that one? It didn't use add_host.


--
Kai Stian Olstad

Marc Haber

unread,
May 17, 2018, 12:45:31 PM5/17/18
to ansible...@googlegroups.com
Hi Kai,

sorry for not getting back to you (and this list) any earlier; the
ansible stuff is just something I'm trying to grasp, and the paid-for
stuff kept prioritizing itself over and over again.

On Tue, Apr 03, 2018 at 10:07:08PM +0200, Kai Stian Olstad wrote:
> I would say don't use add_host it will just complicate thing for you.
> You had a solution that work that you posted on this list back in November,
> why not just use that one? It didn't use add_host.

The November version kept calling apt update over and over again for
every change it did, and while this is only time-consuming for apt[1],
it might be contraproductive in some other use cases of this construct.
Remember, I'm trying to build a standard idiom, and the apt case ist
just an easy example that anybody can easily try.

Greetings
Marc

[1] execution speed is currently my main beef with ansible, so I'd like
my code not be the fault for constant annoyance about things being slow.

Marc Haber

unread,
May 17, 2018, 12:58:11 PM5/17/18
to ansible...@googlegroups.com
Hi Kai,

(apology for taking six weeks to answer from other mail applies hier as
well)
So even with -vvv I won't see variables being handled without explicit
debug being written down?

> And the log show it handles both notify but only one host is added by
> add_host.

But why?

> Your task "delete source.list.d files" is on role zzhandleapt so that will
> only be run/deleted when the notify is run, and notify is on zda-sid-mc.list
> task.

That notify is a leftover from the original try; the current version is
meant to built a list of hosts that need "apt update" in the
apt_action_hosts lists, which the zzhandleapt being called conditionally
for all hosts in the list from the site.yml.

> If the only go is to run apt update, why don't you just notify apt module
> directly?

Because that will run apt update multiple times per host, resulting in
wasted time, and not all jobs called this way are idempotent.
Reply all
Reply to author
Forward
0 new messages