我正在尝试将我的一堆 Ansible 剧本、角色、任务等转换为正式的Ansible 集合,以更好地支持重用和模块化。
TL;DR 是我在打包包含一个包含自定义模块的角色的集合时遇到问题。以前,我可以从角色中的任何任务文件调用角色的自定义模块,而无需完全限定集合名称 (FQCN)。但是,将角色打包为集合的一部分后,Ansible 表示它无法解析角色的自定义模块名称。
在这个集合中,我拥有以下所有内容:
- 一本剧本,
foobook.yml
- 自定义模块,
my_baz_module
- 职务,
bar_role
- 中的自定义模块
bar_role
,my_bar_role_module
这导致以下目录结构:
mynamespace/
\─ mycollection/
|─ playbooks/
| \─ foobook.yml
├─ plugins/
| \─ modules/
| \─ my_baz_module.py
\─ roles/
\─ bar_role/
├─ tasks/
| \─ main.yml
\─ library/
\─ my_bar_role_module.py
这种结构让我可以使用 运行剧本ansible-playbook mynamespace.mycollection.foobook ...
,并在该剧本中使用 访问角色role: mynamespace.mycollection.bar_role
。在角色或剧本中,我可以调用my_baz_module
如下使用的任务:
- name: My task
mynamespace.mycollection.my_baz_module:
...
但是,在roles/bar_role/tasks/main.yml
我无法调用以下任一任务或出现以下错误:
# 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.
需要明确的是:这是与向集合结构过渡直接相关的变化。在将此内容移动到集合之前,我可以my_bar_role_module
毫无问题地从任何任务文件中调用bar_role
。有没有办法支持这种结构而不单独打包角色或移动my_bar_role_module
到集合范围插件目录?
编辑:剧本正在与ansible-playbook mynamespace.mycollection.foobook
. 下面是一个示例剧本:
# foobook.yml
- hosts: localhost
roles:
- role: mynamespace.mycollection.bar_role