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


- name: Configure key on remote host
  authorized_key:
    user: root
    key: "{{ item }}"
  with_file:
    - "{{ my_key }}.pub"
  become: true
  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

[1] https://github.com/ansible/ansible/issues/20919
[2] 
http://docs.ansible.com/ansible/playbooks_conditionals.html#loops-and-conditionals

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/baad666b-c0bd-4e3b-925f-3dc7523f06a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to