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 On Thursday, September 7, 2023 at 4:57:38 PM UTC-4 Todd Lewis wrote: > 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, > -- > Todd > > > On 9/7/23 9:17 AM, Emilio Botero wrote: > > 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: > > --- > - hosts: localhost > gather_facts: false > collections: > - company.iam > vars: > user_groups_file: "../user_group_vars.yml" > > roles: > - role: company.iam.user_groups # Use FQCN here > > here is what I am doing (inside the playbooks directory) > > playbooks git:(ansible_collection) ✗ ansible-playbook playbook.yml > > [WARNING]: No inventory was parsed, only implicit localhost is available > [WARNING]: provided hosts list is empty, only localhost is available. Note > that the implicit localhost does not match 'all' > ERROR! couldn't resolve module/action 'user_groups_sync'. This often > indicates a misspelling, missing collection, or incorrect module path. > > I am really new to ansible collections and I'm afraid I haven't gotten > very far by reading the documentation. Could I please get some help here? > I'd really appreciate it. > > -- > You received this message because you are subscribed to the Google Groups > "Ansible Project" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ansible-project/f915213d-ff2a-4686-b96b-0232eb7aac76n%40googlegroups.com > > <https://groups.google.com/d/msgid/ansible-project/f915213d-ff2a-4686-b96b-0232eb7aac76n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > > -- > Todd > > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/40839cba-79c5-4cdc-a87a-3ad0b2ac3778n%40googlegroups.com.
