Ansible can't find roles from collection

5.2k views Asked by At

I got collection that has structure

   namespace/
── collectionA/
   ├── docs/
   ├── galaxy.yml
   ├── README.md
   └── roles/
        ├── roleA/
        |     └── tasks/
        |           ├──taskA.yml
        |           ├──taskB.yml
        └── roleB/
               └── tasks/
                    ├──taskA.yml
                    ├──taskB.yml

according to using collections if I wan to use that roles all I have to do is include_role with fqdn

- hosts: all
  collections:
    - my_namespace.my_collection

tasks:
  - import_role:
      name: role1

but it seems not working. I still get error:

ERROR! the role 'manage_users' was not found in edaas.post_provisioning:ansible.legacy:/home/jenkins/agent/workspace/Create_Infra/playbooks/roles:/home/cirunner/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/home/jenkins/agent/workspace/Create_Infra/playbooks 12:10:53
12:10:53 The error appears to be in '/home/jenkins/agent/workspace/Create_Infra/playbooks/ansible_main_initial.yml': line 24, column 15, but may 12:10:53 be elsewhere in the file depending on the exact syntax problem. 12:10:53
12:10:53 The offending line appears to be: 12:10:53
12:10:53 - ansible.builtin.import_role: 12:10:53 name: manage_users 12:10:53 ^ here

Collection is installed correctly - checked by ansible-galaxy collection list

Any idea what can be still wrong? Role names are aligned to rules (lowercase and only characters with _ Collection is installed in /home/cirunner/.ansible/collections

ansible [core 2.11.12] config file = None configured module search path = ['/home/cirunner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible ansible collection location = /home/cirunner/.ansible/collections executable location = /usr/local/bin/ansible python version = 3.8.0 (default, Dec 9 2021, 17:53:27) [GCC 8.4.0] jinja version = 3.1.2 libyaml = True

2

There are 2 answers

0
M. Scho. On

I know this is old but I had a similar issue and it took me hours to find my problem. Maybe the way I figured it out will help someone else.

My problem was an empty collection directory left over from some tests in my project directory <project_dir>/collections/ansible_collection/my_namespace/my_collection directory. ansible-galaxy found the real installed collection containing the role in /usr/share/ansible/collections and reported it as installed and everything fine. But ansible-playbook found the empty directory in my project directory first and interpreted this as the collection location and didn't look further to the actual installed collection in /usr/share/ansible/collections.

So, how I figured it out and how you might be able to figure out your problem:

  1. Explicitly mention the collection in your playbook:
...
    collections:
        - my_namespace.my_collection
  1. Add at least four '-v' arguments when running your playbook: ansible-playbook ... -vvvv
  2. Search for following line and see where your collection was found: Loading collection my_namespace.my_collection from <project_dir>/collections/ansible_collections/my_namespace/my_collection

I hope it helps.

0
Braedon On

I came across this thread, and my issue was I had hyphens in my role name, which is not supported as per the documentation found here.

Changing to underscores in the role name resolved the issue.