with_items: expects a list or a set by using it with when:

1,392 views
Skip to first unread message

bshbg

unread,
Oct 17, 2014, 6:04:14 AM10/17/14
to ansible...@googlegroups.com
I have the following two statements which have the same when part. The first one is skipped correctly but the second creates an error. It should be skipped also.

- debug: var="{{ sites[env].restricted_areas }}"
 
when: sites[env].restricted_areas is defined
 
- template: src="apache/site_credentials" dest="{{ apache_vhost_base_path }}/{{ sites[env].server_name }}/conf/{{ item.area.internal_name }}"
  with_items
: sites[env].restricted_areas | list
 
when: sites[env].restricted_areas is defined
  tags
: create



result:

TASK: [website | debug var="{{sites[env].restricted_areas}}"] *****************
skipping
: [TSSDMZPORTAL002]


TASK
: [website | template src="apache/site_credentials" dest="{{apache_vhost_base_path}}/{{sites[env].server_name}}/conf/{{item.area.internal_name}}"] ***
fatal
: [TSSDMZPORTAL002] => with_items expects a list or a set


can someone help me?

Michael DeHaan

unread,
Oct 20, 2014, 5:02:25 PM10/20/14
to ansible...@googlegroups.com
The when is evaluated for each step in the loop, not before looping, which is the source of confusion here.

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/eb330a83-d434-4d8f-8022-0f9acafff636%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Frank Lye

unread,
Nov 5, 2015, 5:42:04 PM11/5/15
to Ansible Project
I'm having the same issue. How can we make this task check if the variable exists before looping ? If we can't use when here, what can we do ?

Markus Ellers

unread,
Nov 7, 2015, 5:11:54 AM11/7/15
to Ansible Project
you could put the tasks in a different yml file and do a conditional include based on your "when".

Brian Coca

unread,
Nov 7, 2015, 6:24:51 PM11/7/15
to Ansible Project
first no moustaches in var, it just wants a variable name

- debug: var=sites[env].restricted_areas

you are putting several variables that can be undefined in your when
statement so to be safe you should check each step:

when: env is defined and env in sites and 'restricted_areas' in sites[env]

Now you can split them up and check each step to make sure what you
expect to be defined is there


--
Brian Coca

Frank Ly

unread,
Nov 8, 2015, 10:19:16 AM11/8/15
to Ansible Project
Markus,

That's exactly what I ended up doing. Thanks for your help.
Reply all
Reply to author
Forward
0 new messages