# Something Similar to Ruby's Case Operator

**URL:** <https://forum.ansible.com/t/something-similar-to-rubys-case-operator/15206>\
**Category:** Ansible Project\
**Created:** [May 10, 2014, 2:09pm UTC](https://forum.ansible.com/t/something-similar-to-rubys-case-operator/15206 "2014-05-10T14:09:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navid\_Paya2](https://avatars.discourse-cdn.com/v4/letter/n/dbc845/32.png) [@Navid\_Paya2](https://forum.ansible.com/u/Navid_Paya2)\
**Post date:** [May 10, 2014, 2:09pm UTC](https://forum.ansible.com/t/something-similar-to-rubys-case-operator/15206/1 "2014-05-10T14:09:12Z")

</div>

Hi all  
We have a mix of Debian and Redhat in our infrastructure so basically for every single step, I have to add two of the same operators for both of them, like this:

- name: Install prerequisite packages  
action: apt pkg={{item}} state=installed  
with\_items:

- sysstat  
when: ansible\_os\_family == ‘Debian’

- name: Install prerequisite packages  
action: yum pkg={{item}} state=installed  
with\_items:

- sysstat  
when: ansible\_os\_family == ‘RedHat’

Does Ansible have an operator similar to the case operator in Ruby or [Chef](http://docs.opscode.com/chef/dsl_recipe.html#case-statements) and Puppet for that matter?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 12, 2014, 3:11pm UTC](https://forum.ansible.com/t/something-similar-to-rubys-case-operator/15206/2 "2014-05-12T15:11:02Z")

</div>

There is no case statement in ansible.

You can look into “group\_by” when eliminating repeated conditionals if this works for you.

- hosts: webservers  
tasks:

- group\_by: key=os-{{ ansible\_os\_family }}

- hosts:webservers:&os-Debian  
tasks:

- …

etc
