# Skip task if variable isn't set?

**URL:** <https://forum.ansible.com/t/skip-task-if-variable-isnt-set/18621>\
**Category:** Ansible Project\
**Created:** [June 26, 2015, 3:49pm UTC](https://forum.ansible.com/t/skip-task-if-variable-isnt-set/18621 "2015-06-26T15:49:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [June 26, 2015, 3:49pm UTC](https://forum.ansible.com/t/skip-task-if-variable-isnt-set/18621/1 "2015-06-26T15:49:14Z")

</div>

I have a task that installs PHP and Perl modules against a group of servers. Servers w/ the word “batch” in their hostname run both Perl and PHP tasks (remove Perl tasks for simplicity). It seems the “Set PHP status success” task runs against machines w/ the word “batch” in them. ANy way I can skip it?

- name: Install PHP modules  
command: sudo rpm -Uvh {{ rpm\_repository }}/{{ item.key }}-{{ item.value.svn\_tag }}.rpm --force  
with\_dict: deploy\_modules\_php  
register: php\_command\_result  
when: “‘batch’ in ansible\_hostname”  
ignore\_errors: True

- name: set PHP status fail  
when: php\_command\_result|failed  
set\_fact:  
color=“red”  
msg=“Unable to install PHP RPMs!”

- name: Set PHP status success  
when: php\_command\_result|success  
set\_fact:  
color=“green”  
msg=“Successfully installed PHP RPMs!”

---

<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:** [June 26, 2015, 4:18pm UTC](https://forum.ansible.com/t/skip-task-if-variable-isnt-set/18621/2 "2015-06-26T16:18:12Z")

</div>

when runs for EACH item from with\_items, so you cannot use it to avoid  
undefined with\_items, but you can do this:

with\_dict: deploy\_modules\_php|default({})

which will return an empty dict, which will also skip the loops

---

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [June 26, 2015, 4:53pm UTC](https://forum.ansible.com/t/skip-task-if-variable-isnt-set/18621/3 "2015-06-26T16:53:33Z")

</div>

But the dictionary isn’t empty. I populate it for hosts w/ the word “batch.”

When it does run against a server w/o the word batch, “Install PHP modules” is skipped because of the when.

- name: Install PHP modules  
command: sudo rpm -Uvh {{ rpm\_repository }}/aria-{{ item.key }}-{{ item.value.svn\_tag }}.rpm --force  
with\_dict: deploy\_modules\_php  
register: php\_command\_result  
when: “‘batch’ in ansible\_hostname”  
ignore\_errors: True

---

<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:** [June 26, 2015, 5:23pm UTC](https://forum.ansible.com/t/skip-task-if-variable-isnt-set/18621/4 "2015-06-26T17:23:55Z")

</div>

sorry, then I don't understand the question.
