# Playbooks Simplify

**URL:** <https://forum.ansible.com/t/playbooks-simplify/12301>\
**Category:** Ansible Project\
**Tags:** ansible-project, ubuntu\
**Created:** [February 7, 2013, 8:27am UTC](https://forum.ansible.com/t/playbooks-simplify/12301 "2013-02-07T08:27:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Acyl](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Acyl](https://forum.ansible.com/u/Acyl)\
**Post date:** [February 7, 2013, 8:27am UTC](https://forum.ansible.com/t/playbooks-simplify/12301/1 "2013-02-07T08:27:32Z")

</div>

Hello,

Is it possible to use a variable which will choose a module or another according to OS type in a playbook ?  
My IT infrastructure is composed of machines CentOS, Redhat, Debian and other OS. I would like to reduce the size and complexity of my playbook, by adding a condition which allows Ansible to choose the package manager (yum or apt according to OS type).

Here are files contents:

external\_vars.yml :  
is\_centos: “‘$ansible\_distribution’ == ‘CentOS’”  
is\_debian: “‘$ansible\_distribution’ == ‘Debian’”  
is\_redhat: “‘$ansible\_distribution’ == ‘RedHat’”  
is\_deblike: “(‘$ansible\_distribution’ == ‘Ubuntu’) or (‘$ansible\_distribution’ == ‘Debian’)”  
is\_rpmlike: “(‘$ansible\_distribution’ == ‘CentOS’) or (‘$ansible\_distribution’ == ‘RedHat’)”

my\_playbook.yml:

- name: Verify that phpmyadmin is installed for rpmlike OS  
action: yum pkg= phpmyadmin state=installed  
only\_if: ‘$is\_rpmlike’
- name: Verify that phpmyadmin is installed for deblike OS  
action: apt pkg= phpmyadmin state=installed  
only\_if: ‘$is\_deblike’

I hope that you will understand my question without any reading troubles because I’m french.  
Finaly, if I missed something in the documentation, please let me know.  
Best regards.

---

<div class="post-metadata">

**Author:** ![Rodney\_Quillo](https://avatars.discourse-cdn.com/v4/letter/r/7ba0ec/32.png) [@Rodney\_Quillo](https://forum.ansible.com/u/Rodney_Quillo)\
**Post date:** [February 7, 2013, 9:16am UTC](https://forum.ansible.com/t/playbooks-simplify/12301/2 "2013-02-07T09:16:39Z")

</div>

This is found on examples directory.

[https://github.com/ansible/ansible/blob/devel/examples/playbooks/conditionals\_part1.yml](https://github.com/ansible/ansible/blob/devel/examples/playbooks/conditionals_part1.yml)

...  
vars\_files:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# common variables on OS  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "vars/external\_vars.yml"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# selection if OS is different. but instead of  
$facter\_operatingsystem use $ansible\_distribution  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ["vars/$facter\_operatingsystem.yml", "vars/defaults.yml"]

...

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 7, 2013, 12:44pm UTC](https://forum.ansible.com/t/playbooks-simplify/12301/3 "2013-02-07T12:44:33Z")

</div>

There's also a fact which returns the package manager.

That all being said, this is a sometimes a non-obviously complex thing  
to do, because most likely configuration files and other things will  
be different as well, service names, and so on.

It is often much easier to just have two task include files, for  
instance, because they'll both be very short...

Apache is very different in layout between Ubuntu and Red Hat for example.

The other reason we do this is so we can expose all the "bells and  
whistles" of the individual package manager, rather than resorting to  
a least-common-denominator approach.

---

<div class="post-metadata">

**Author:** ![Acyl](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Acyl](https://forum.ansible.com/u/Acyl)\
**Post date:** [February 7, 2013, 5:19pm UTC](https://forum.ansible.com/t/playbooks-simplify/12301/4 "2013-02-07T17:19:18Z")

</div>

I took inspiration from conditionals\_part1.yml creating three files called Debian.yml, CentOS.yml and RedHat.yml in which I declared a variable packager.  
Now my playbook works well.  
Thank you very much for your help!  
Acyl.

2013/2/7 Michael DeHaan \<[michael.dehaan@gmail.com](mailto:michael.dehaan@gmail.com)\>
