How to stop fileglob lookup warning for not existing paths

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?

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"

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

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.

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

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.

Hi,

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

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

- Philippe

Thanks Philippe for the PR link.
I just thought about extending fileglob in this way.

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.