0

Follow these project structures:

ansible-project1
+--playbooks
   playbook1.yml
+--roles
   +--role1
   +--role2

ansible-project2
+--playbooks
   playbook2.yml
+--roles
   +--role3
   +--role4

Is it possible to have two different ansible projects, where a playbook of ansible-project2 use its own roles and roles from ansible-project1?

Is it possible to include a playbook from ansible-project1 in other playbook on ansible-project2 where first playbook is using roles from its roles folder? For example, following projects structures: playbook2.yml include playbook1.yml, where playbook1.yml use role1.

I don't know if it's important to mention but ansible-project1 and ansible-project2 are different git repos.

OscarRP
  • 85
  • 2
  • 10
  • 1
    You should version your roles separately, either on their own or inside a collection. Collections can also contain modules, filters.... and playbooks. Once this is done, you can reuse your role/collection anywhere with the ansible-galaxy utility. Altough not really the same question, I wrote [this answer](https://stackoverflow.com/a/55774721/9401096) regarding roles (collection did not exist back then). To adapt to a collection see the [documentation](https://docs.ansible.com/ansible/latest/galaxy/user_guide.html#install-multiple-collections-with-a-requirements-file). – Zeitounator Feb 09 '21 at 05:33
  • @Zeitounator Thanks for your response, now is much clear. Anyway I have a doubt about your linked answer. If I use that approach with dependencies in meta/main.yml file, the parent roles are executed also. Exist some way to only execute a role an its dependencies only when are called? – OscarRP Feb 09 '21 at 15:22
  • If you declare deps on roles, the deps are always played before the role itself. If you don't want that, don't declare deps. – Zeitounator Feb 09 '21 at 15:40
  • @Zeitounator But will not be downloaded when ansible-galaxy install is executed. What I have to do If I have roleA that include or use roleB? For example execution order could be: playbook1 >> use role A >> and role A use roleB. How I can get this behavior only set role A as requirement of playbook1? – OscarRP Feb 09 '21 at 15:57
  • @Zeitounator is it possible that meta/requirements.yml on Ansible 2.10 solve this. https://docs.ansible.com/ansible/latest/galaxy/user_guide.html#using-meta-requirements-yml – OscarRP Feb 09 '21 at 16:21
  • It could I actually didn't know about it and never tried. Let me know if it fixes your issue. – Zeitounator Feb 09 '21 at 16:26

2 Answers2

2

Ansible galaxy solves this problem and it’s included with Ansible. Generate a new role using it and, rather than sharing it publicly, store it in your version control system as a separate repo.

Shareable Ansible galaxy roles can then be added to a requirements file and downloaded, as is a standard with python.

0

We can reuse custom ansible roles if they are in same repo but I'm not sure if reuse is possible if they are in different Git Repo. Ansible Galaxy roles can be reused.

altair66
  • 236
  • 1
  • 4