Running playbook in ansible collection

Hi,
I am writing an ansible collection to configure some internal company tools.
Inside this collection I want to have several roles. These roles are very similar to each other, so I want to put some shared utility functions and classes in some shared location. As far as I understand, that’s what the plugins/module_utils directory in a collection is for.
So here is my collection structure:

.
├── README.md
└── ansible_collections
└── company
└── iam
├── README.md
├── docs
├── galaxy.yml
├── company-iam-1.0.0.tar.gz
├── meta
│ └── runtime.yml
├── playbooks
│ └── playbook.yml
├── plugins
│ ├── README.md
│ └── module_utils
│ ├── api_wrapper.py
│ ├── synchronizer.py
│ └── utils.py
├── roles
│ └── user_groups
│ ├── README.md
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── library
│ │ └── user_groups_sync.py
│ ├── meta
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ └── vars
│ └── main.yml
├── tests
│ ├── output
....

I want to call the playbook in playbooks/playbook.yml, that should run the role user_groups and run the code inside the library/user_groups_sync.py.

This is what the playbook looks like:

Hi,

I am writing an ansible collection to configure some internal company
tools. Inside this collection I want to have several roles. These
roles are very similar to each other, so I want to put some shared
utility functions and classes in some shared location. As far as I
understand, that's what the `plugins/module_utils` directory in a
collection is for. So here is my collection structure:

```
.
├── README.md
└── ansible_collections
    └── company
        └── iam
            ├── README.md
            ├── docs
            ├── galaxy.yml
            ├── company-iam-1.0.0.tar.gz
            ├── meta
            │ └── runtime.yml
            ├── playbooks
            │ └── playbook.yml
            ├── plugins
            │ ├── README.md
            │ └── module_utils
            │ ├── api_wrapper.py
            │ ├── synchronizer.py
            │ └── utils.py
            ├── roles
            │ └── user_groups
            │ ├── README.md
            │ ├── defaults
            │ │ └── main.yml
            │ ├── files
            │ ├── handlers
            │ │ └── main.yml
            │ ├── library
            │ │ └── user_groups_sync.py

`library` (and similar directories for plugins) do not work for roles
inside collections. You need to make user_groups_sync a proper module
in the collection, i.e. put user_groups_sync.py in
ansible_collections/company/iam/plugins/modules/ and refer to it using
the FQCN `company.iam.user_groups_sync`.

Cheers,
Felix

Hey Emilio,

I hope I don’t waste your time with bad advice. My experience with collections has been “interesting” - not always in a productive way. So here goes.

While it’s certainly possible to package playbooks in collections, that might be adding to the problem. It appears you’re invoking your playbook.yml while your current working directory is ./ansible_collections/company/iam/playbooks. But if you want to invoke a playbook that’s part of the collection, then you want to use its FQCN:

   ansible-playbook company.iam.playbook.yml

But that is only going to work if ansible can locate your collection(s). If you say

   ansible-galaxy collection list

and it doesn’t list company.iam, then you need to fix up your collections_path to include the directory containing your ansible_collections directory.

While I was writing this, Felix F. chimed in with some actual knowledge. If you do what he said in addition to my suggestions, you should be well on your way to, er, more interesting problems!

Good luck,

Hi Felix, Todd:

Thank you for your answers.
I managed to get my code to run.
I had to do some restructuring! I put shared code in module_utils/ and then user_groups_sync.py in modules/

Moving on to more interesting problems!

Emilio