ansible – Roles – Separating distribution specific tasks and variables inside a role

We can easily separate distribution specific tasks and variables into different dedicated .yml files.
Ansible helps us to automatically identify the target hosts distribution via {{ ansible_distribution }} and {{ ansible_distribution_version }}, so we just have to name the distribution dedicated .yml files accordingly.

For Ubuntu Xenial the basic role dir tree would then look something like that:

role
├── tasks
│   ├── main.yml
│   └── Ubuntu16.04.yml
└── vars
    └── Ubuntu16.04.yml

Inside the tasks/main.yml we can now automatically include the proper variables and tasks for the target hosts distribution.

tasks/main.yml

---

- name: include distribution specific vars
  include_vars: "{{ ansible_distribution }}{{ ansible_distribution_version }}.yml"

- name: include distribution specific install
  include: "{{ ansible_distribution }}{{ ansible_distribution_version }}.yml"

Inside tasks/Ubuntu16.06.yml and vars/Ubuntu16.04.yml we can now define tasks and variables for Ubuntu Xenial respectively.

if you want to reproduce, please indicate the source:
ansible – Roles – Separating distribution specific tasks and variables inside a role - CodeDay