Conditional with dict2item is falied

20 views
Skip to first unread message

Rafael Tomelin

unread,
Jun 15, 2020, 3:39:42 PM6/15/20
to ansible...@googlegroups.com

Hi guys,

I have a task with equal below, but when I validate it is defined it not recognized.
- name: include vars virtual_machine
include_tasks: virtual_machine.yml
when:
- gms_az_vm_action == 'create_virtual_machine'
- virtual_machine is defined
loop: "{{ virtual_machine|dict2items }}"



How I do for doesn't read loop and not stop playbook when virtual_machine not defined?

--
Atenciosamente,

Rafael Tomelin
Tel.: 51-984104084
Skype: rafael.tomelin

LPI ID: LPI000191271
Red Hat Certified Engineer
Puppet Professional 2017 Certification

Matt Martz

unread,
Jun 15, 2020, 3:43:02 PM6/15/20
to ansible...@googlegroups.com
In the case of using `when` and `loop`, the `when` statements is applied to each iteration of the `loop`, and not before it.

As such, you cannot prevent a loop from happening using a when statement.

Instead you want to do this in loop, like:

loop: '{{ virtual_machine|default({})|dict2items }}'

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGEUqbAv4tEJoSWWU7TR9YP3x%2B5BzVDO6NmS7rZCGYhEHN_ocg%40mail.gmail.com.


--
Matt Martz
@sivel
sivel.net

Rafael Tomelin

unread,
Jun 15, 2020, 4:07:07 PM6/15/20
to ansible...@googlegroups.com

Stefan Hornburg (Racke)

unread,
Jun 15, 2020, 5:31:26 PM6/15/20
to ansible...@googlegroups.com
On 6/15/20 9:39 PM, Rafael Tomelin wrote:
>
> Hi guys,
>
> I have a task with equal below, but when I validate it is defined it not recognized.
> - name: include vars virtual_machine
> include_tasks: virtual_machine.yml
> when:
> - gms_az_vm_action == 'create_virtual_machine'
> - virtual_machine is defined
> loop: "{{ virtual_machine|dict2items }}"
>
>
>
> How I do for doesn't read loop and not stop playbook when virtual_machine not defined?

loop: "{{ virtual_machine | default({}) | dict2items }}"

This produces an empty dict ({}) if virtual_machine is undefined and the loop goes over zero iterations.

Regards
Racke

>
> --
> Atenciosamente,
>
> Rafael Tomelin
> Tel.: 51-984104084
> Skype: rafael.tomelin
>
> LPI ID: LPI000191271
> Red Hat Certified Engineer
> Puppet Professional 2017 Certification
>
> --
> 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>.
> <https://groups.google.com/d/msgid/ansible-project/CAGEUqbAv4tEJoSWWU7TR9YP3x%2B5BzVDO6NmS7rZCGYhEHN_ocg%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc
Reply all
Reply to author
Forward
0 new messages