3

We have 3 repository, let say repo Y,Z and W.

There are roles under repository W that repository Y and Z want to use.

Share role in repository diagram

From reading, it seem we need to set requirements.yml as below,

- src: git@github.com:SOMEREPOSITORY/test-role.git
  version: main
  name: test_role

However, we still not success to use the share roles,

ERROR! the role 'test_role' was not found in /home/coder/provision/Build/roles:/home/coder/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/home/coder/provision/Build

is there need to be configure in ansible.cfg file that we missed?

MrAZ
  • 394
  • 3
  • 13
  • Long story short: either package each repo as a collection containing multiple roles and install the collections in project W or package each role in its own repository and import roles independently in project W. – Zeitounator Jun 28 '21 at 07:51
  • if use this method, when the roles have updated.. is it we need to update the package also? – MrAZ Jun 28 '21 at 08:02
  • Not if you point directly to your git repo as you did in the above example. – Zeitounator Jun 28 '21 at 10:03
  • Could be a typo. One place you have `test_role` and in the other `test-role`. – Jack Jun 28 '21 at 14:26
  • test-role is the repository name, under this repository it has role name test_role – MrAZ Jun 28 '21 at 17:14

2 Answers2

3

If you are trying to reuse roles you are a little bit behind (as in like 2010-ish era).

What you really want to do is to build a collection and reuse that. A collection can contain any number of roles and is easy to build and install. The only requirement is to use ansible 2.9 or newer.

Please read https://docs.ansible.com/ansible/latest/dev_guide/developing_collections.html

You don't need to publish a collection to galaxy.ansible.com to reuse it, you can install it directly from your git repos. The best part about it is that you do not need to mangle with ansible include paths to make use of it.

BTW, What did you use to build the diagram?

sorin
  • 161,544
  • 178
  • 535
  • 806
  • Thanks Sorin.. so if using this collection method, when we do the changes in the roles under W repository, is it will automatically reflect to each repo that link to it? Im using Visio 2013 for the diagram ... – MrAZ Jun 30 '21 at 05:53
  • Nope but keep in mind that it is up to you to setup proper CI/CD pipelines on each repository. If you have a setup where a change in one repo can easily break another one, you already failed the QA step. Testing cross-repo is possible but only few systems can properly do it, example Zuul CI. If the shared collection is not really a product by itself you may be better off using a more monolithic repository layout. You can configure the dependency to checkout specific tag but ideally you should use versioning. – sorin Jun 30 '21 at 06:56
  • Noted.. Thanks @sorin – MrAZ Jun 30 '21 at 13:35
  • Since you did mentioned that the initial method that i want to accomplish is an old method from 2010-era .. Is this method have a success case before? – MrAZ Jun 30 '21 at 13:47
3

I found the solution after multiple days.

Not Using Ansible Tower.
The solution is when we not using Ansible Tower, then we can install the role from another repository using below steps,

  1. Create the requirements.yml file under your ./roles folder The content of requirements.yml example:

    ---
    - src: git@github.com:SOMEREPOSITORY/testing-repository.git  
      name: test_role
      scm: git
    
  2. Then, run below command to install the role from another repository, ensure you in the directory same level with roles directory before execute below command:

    ansible-galaxy install --role-file roles/requirements.yml --roles-path roles/
    

Using Ansible Tower.

  1. Create the requirements.yml file under ./roles folder same with above content example.
  2. Ensure that you ./roles folder is first level directory of repository. For example:

Let say the Git repository name is Test_Repository, then ./roles directory should be inside the 'Test_Repository' folder. Since the playbook structure is essential for Ansible Tower to find the requirements.yml.

  1. Then, just lunch your playbook from Ansible Tower, the Project Sync will automatically find the roles from the requirements.yml content and used it. No additional task needed to be configure in Ansible Tower since it is automatic.

Hope this helps others.

Andrew
  • 3,912
  • 17
  • 28
MrAZ
  • 394
  • 3
  • 13