Hello,
I’ve written a role to install and configure Apache, the role is at the moment specific to Debian.
This means my role/apache/tasks/main.yml contains corresponding tasks.
I need also to support CentOS.
I could add corresponding tasks to the same file and add a when clause for each task, based on OS version.
But it makes more sense to create a specific role for each os: apache-debian and apache-centos
My main playbook, site.yml include the apache role:
role:
- apache
I’d like my playbook to describe I want Apache installed and the apache role to install the right role based on the host OS.
As a task file can’t conditionally include (yet!) a role, the only option (not tested) I’ve come so far to implement it, is to use role dependency and when clause by putting in role/apache/meta/main.yml:
dependencies:
-
{ role: apache-debian, when: “ansible_os_family == ‘Debian’” }
-
{ role: apache-centos, when: “ansible_os_family == ‘CentOS’” }
and role/apache/tasks/main.yml would be empty and just acts as a dispatcher.
This way my apache role encapsulates the apache-debian and apache-centos roles, so far so good.
But I’d like these sub-roles to be packaged with the apache role to be shared easily.
This means they could be defined in a “roles” sub-directory of the apache role.
I don’t think it’s currently possible so the sub-roles should be located at the same level than my shared apache role, which is far from perfect ![]()
Do you see other options to achieve clean role encapsulation and packaging?
Thanks
@sebbrochet