include when foo['bar'] is defined

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?

The include should always happen, the when: should be applied to the
included tasks and these should be skipped.

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

when won't prevent with_items from evaluating, since the when: can
include the 'item' variable to be conditional per iteration. Add a

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