How are ansible playbooks supposed to be imported into a collection?

HOW TO -️ October 18, 2021

I'm trying to develop a reusable collection of playbooks that I can run from docker containers. I don't want to share duplicate code between containers and I don't want to have containers that run multiple playbooks so what I'd like do develop is something where I have a

playbooks/doit.yml

file and a

.ansible/collections/playbooks/x/y/playbooks/thing-to-do.yml

playbook that I can import into my playbook

However, none of the documentation explains how to do that yet.

import playbooks doesn't say anything about collections

https://docs.ansible.com/ansible/latest/modules/import_playbook_module.html

and playbooks collection documentation currently says tbd

https://docs.ansible.com/ansible/latest/dev_guide/developing_collections.html#playbooks-directory

I'm running ansible 2.9, I suppose I could import the playbook given the full path, but that seems like not what they're wanting me to do.

Answer

Sounds like you want a collection of roles instead of a collection of playbooks? @BruceBecker I might, but our software architect said if its specific to one type of device it goes a playbook - if it's something we can make generic we put it in a role. So I'm stuck with playbooks there. Also, I'm jumping around between devices in the playbooks themselves and refreshing the inventory a few times (this implementation is to set up and configure a network of virtual machines and tie them in to our infrastructure so there's a lot of flitting about - need to ssh to about 15 devices in the playbooks all told) no offence meant to your architect, but that doens't track well with me. Roles are meant to be re-usable. Playbooks are strategies, they invoke roles which execute relevant tasks based on what you're throwing them at. Abstract roles should be able to pick up different vars, depending on their targets, and still do the right thing. The devices should deliver relevant facts about themselves to the roles. Using roles over playbooks gives you the explicit advantage of also being able to thoroughly test the roles. I know this may not sound helpful, but I hope it does :) It's good advice, I don't see any way around it until the powers that be flesh out what they mean by putting playbooks in collections. I was poking around the source, doesn't seem to be any way to include those playbooks. Even if I wanted to, I'd have to specify an absolute path to the playbooks in the collection. Looking through a few pages of ansible collections repos on github I've found 0 instances of people trying to do this so I guess I'll just give up. Not sure if this question is worth closing or if you want to just answer "don't do that" You could use import_playbook, with judicious use of lookup plugins to find the right ones.

Initializing...