Best way to organize your playbooks

I have a several projects which describing installation 2 different applications. Each directory for application:

app1/roles/common/main.yml
app2/roles/common/main.yml

But application have similar between them tasks which described in “main.yml” of role “common”

What is the best way have only one source to those similar tasks without having 2 or more versions and duplicate them across projects. From my point of view:

It can be separate dir:
playbooks/common_tasks.yml

Which will be imported to those “main.yml” files:

app1/roles/common/main.yml
app2/roles/common/main.yml

But maybe someone else see a better solution?

You could make use of the roles_path configuration setting [0] and have
a directory structure like this:

ansible/
  common/roles/
  app1/roles/
  app2/roles/

and in the app folders each an ansible.cfg with:

roles_path: ./roles:../common/roles

[0]
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-roles-path

Awesome! Looks like a plan! Thanks!

чт, 1 авг. 2019 г. в 07:44, Sebastian Meyer <meyer@b1-systems.de>:

You might re-examine whether it is appropriate for a role and a playbook as it feels like your playbooks are too heavy.

Roles ideally should be reusable logic for a given context you can apply to multiple situations. For example, you might have multiple kafka/mysql/etc clusters for different uses that a single role given the necessary info via playbook and inventory can manage. If you need to build higher order reusable roles, you can have roles with dependent roles that compose them to your ends; leaving your playbooks thinner. Playbooks ultimately should be highest order orchestration that isn’t bogged down into all of the technical details.

Say what you will about Puppet but I would consider the reasoning behind their roles and profiles and how it applies to Ansible: https://puppet.com/docs/pe/2018.1/the_roles_and_profiles_method.html

Think re-examine how i look into the roles is helped to me. Now after i reorganize my roles slightly different is helped for me:

Each role have purpose of install nginx, monitoring, etc.

And after that high level playbook will combine them together to get application worked. For example distributed wordpress, or ha lamp stack rather than I have before each role to configure application.