include when foo['bar'] is defined

27 views
Skip to first unread message

Bret Martin

unread,
Mar 23, 2015, 3:45:04 PM3/23/15
to ansible...@googlegroups.com
I have a number of plays like

- include: data-importer.yml
  when: instances['data-importer'] is defined

that no longer work after a recent Ansible upgrade. The include occurs even when instances['data-importer'] is not set. I tracked the behavior change down to between 1.8.1 and 1.8.2.

Is this supposed to still work?

Brian Coca

unread,
Mar 23, 2015, 3:46:30 PM3/23/15
to ansible...@googlegroups.com
The include should always happen, the when: should be applied to the
included tasks and these should be skipped.



--
Brian Coca

Bret Martin

unread,
Mar 23, 2015, 3:58:35 PM3/23/15
to ansible...@googlegroups.com
Thanks for the quick reply. That is definitely the behavior I've seen previously. Looking closer after your description of what happens, I note that the included tasks are being skipped. The failure is happening on a task that uses with_items, where the with_items line refers to the variable tested in the include/when.

Playbook/parent file:

- include: data-importer.yml
when: instances['data-importer'] is defined


Included file 1:

- include: instance-common.yml
description="Data Importer"
instance=data-importer


Included file 2:

- name: create {{ description }} instance
ec2:
[...]
with_items: instances[instance]["hostname"]


With this arrangement, I get

TASK: [aws-ec2 | create Data Importer instance] *******************************
fatal: [aws-vpc-hbi16058] => with_items expects a list or a set


So my problem seems to be specific to with_items and skipped tasks.

--Bret

Brian Coca

unread,
Mar 23, 2015, 4:10:57 PM3/23/15
to ansible...@googlegroups.com
when won't prevent with_items from evaluating, since the when: can
include the 'item' variable to be conditional per iteration. Add a
|default([]) to avoid this issue.




--
Brian Coca

Bret Martin

unread,
Mar 23, 2015, 4:15:55 PM3/23/15
to ansible...@googlegroups.com
Thanks again. I tried changing the play in the innermost file (actually, before I even started this thread) to

- name: create {{ description }} instance
ec2:
[...]
with_items: instances[instance]["hostname"] | default([])

but the result is the same:

TASK: [aws-ec2 | create Data Importer instance] *******************************
fatal: [aws-vpc-hbi16058] => with_items expects a list or a set

--Bret
Reply all
Reply to author
Forward
0 new messages