with_dict fatal error after conditional include

187 views
Skip to first unread message

Chip Selden

unread,
Jan 26, 2015, 6:39:28 PM1/26/15
to ansible...@googlegroups.com
Ansible Community,

I'm using a conditional include statement to call a task file which has a task that uses a "with_dict" loop. When the include statement conditional fails, I'd like the task in "option.yml" to be skipped  as the task would result in an error anyway. 

When this is run, I get an error "with_dict expects a dict" since there is no dictionary "groups[{{ grps[2] }}]". Why does Ansible try to evaluate the task at all since it should be skipped because of the include conditional?

Ideally, there would be a way to use a "with_items" loop with an include statement, but I am aware that that is intentionally not allowed.

(Note: I know this is a terrible example of how/when to use an include statement, but it shows the gist of how the error occurs. My actual usage is much more complicated.)
## variables
---
people:
  admins:
    alice:
      name: Alice Anderson
      age: 24
    bob: 
      name: Bob Brown
      age: 29
  devs:
    chris:
      name: Chris Campbell
      age: 34
groups:
  - admins
  - devs

## file main.yml
---
- debug: var=groups
- include: option.yml
  vars:
    group: "{{groups[2]}}"
  when: "2 < groups|length"

## file option.yml
---
- name: Print person info
  shell: "echo {{item.value.name}} is {{item.value.age}}"
  with_dict: groups[group]


Barry Kaplan

unread,
May 1, 2015, 6:33:28 AM5/1/15
to ansible...@googlegroups.com
I just ran into this same problem. Here is a much simpler test case:

- debug: msg="DICT {{item.key}}"
  when: undefined_dict is defined
  with_dict: undefined_dict

results in:

TASK: [consul | debug msg="DICT {{item.key}}"] ********************************
fatal: [consul-client-1] => with_dict expects a dict


Barry Kaplan

unread,
May 1, 2015, 6:37:00 AM5/1/15
to ansible...@googlegroups.com

Barry Kaplan

unread,
May 1, 2015, 6:43:23 AM5/1/15
to ansible...@googlegroups.com
I just found https://github.com/ansible/ansible/issues/8546, which explains clearly that when is for each item in the loop. (Which I now recall reading in the docs). So this is clearly not a bug.

And the workaround in that issues works just fine for my case.

Barry Kaplan

unread,
May 1, 2015, 6:47:12 AM5/1/15
to ansible...@googlegroups.com
But related, and where this started for me, was running into this via a conditional include:

main.yml
- { include: service_def.yml, when: consul_services is defined }

service_def.yml
- name: Create service definitions
  template: > ...
  with_dict: consul_services

I did not expect service_def.yml to even be loaded in this case, but it clearly does. What exactly is happening with task includes with a 'when: false'? Does each task in the include get a hidden 'when: false' added to it? That would explain the behavior, but its definately not the behavior of least surprise to me.

Reply all
Reply to author
Forward
0 new messages