# Host lists, facts and handlers

**URL:** <https://forum.ansible.com/t/host-lists-facts-and-handlers/27164>\
**Category:** Ansible Project\
**Created:** [March 31, 2018, 6:35pm UTC](https://forum.ansible.com/t/host-lists-facts-and-handlers/27164 "2018-03-31T18:35:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [March 31, 2018, 6:35pm UTC](https://forum.ansible.com/t/host-lists-facts-and-handlers/27164/1 "2018-03-31T18:35:46Z")

</div>

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

Here is my code:  
==\> ./site.yml \<==

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 31, 2018, 7:39pm UTC](https://forum.ansible.com/t/host-lists-facts-and-handlers/27164/2 "2018-03-31T19:39:37Z")

</div>

> 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:  
&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 31, 2018, 10:14pm UTC](https://forum.ansible.com/t/host-lists-facts-and-handlers/27164/3 "2018-03-31T22:14:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [April 2, 2018, 2:53pm UTC](https://forum.ansible.com/t/host-lists-facts-and-handlers/27164/4 "2018-04-02T14:53:27Z")

</div>

Hi Kai,

thanks for helping!

> \> 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:  
> &nbsp;&nbsp;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](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?

Greetings  
Marc

---

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [April 2, 2018, 3:06pm UTC](https://forum.ansible.com/t/host-lists-facts-and-handlers/27164/5 "2018-04-02T15:06:51Z")

</div>

Hi Kai, again,

thanks for helping!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 3, 2018, 7:58pm UTC](https://forum.ansible.com/t/host-lists-facts-and-handlers/27164/6 "2018-04-03T19:58:56Z")

</div>

> > \> 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:  
> > &nbsp;&nbsp;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](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.

&nbsp;&nbsp;&nbsp;- name: apt update  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apt:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;update\_cache: yes

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 3, 2018, 8:07pm UTC](https://forum.ansible.com/t/host-lists-facts-and-handlers/27164/7 "2018-04-03T20:07:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [May 17, 2018, 4:45pm UTC](https://forum.ansible.com/t/host-lists-facts-and-handlers/27164/8 "2018-05-17T16:45:16Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [May 17, 2018, 4:58pm UTC](https://forum.ansible.com/t/host-lists-facts-and-handlers/27164/9 "2018-05-17T16:58:00Z")

</div>

Hi Kai,

(apology for taking six weeks to answer from other mail applies hier as  
well)

> \> \> \> 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](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.

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.

Greetings  
Marc
