How to stop fileglob lookup warning for not existing paths

97 views
Skip to first unread message

cl...@netsandbox.de

unread,
Mar 8, 2022, 7:50:59 AMMar 8
to Ansible Project
Hi, I have the following task:

- ansible.builtin.copy:
src: "{{ item }}"
dest: /tmp/
with_fileglob: "files/tmp/{{ inventory_hostname }}/*.yaml"

Directory files/tmp/bar/ contains yaml files for host bar.
Directory files/tmp/foo/ doesn't exist because host foo should not receive any files with this task.

If I run this task on hosts bar and foo, I always get a warning:
[WARNING]: Unable to find 'files/tmp/foo' in expected paths (use -vvvvv to see paths)

I added a condition:
when: inventory_hostname == "bar"

and even wrapped the above task in a block with the above mentioned when condition,
but I always get the warning.

Any idea how to stop the warning?

Vladimir Botka

unread,
Mar 8, 2022, 9:00:46 AMMar 8
to cl...@netsandbox.de, ansible...@googlegroups.com
On Tue, 8 Mar 2022 04:50:59 -0800 (PST)
"cl...@netsandbox.de" <cl...@netsandbox.de> wrote:

> Hi, I have the following task:
>
> - ansible.builtin.copy:
> src: "{{ item }}"
> dest: /tmp/
> with_fileglob: "files/tmp/{{ inventory_hostname }}/*.yaml"
>
> Directory files/tmp/bar/ contains yaml files for host *bar*.
> Directory files/tmp/foo/ doesn't exist because host *foo* should not
> receive any files with this task.
>
> If I run this task on hosts *bar* and *foo*, I always get a warning:
> [WARNING]: Unable to find 'files/tmp/foo' in expected paths (use -vvvvv to
> see paths)

The simplest solution is to create empty directories if missing

- ansible.builtin.file:
state: directory
path: "files/tmp/{{ inventory_hostname }}"
delegate_to: localhost

- ansible.builtin.copy:
src: "{{ item }}"
dest: /tmp/
with_fileglob: "files/tmp/{{ inventory_hostname }}/*.yaml"


--
Vladimir Botka

cl...@netsandbox.de

unread,
Mar 8, 2022, 9:20:10 AMMar 8
to Ansible Project
That's not really a solution if I have 100 playbook hosts and 5 hosts where I will deploy files in this way.

cl...@netsandbox.de

unread,
Mar 8, 2022, 9:34:50 AMMar 8
to Ansible Project
I was really surprised that wrapping the task in a block still gives me the warning for host foo:

- block:
- ansible.builtin.copy:
src: "{{ item }}"
dest: /tmp/
with_fileglob: "files/tmp/{{ inventory_hostname }}/*.yaml"
when: inventory_hostname == "bar"

I would expect here that the task (and the fileglob lookup) isn't run for host foo.

Vladimir Botka

unread,
Mar 8, 2022, 9:58:28 AMMar 8
to cl...@netsandbox.de, ansible...@googlegroups.com
On Tue, 8 Mar 2022 06:20:10 -0800 (PST)
"cl...@netsandbox.de" <cl...@netsandbox.de> wrote:

> That's not really a solution if I have 100 playbook hosts and 5 hosts where
> I will deploy files in this way.

In this case, it would be more efficient to select those 5 hosts
instead of looping all 100.

--
Vladimir Botka

cl...@netsandbox.de

unread,
Mar 8, 2022, 12:10:35 PMMar 8
to Ansible Project
The above task is just a minimal example that shows my problem.
This is actually used in my "one playbook to rule them all" and it doesn't make sense to split this playbook in multiple ones.
I already know some not so nice workaround that maybe will suppress the warning.

I actually look for someone who shows me that I'm maybe missing something or telling me that this is a bug.
Especially when wrapping the task in a block which still triggers the fileglob for hosts which doesn't met the when condition sounds line a bug to me.

Philippe Kück

unread,
Mar 8, 2022, 12:18:53 PMMar 8
to ansible...@googlegroups.com
Hi,


see https://github.com/ansible/ansible/pull/47344

I believe it's simply not wanted to avoid the warnings.


- Philippe

On 08.03.22 13:50, cl...@netsandbox.de wrote:
> Hi, I have the following task:
>
> - ansible.builtin.copy:
> src: "{{ item }}"
> dest: /tmp/
> with_fileglob: "files/tmp/{{ inventory_hostname }}/*.yaml"
>
> Directory files/tmp/bar/ contains yaml files for host *bar*.
> Directory files/tmp/foo/ doesn't exist because host *foo* should not
> receive any files with this task.
>
> If I run this task on hosts *bar* and *foo*, I always get a warning:
> [WARNING]: Unable to find 'files/tmp/foo' in expected paths (use -vvvvv
> to see paths)
>
> I added a condition:
> when: inventory_hostname == "bar"
>
> and even wrapped the above task in a *block* with the above mentioned
> when condition,
> but I always get the warning.
>
> Any idea how to stop the warning?
>
> --
> 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
> <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/fd101353-b255-488f-881e-ea9ab39d2066n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/fd101353-b255-488f-881e-ea9ab39d2066n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature

cl...@netsandbox.de

unread,
Mar 8, 2022, 12:54:32 PMMar 8
to Ansible Project
Thanks Philippe for the PR link.
I just thought about extending fileglob in this way.

Vladimir Botka

unread,
Mar 9, 2022, 2:44:13 AMMar 9
to cl...@netsandbox.de, ansible...@googlegroups.com
On Tue, 8 Mar 2022 06:34:50 -0800 (PST)
"cl...@netsandbox.de" <cl...@netsandbox.de> wrote:

> I was really surprised that wrapping the task in a block still gives me the
> warning for host *foo*:
>
> - block:
> - ansible.builtin.copy:
> src: "{{ item }}"
> dest: /tmp/
> with_fileglob: "files/tmp/{{ inventory_hostname }}/*.yaml"
> when: inventory_hostname == "bar"
>
> I would expect here that the task (and the fileglob lookup) isn't run for
> host *foo*.

It's not a bug it's by design. In a loop, the condition is evaluated
on each iteration. In your case, *with_fileglob* must evaluate the
list before the condition is applied.

*block* is irrelevant here. The condition is applied to all tasks.
Try for example

- block:
- debug:
msg: OK1
- debug:
msg: OK2
when: false

Instead of skipping the whole block the tasks will be skipped one by
one.

--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages