# Only execute task if variable is defined when using with\_items?

**URL:** <https://forum.ansible.com/t/only-execute-task-if-variable-is-defined-when-using-with-items/19580>\
**Category:** Ansible Project\
**Created:** [October 21, 2015, 6:30pm UTC](https://forum.ansible.com/t/only-execute-task-if-variable-is-defined-when-using-with-items/19580 "2015-10-21T18:30:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bob\_Tanner](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Bob\_Tanner](https://forum.ansible.com/u/Bob_Tanner)\
**Post date:** [October 21, 2015, 6:30pm UTC](https://forum.ansible.com/t/only-execute-task-if-variable-is-defined-when-using-with-items/19580/1 "2015-10-21T18:30:36Z")

</div>

I want the following task to be executed only when etc\_hosts is defined

- name: other important hosts in /etc/hosts  
lineinfile: \>  
dest=‘/etc/hosts’  
regexp=‘.\*{{ item.hostname }}$’  
line=‘{{ item.address }} {{ item.hostname }}’  
state=‘present’  
with\_items:
- ‘{{ etc\_hosts }}’

All these tasks fail for one reason or another.

- name: other important hosts in /etc/hosts  
lineinfile: \>  
dest=‘/etc/hosts’  
regexp=‘.\*{{ item.hostname }}$’  
line=‘{{ item.address }} {{ item.hostname }}’  
state=‘present’  
when: etc\_hosts is defined  
with\_items:
- ‘{{ etc\_hosts }}’

FAILED! =\> {“failed”: true, “msg”: “ERROR! ‘etc\_hosts’ is undefined”}

- name: other important hosts in /etc/hosts  
lineinfile: \>  
dest=‘/etc/hosts’  
regexp=‘.\*{{ item.hostname }}$’  
line=‘{{ item.address }} {{ item.hostname }}’  
state=‘present’  
when: {{ etc\_hosts }} is defined  
with\_items:
- ‘{{ etc\_hosts }}’

The offending line appears to be:

state=‘present’  
when: {{ etc\_hosts }} is defined  
^ here

- name: other important hosts in /etc/hosts  
lineinfile: \>  
dest=‘/etc/hosts’  
regexp=‘.\*{{ item.hostname }}$’  
line=‘{{ item.address }} {{ item.hostname }}’  
state=‘present’  
when: “{{ etc\_hosts }}” is defined  
with\_items:
- ‘{{ etc\_hosts }}’

The offending line appears to be:

state=‘present’  
when: “{{ etc\_hosts }}” is defined  
^ here

---

<div class="post-metadata">

**Author:** ![Markus\_Ellers](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Markus\_Ellers](https://forum.ansible.com/u/Markus_Ellers)\
**Post date:** [October 22, 2015, 5:56pm UTC](https://forum.ansible.com/t/only-execute-task-if-variable-is-defined-when-using-with-items/19580/2 "2015-10-22T17:56:23Z")

</div>

Hm, the doc only gives an example on how to fail the whole play in a controlled way if the variable is not defined, Is that an option for you?

```auto
tasks:
    - shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
      when: foo is defined

    - fail: msg="Bailing out. this play requires 'bar'"
      when: bar is undefined

```

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 22, 2015, 8:19pm UTC](https://forum.ansible.com/t/only-execute-task-if-variable-is-defined-when-using-with-items/19580/3 "2015-10-22T20:19:13Z")

</div>

when: executes per item so you cannot use it to bypass a with\_items:  
undefined, use default filter:

with\_items: "{{etc\_hosts|default() }}"

this returns an empty list, which will skip the loop and the task.
