# Running playbook in ansible collection

**URL:** <https://forum.ansible.com/t/running-playbook-in-ansible-collection/38414>\
**Category:** Ansible Project\
**Tags:** galaxy-ng\
**Created:** [September 7, 2023, 1:17pm UTC](https://forum.ansible.com/t/running-playbook-in-ansible-collection/38414 "2023-09-07T13:17:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emilio\_Botero](https://avatars.discourse-cdn.com/v4/letter/e/49beb7/32.png) [@Emilio\_Botero](https://forum.ansible.com/u/Emilio_Botero)\
**Post date:** [September 7, 2023, 1:17pm UTC](https://forum.ansible.com/t/running-playbook-in-ansible-collection/38414/1 "2023-09-07T13:17:26Z")

</div>

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:

```auto
.
├── 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:

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [September 7, 2023, 8:32pm UTC](https://forum.ansible.com/t/running-playbook-in-ansible-collection/38414/2 "2023-09-07T20:32:10Z")

</div>

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  
> &nbsp;&nbsp;&nbsp;&nbsp;└── company  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└── iam  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── README.md  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── docs  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── galaxy.yml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── company-iam-1.0.0.tar.gz  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── meta  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ └── runtime.yml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── playbooks  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ └── playbook.yml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── plugins  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ ├── README.md  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ └── module\_utils  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ ├── api\_wrapper.py  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ ├── synchronizer.py  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ └── utils.py  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── roles  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ └── user\_groups  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ ├── README.md  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ ├── defaults  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ │ └── main.yml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ ├── files  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ ├── handlers  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ │ └── main.yml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ ├── library  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│ │ └── 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

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 7, 2023, 8:57pm UTC](https://forum.ansible.com/t/running-playbook-in-ansible-collection/38414/3 "2023-09-07T20:57:22Z")

</div>

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:

```auto
   ansible-playbook company.iam.playbook.yml

```

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

```auto
   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,

---

<div class="post-metadata">

**Author:** ![Emilio\_Botero](https://avatars.discourse-cdn.com/v4/letter/e/49beb7/32.png) [@Emilio\_Botero](https://forum.ansible.com/u/Emilio_Botero)\
**Post date:** [September 8, 2023, 1:36pm UTC](https://forum.ansible.com/t/running-playbook-in-ansible-collection/38414/4 "2023-09-08T13:36:31Z")

</div>

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
