Using when and with_items

265 views
Skip to first unread message

James Goodhouse

unread,
Mar 31, 2014, 6:56:21 PM3/31/14
to ansible...@googlegroups.com
I see in the documentation then combining when with with_items will process the when statement for each item separately. Does this mean I cannot put a generic when statement on a task that has with_items?

I'm trying to process a task only when a condition is met. For example, if variable xyz is desfined, then proceed with the task that has a with_items statement. Unfortunately, this does not work and returns the error "with_items expects a list or a set". I'm using the following chunk of code:

- name: copy template to folder
  template:
    src: template.j2
    dest: /remote/file/location/{{ item.name }}
    owner: root
    group: root
    mode: 0600
  with_items: variable_array
  when: variable_array is defined

James Goodhouse

unread,
Mar 31, 2014, 7:00:52 PM3/31/14
to ansible...@googlegroups.com
I need to make a correction. It seems to work fine when I have just a basic variable set, but I if I use the following it doesn't work. Am I thinking about this wrong?

- name: copy template to folder
  template:
    src: template.j2
    dest: /remote/file/location/{{ item.name }}
    owner: root
    group: root
    mode: 0600
  with_items: variable['another_var']
  when: variable_array is defined

Marko Lisica

unread,
Apr 1, 2014, 3:33:47 PM4/1/14
to ansible...@googlegroups.com

Hi can you show your vars also?
Variable_array particulary.
Reply all
Reply to author
Forward
0 new messages