简体   繁体   中英

Ansible collection with role-scope custom modules

I am trying to convert my pile of Ansible playbooks, roles, tasks, etc into a formalized Ansible Collection to better support re-use and modularity.

The TL;DR is that I am having issues packaging a collection that includes a role that includes a custom module. Previously I could invoke a role's custom module from any taskfile in the role without a Fully Qualified Collection Name (FQCN). However, with the role packaged as part of a collection, Ansible says it cannot resolve the role's custom module name.


Within this collection I have all of the following:

  • A playbook, foobook.yml
  • A custom module, my_baz_module
  • A role, bar_role
  • A custom module within bar_role , my_bar_role_module

This leads to the below directory structure:

mynamespace/
\─ mycollection/
   |─ playbooks/
   |  \─ foobook.yml
   ├─ plugins/
   |  \─ modules/
   |     \─ my_baz_module.py
   \─ roles/
      \─ bar_role/
         ├─ tasks/
         |  \─ main.yml
         \─ library/
            \─ my_bar_role_module.py

This structure lets me run the playbook with ansible-playbook mynamespace.mycollection.foobook... , and within that playbook access the role with role: mynamespace.mycollection.bar_role . In either the role or the playbook I can invoke a task that uses my_baz_module like below:

- name: My task 
  mynamespace.mycollection.my_baz_module:
    ...

However, in roles/bar_role/tasks/main.yml I cannot invoke a task like either of the below or I get the following errors:

# method one
- name: My task
  my_bar_role_module:
    ...

# method two
- name: My task
  mynamespace.mycollection.my_bar_role_module:
    ...

# method three, though I didn't expect this to work (and it didn't)
- name: My task
  mynamespace.mycollection.bar_role.my_bar_role_module:
    ...
# Error from method one
ERROR! couldn't resolve module/action 'my_bar_role_module'. This often indicates a misspelling, missing collection, or incorrect module path.

# Error from method two
ERROR! couldn't resolve module/action 'mynamespace.mycollection.my_bar_role_module'. This often indicates a misspelling, missing collection, or incorrect module path.

To be clear: this is a change directly related to the transition to the collection structure. Before moving this content to a collection I could invoke my_bar_role_module from any taskfile in bar_role without any problems. Is there any way to support this structure without packaging the role separately or moving my_bar_role_module to the collection-scope plugins directory?

EDIT: The playbook is being launched with ansible-playbook mynamespace.mycollection.foobook . An example playbook is below:

# foobook.yml
- hosts: localhost
  roles:
    - role: mynamespace.mycollection.bar_role

Shipping modules inside collection roles is deliberately not supported ; you must include them as normal collection modules. You can if you wish use a sub-directory to indicate that the module is not intended for external use, eg by placing it at plugins/modules/internal/my_bar_module.py and calling mynamespace.mycollection.internal.my_bar_module .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM