# Create exception for service on a host

**URL:** <https://forum.ansible.com/t/create-exception-for-service-on-a-host/21230>\
**Category:** Ansible Project\
**Created:** [April 21, 2016, 2:27pm UTC](https://forum.ansible.com/t/create-exception-for-service-on-a-host/21230 "2016-04-21T14:27:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pogan](https://avatars.discourse-cdn.com/v4/letter/p/848f3c/32.png) [@Pogan](https://forum.ansible.com/u/Pogan)\
**Post date:** [April 21, 2016, 2:27pm UTC](https://forum.ansible.com/t/create-exception-for-service-on-a-host/21230/1 "2016-04-21T14:27:07Z")

</div>

Hi,

I guess this is fairly simple question, but I’m stuck with it…  
I have a playbook that checks whether services are up on hosts and starts them if they are not in started state, for example:

> - hosts: mailservers
> 
> tasks:
> 
> - name: Check amavis service, and start if needed.  
> service: name=amavis state=started

What if i wanted to leave service in stopped state only on one of the hosts in “mailservers” group while performing this playbook? How can I achieve that?

---

<div class="post-metadata">

**Author:** ![Pogan](https://avatars.discourse-cdn.com/v4/letter/p/848f3c/32.png) [@Pogan](https://forum.ansible.com/u/Pogan)\
**Post date:** [April 22, 2016, 2:42pm UTC](https://forum.ansible.com/t/create-exception-for-service-on-a-host/21230/2 "2016-04-22T14:42:01Z")

</div>

I forgot to mention that idea is to do this through host variable file, and not through playbook itself.

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [April 22, 2016, 6:38pm UTC](https://forum.ansible.com/t/create-exception-for-service-on-a-host/21230/3 "2016-04-22T18:38:51Z")

</div>

Set a variable called bar for this host foo in the host\_vars/foo file  
and only execute the task, when this variable is not set:

- hosts: mailservers  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Check amavis service, and start if needed.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service: name=amavis state=started  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: hostvars[foo]['bar']|default('') is undefined

I'm not sure what exactly has to be inside the default, but using  
default avoids errors when the variable bar is undefined...

Johannes
