Roles in collection and AWX

Hi.
I have my own collection with some roles in it. I have collections/requirements.yml file. I install collection with

ansible-galaxy collection install -r collections/requirements.yml -p collections

I also have colletions_paths = ./collections in ansible.cfg.

Part of my playbook looks like:

...
roles:
  - role: mynamespace.mycollection.my_role
...

It works fine unitl I try it to run on AWX which gives me an error

ERROR! the role 'mynamespace.mycollection.my_role' was not found in /runner/project/certificates/roles:/runner/requirements_roles:/runner/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/runner/project/certificates

My question is: Is it possible to use roles in collection with AWX templates?

Thank you

Hello,

Just adding $ANSIBLE_HOME/collections to collections_path should be enough. You don’t even need a requirements.yml file this way (I see you already installed the collection correctly on the project folder, so that must be it). I’m showing what I use myself on ./ansible.cfg

[defaults]
collections_path = "/home/user/.ansible/collections:/usr/share/ansible/collections:./collections:$ANSIBLE_HOME/collections"

Cheers

1 Like

@jbericat thank you, but your solution is not working in AWX, only locally.

1 Like

Ok let me try it myself because that is something I do every now and then… I’ll get back to you shortly

Hello again,

Here in this repo I created a test collection with just one role:

Then I created a Job template from the test_forum_dkoci.yml playbook and I ran it on my AWX 23.0.0:

I suggest you compare your collection with mine, see what makes yours to fail when running on AWX. Just one thought here: Are you using ansible-galaxy collection init to create your collection folder structure, or did you build it from scratch? Your problem may be related to this…

Hope it helps!

1 Like

Your role/Collection it’s published on ansible galaxy o is local to your playbook?

If it’s not published and is in separate repo respect to your playbook, you need to tell to awx wheree you need to download collection/role with src params in requirements.yml.

If role/Collection is inside your playbook, you need to have a folder tree like collections/ansible_collections/mynamespace/mycollection (as per example of @jbericat.)

If your Collection is in ansible galaxy, awx will download it automatically.

2 Likes

@jbericat Thank you for sharing the project. The difference between your project and mine is that I don’t have collections in the same project as the playbook. I have 2 gitlab projects. One contains collections, and the other contains the playbook where I want to use roles from the collection. The project with the playbook has a ‘collections’ directory with a ‘requirements.yml’ file containing the URL to the collection. As far as I know, AWX should install all the collections listed in ‘requirements.yml’ before running the template.

My collections/requiremets.yml looks like

collections:

  • name: git+https://mygitlab/mynamespace.mycollection.git

Hello,

I see, thanks for sharing further details. I’ll replicate it again to illustrate my answer better.

Meanwhile, since I see you’re online now; try copying requirements.yml to the root of your project. Also; check & share your synced project log on AWX, it should look similar to this;

...
TASK [Fetch galaxy roles from requirements.(yml/yaml)] *************************
skipping: [localhost]
TASK [Fetch galaxy collections from collections/requirements.(yml/yaml)] *******
changed: [localhost] => (item=/var/lib/awx/projects/XXX/collections/requirements.yml)
PLAY RECAP *********************************************************************localhost                  : ok=4    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0 
1 Like

Logs from project sync:

PLAY [Install content with ansible-galaxy command if necessary] ****************

TASK [Warn about disabled content sync] ****************************************

skipping: [localhost]

TASK [End play due to disabled content sync] ***********************************

skipping: [localhost]

TASK [Fetch galaxy roles from roles/requirements.(yml/yaml)] *******************

[WARNING]: Unable to find ‘/var/lib/awx/projects/_11__okd_test_files/roles’ in

expected paths (use -vvvvv to see paths)

skipping: [localhost]

TASK [Fetch galaxy collections from collections/requirements.(yml/yaml)] *******

[WARNING]: Unable to find

‘/var/lib/awx/projects/_11__okd_test_files/collections’ in expected paths (use

-vvvvv to see paths)

skipping: [localhost]

TASK [Fetch galaxy roles and collections from requirements.(yml/yaml)] *********

skipping: [localhost]

Here I see some clues. Try copying your requirements file to root as I said, it looks like AWX is searching for roles on this path → ./requirements.yml

Also, adjust its format to match the one suggested on the docs

collections:
  - name: https://github.com/organization/repo_name.git
    type: git
    version: devel

EDIT: If you get your collection from Galaxy / Git Repo, you don’t have to install locally on the project, nor add the collection path to ansible.cfg (as pointed-out by @tanganellilore). If you want to run the roles locally with ansible-playbook you can just install the collection to user space (/home/user/.ansible). So, you may also try to delete the collection from the project folder and disable the custom ansible.cfg collection path settings.

PS: On what version of AWX are you, btw?

1 Like

Ok the catch is to move collections/requirements.yml to the root of the project. It is working now.
Thank you very much.

1 Like

Excellent!! I’m glad we could make it work at last

Have fun! :wink:

Can your share your playbook repository path tree?

Because awx, as you can see in the log, search collections/roles requirements in different path.

If it nod found it, probably you choice the wrong file ame and/or wrong filepath.