Guidelines for writing cross platform roles

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?

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

Thank you for your answer. :slight_smile:

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.