ansibleyamlansible-role

Ansible share roles between repository


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?


Solution

  • 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.