# Proper use of "when" conditional

**URL:** <https://forum.ansible.com/t/proper-use-of-when-conditional/19394>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [September 30, 2015, 5:04pm UTC](https://forum.ansible.com/t/proper-use-of-when-conditional/19394 "2015-09-30T17:04:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ender](https://avatars.discourse-cdn.com/v4/letter/e/58f4c7/32.png) [@ender](https://forum.ansible.com/u/ender)\
**Post date:** [September 30, 2015, 5:04pm UTC](https://forum.ansible.com/t/proper-use-of-when-conditional/19394/1 "2015-09-30T17:04:47Z")

</div>

I am fairly new to Ansible and having a problem with a playbook in which I wish to have a handler run one script if the target OS is in the RedHat family and another script if the target OS is in the Debian family. I am clearly doing this wrong. Here’s the relevant snippet:

[…]

handlers:

- name: restart foo  
shell: /etc/init.d/barfoo stop ; /etc/init.d/barfoo start  
when: ansible\_os\_family == “RedHat”  
shell: /etc/init.d/bazfooinit stop ; /etc/init.dbazfooinit start  
when: ansible\_os\_family == “Debian”

[…]

This works fine for the targets running a Debian distro (in this case two Debians and an Ubuntu) but does not work. for the hosts running a Red Hat distro (actually three CentOS servers, all running the same version of CentOS). The handler just returns “skipping: []” for each CentOS host.

Running ansible against those targets with -m setup -a ‘filter=ansible\_os\_family’ returns the expected “RedHat” for the CentOS hosts.

I suspect I am doing something wrong. Is it not possible to have two “when:” statements and only the last one is interpreted? That would seem to explain what I am seeing. If that, or something similar is the case how can I accomplish what I need to do… kind of a “if RedHat, elseif Debian” sort of behavior?

Thanks!

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [September 30, 2015, 6:58pm UTC](https://forum.ansible.com/t/proper-use-of-when-conditional/19394/2 "2015-09-30T18:58:58Z")

</div>

HI Ender,

I believe there can be only one action in handler. Use variables to solve this. First task in role:

- name: Load the OS specific variables  
include\_vars: “{{ ansible\_os\_family }}.yml”

in vars/ have a files:

RedHat.yml  
service\_name: “barfoo”

Debian.yml

service\_name: “barfooinit”

Then in handler:

handlers:

service: name={{ service\_name }} state=restarted

New platform means new variable file, no task modifications.

---

<div class="post-metadata">

**Author:** ![ender](https://avatars.discourse-cdn.com/v4/letter/e/58f4c7/32.png) [@ender](https://forum.ansible.com/u/ender)\
**Post date:** [October 1, 2015, 12:40pm UTC](https://forum.ansible.com/t/proper-use-of-when-conditional/19394/3 "2015-10-01T12:40:02Z")

</div>

Thanks for the reply. I think this will end up being more complicated as I cannot use the service module but must run the script with a stop argument and then a start argument. Can I define a function in Ansible? Have a RedHat.yml file that defines a “restart” function using the proper shell information (a task?) for that OS and a separate Debian.yml file with the correct syntax for that OS and then call the function “restart” in a handler?

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [October 1, 2015, 2:51pm UTC](https://forum.ansible.com/t/proper-use-of-when-conditional/19394/4 "2015-10-01T14:51:10Z")

</div>

Not a problem too, define two handlers:

- name: stop foo  
service: name={{ service\_name }} state=stopped

- name: start foo  
service: name={{ service\_name }} state=started

In tasks define:

tasks:

- name: change happen here

…

notify:

- stop foo

- start foo
