# Guidelines for writing cross platform roles

**URL:** <https://forum.ansible.com/t/guidelines-for-writing-cross-platform-roles/29797>\
**Category:** Ansible Project\
**Created:** [February 28, 2019, 11:51am UTC](https://forum.ansible.com/t/guidelines-for-writing-cross-platform-roles/29797 "2019-02-28T11:51:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elan](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@Elan](https://forum.ansible.com/u/Elan)\
**Post date:** [February 28, 2019, 11:51am UTC](https://forum.ansible.com/t/guidelines-for-writing-cross-platform-roles/29797/1 "2019-02-28T11:51:43Z")

</div>

Recently I have been watching some ansible conference recordings and reading about best practices and I have a question.

How do you write your playbooks/roles to for cross platform use and be able to not to turn your specification to code with frequent "if"s and to not write too much boilerplate for each OS?

Ok I can abstract out package managers for example and just use package module, but still package naming sometimes will occur, or system will not be compatible with some module.

Is this even possible to write such roles? Or is there any guidelines that help avoid code overhead?  
Sadly I didn’t found any material on that matter.

Or the only way is writing separate tasks for OS-dependent “native” stuff and adding "if"s on top role level?

---

<div class="post-metadata">

**Author:** ![Jeffrey\_Agnitsch](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@Jeffrey\_Agnitsch](https://forum.ansible.com/u/Jeffrey_Agnitsch)\
**Post date:** [February 28, 2019, 2:49pm UTC](https://forum.ansible.com/t/guidelines-for-writing-cross-platform-roles/29797/2 "2019-02-28T14:49:58Z")

</div>

What type of OS’s are you referring to ? If you are staying within the realm of Linux’s you can gather facts and then do when statement. It might also be good to just create separate named roles for the different OS.

- name: Copy | Copy Repo  
copy: src=/etc/ansible/files/repos/oracle\_local\_6.repo dest=/etc/yum.repos.d/oracle\_local\_6.repo  
when:

- ansible\_facts[‘distribution\_major\_version’] == “6”

- name: Copy | Copy Repo  
copy: src=/etc/ansible/files/repos/oracle\_local\_7.repo dest=/etc/yum.repos.d/oracle\_local\_7.repo  
when:

- ansible\_facts[‘distribution\_major\_version’] == “7”

. You could also separate out things within the inventory file such as

[debian]  
server1  
server2

[centos]  
server3  
server4

then do individual playbooks on those groups using something like

- hosts: centos  
-tasks:  
blah blah blah

- hosts: debian  
-tasks  
blah blah blah

---

<div class="post-metadata">

**Author:** ![Elan](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@Elan](https://forum.ansible.com/u/Elan)\
**Post date:** [February 28, 2019, 8:05pm UTC](https://forum.ansible.com/t/guidelines-for-writing-cross-platform-roles/29797/3 "2019-02-28T20:05:21Z")

</div>

Thank you for your answer. 🙂

I was afraid that there wasn’t a way to go without "if"s on low levels and it is only possible by separating it to playbooks. Looks like I will need to redesign my project structure to account that.
