Hi everybody,
following this bug [1] I've opened yesterday and the suggestion from Brian I want to understand in depth what is considered by design inside Ansible about "when".
Now, leaving aside the usage of "when" inside roles, let's start with this simple example:
---
- name: Create key
delegate_to: localhost
command: >
ssh-keygen -f {{ my_key }} -N ''
-C 'ansible_generated_virt_host'
-t rsa -b 4096
args:
creates: "{{ my_key }}"
when: do_the_test|bool
Two tasks, each one depending on the "do_the_test" condition.
Now, if you execute this you will get an error concerning the fact that "{{ my_key }}.pub" is not existing. And of course it is not, because it should be created by the first task, which is skipped because of the "when:" condition.
Now, you will say that as stated here [2] this is by design, in fact it explicitly says:
be aware that the when statement is processed separately for each item.
So in this case, if I want to skip that task I need to find another method, since Ansible will try to "resolve" the variable "{{ item }}" loading *in any case* the file(s) declared in "with_file".
And here comes my question: I understood technically why my problem is happening (and if you see the bug I already found a workaround), but how can I reach what I want then? Is there something that makes you skip the whole task without processing each item? The point is that if I want a task to be skipped I don't want to care about what's inside it, I just don't want to execute (or parse, or load) anything around it.
Thanks for your attention,
Raoul