Should automation developers be starting with role-only collections?

I didn’t know the answer to this, so I asked Codex to check the molecule source:

The relevant flow is:

  • get_configs() discovers molecule.yml files with wcmatch.glob() in src/molecule/command/base.py:490-529.
  • Scenarios.all then sorts the resulting scenarios with:
    scenarios.sort(key=lambda x: x.directory)
    

So with this layout:

molecule/reverse1/molecule.yml
molecule/reverse2/molecule.yml

Molecule will process:

reverse1
reverse2

The existing test tests/unit/test_scenarios_ordered.py:30-63 confirms this behavior. It supplies configurations in the order two, one, three, but uses directories named 01_foo, 02_foo, and 03_foo. Molecule returns them as one, two, three, based on directory order.

Here is an example with shared_state: true and using uv run tox --ansible -e molecule-py3.12-2.19 - the scenario sequence is that default -> create runs first, then all the scenario specific sequences, then finally default -> destroy:

DETAILS                                                                        
default ➜ create: Executed: Successful

agent_airgap ➜ prepare: Executed: Successful
agent_airgap ➜ converge: Executed: Successful
agent_airgap ➜ cleanup: Executed: Successful

agent_default ➜ prepare: Executed: Successful
agent_default ➜ converge: Executed: Successful
agent_default ➜ cleanup: Executed: Successful

agent_token ➜ prepare: Executed: Successful
agent_token ➜ converge: Executed: Successful
agent_token ➜ cleanup: Executed: Successful

hub_default ➜ converge: Executed: Successful
hub_default ➜ verify: Executed: Successful
hub_default ➜ cleanup: Executed: Successful

default ➜ destroy: Executed: Successful

SCENARIO RECAP                                                                 
default                   : actions=1  successful=1  disabled=0  skipped=0  missing=0  failed=0
agent_airgap              : actions=3  successful=3  disabled=0  skipped=0  missing=0  failed=0
agent_default             : actions=3  successful=3  disabled=0  skipped=0  missing=0  failed=0
agent_token               : actions=3  successful=3  disabled=0  skipped=0  missing=0  failed=0
hub_default               : actions=3  successful=3  disabled=0  skipped=0  missing=0  failed=0
default                   : actions=1  successful=1  disabled=0  skipped=0  missing=0  failed=0

  molecule-py3.12-2.19: OK (241.33=setup[0.01]+cmd[4.79,73.05,163.48] seconds)
  congratulations :) (241.80 seconds)

I don’t think that is possible with molecule as is today.

1 Like

Thank you for the answer.

@Jeff_Pullen @kks - The topic of reusable collection playbooks for provisioning molecule instances came up a few weeks ago in the Ansible DevTools Matrix channel.

It was related to a PR which came in to molecule-plugins to add a brand new driver: feat(lima): Add Lima driver by simi · Pull Request #387 · ansible-community/molecule-plugins · GitHub

My response (if you don’t have access to Matrix):

Thanks for the driver submission. @ziegenberg and I have recently taken over maintainership of molecule-plugins.

One thing worth highlighting is that molecule-plugins is a collection of drivers for Molecule’s pre ansible-native operating model. Since Molecule adopted the newer ansible-native model, new platform integrations are generally expected to be implemented as Ansible collections rather than as Molecule drivers.

The current documentation doesn’t make this distinction particularly clearly, so I understand why you opened this PR.

My recommendation would be to implement Lima support as an Ansible collection targeting the ansible-native model, rather than adding a new driver to molecule-plugins. That approach aligns with the current direction of the Molecule project and will be a better long term investment. We should also make this guidance much clearer in the molecule-plugins documentation. I’ll open an issue to track updating the documentation so that future contributors are directed towards the ansible-native model for new platform integrations.

The discussion then went toward the option of having a collection playbook for the molecule provisioning:

The ansible-native pattern favours usage of collections, there shouldn’t be any need to install a Python package like molecule-lima (for example).

With molecule today it is possible to do something like this:

# molecule.yml
ansible:
 playbooks:
   create: create.yml
   destroy: destroy.yml
# create.yml
- name: Create instances
  ansible.builtin.import_playbook: example.infra.create
# destroy.yml
- name: Destroy instances
  ansible.builtin.import_playbook: example.infra.destroy

Where example.infra.create and example.infra.destroy are collection playbooks containing all the tasks for handling the create / destroy sequence for the infrastructure provider. The above is like a “wrapper” approach, where you still need the create.yml and destroy.yml playbooks themselves inside the scenario directory. What I would like to see is support of the below pattern, which you mentioned above:

# molecule.yml
ansible:
 playbooks:
   create: example.infra.create
   destroy: example.infra.destroy

This is not supported currently, but if this was there wouldn’t be a need to have a create.yml or destory.yml playbook in the scenario directory at all, molecule could just directly import the collection playbook to handle the create / destroy sequence.

so what about this option, is there any chance this will be implemented?

@kks The below is already possible today:

Are you referring to the other below?

I am referring to the latter
This one:

# molecule.yml
ansible:
 playbooks:
   create: example.infra.create
   destroy: example.infra.destroy

feat: support fully qualified collection name playbook references by dbrennand · Pull Request #4735 · ansible/molecule · GitHub :slightly_smiling_face:

1 Like

That looks great, thank you!

It was possible in molecule (not sure about current version) to use explict playbook in scenario sequence configuration for side_effect (and only it), e.g.:


scenario:
  test_sequence:
    - syntax
    - destroy
    - create
    - prepare
    - converge
    - check
    - idempotence
    - side_effect <<playbook file>>
    - verify
    - cleanup
    - destroy

That would be great in side effect also supported FQCN, not only path to playbook file.