Combine with_items with when.

61 views
Skip to first unread message

Jonas Rottmann

unread,
May 7, 2015, 2:13:50 AM5/7/15
to ansible...@googlegroups.com
Hi,

Is it possible to combine with_items with when? so i.e.

- name: Vhosts apache config
  template: src=apache_vhost.j2 dest=/etc/apache2/sites-available/{{ item.domain }}.conf
  with_items: apache.vhosts
  when: "ansible_os_family == 'Debian' and item.state == 'availible'"
  tags:
    - apache
  notify:
    - reload apache2

For this I get:  error while evaluating conditional

so I can choose for the loop if it is run according to an property set on this item?

If not is there a workaround or anything for it?

Thanks

Brian Coca

unread,
May 7, 2015, 10:22:00 AM5/7/15
to ansible...@googlegroups.com
yes, when runs after with_ and can use the item variable, your problem
is with quoting, not the items.

when: ansible_os_family == 'Debian' and item.state == 'available'

The when is always templated (implicit {{ }} around it), if you quote
the whole thing it will get looked at as a string.


--
Brian Coca

Jonas Rottmann

unread,
May 8, 2015, 6:42:26 AM5/8/15
to ansible...@googlegroups.com
Hi,

Without quotes I get the same error without quotes...

- name: Vhosts apache config
  template: src=apache_vhost.j2 dest=/etc/apache2/sites-available/{{ item.domain }}.conf
  with_items: apache.vhosts
  when: ansible_os_family == 'Debian' and item.state == 'availible'
  tags:
    - apache
  notify:
    - reload apache2

Brian Coca

unread,
May 8, 2015, 12:29:37 PM5/8/15
to ansible...@googlegroups.com
does every list entry in apache.vhosts have 'state'?




--
Brian Coca
Reply all
Reply to author
Forward
0 new messages