Spo*_*nts 7 ansible ansible-role
这是我目前正在使用的 Ansible playbook 的一个示例:
---
- hosts: all
collections:
- mynamespace.my_collection
roles:
- mynamespace.my_role1
- mynamespace.my_role2
- geerlingguy.repo-remi
Run Code Online (Sandbox Code Playgroud)
该mynamespace.my_collection集合是一个自定义集合,其中包含几个角色,即mynamespace.my_role1和mynamespace.my_role2。
我有一个requirements.yml文件如下:
---
collections:
- name: git@github.com:mynamespace/my_collection.git
roles:
- name: geerlingguy.repo-remi
version: "2.0.1"
Run Code Online (Sandbox Code Playgroud)
我按如下方式安装集合和角色:
ansible-galaxy collection install -r /home/ansible/requirements.yml --force
ansible-galaxy role install -r /home/ansible/requirements.yml --force
Run Code Online (Sandbox Code Playgroud)
每次我尝试运行剧本时,它都会失败并出现以下错误:
ERROR! the role 'mynamespace.my_role1' was not found in mynamespace.my_collection:ansible.legacy:/home/ansible/roles:/home/ansible_roles:/home/ansible
The error appears to be in '/home/ansible/play.yml': line 42, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
roles:
- mynamespace.my_role1
^ here
Run Code Online (Sandbox Code Playgroud)
为免生疑问,我尝试了多种方法来定义剧本中的角色,包括mynamespace.my_collection.my_role1(集合中角色的完全限定名称)。
我怀疑我做错了什么或误解了它应该如何工作,但我的理解是一个集合可以包含多个角色,一旦安装了该集合,我应该能够在我的剧本中调用集合中的一个或多个角色使用它,但它似乎对我不起作用。
该错误似乎表明它正在寻找集合中的角色,但没有找到它。
该集合安装到路径中,/home/ansible_collections/mynamespace/my_collection并且在该目录中是roles/my_role1和roles/my_role2。
也许集合中角色的结构是错误的?
我在 CentOS 8 上使用 Ansible 2.10。
感谢您的任何建议!
编辑:
我只是想扩展我之前提到的一些东西。我相信文档说应该使用完全限定的名称来引用剧本中集合中的角色。
不幸的是,这个错误也是:
ERROR! the role 'mynamespace.my_collection.my_role1' was not found in mynamespace.my_collection:ansible.legacy:/home/ansible/roles:/home/ansible_roles:/home/ansible
The error appears to be in '/home/ansible/play.yml': line 42, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
roles:
- mynamespace.my_collection.my_role1
^ here
Run Code Online (Sandbox Code Playgroud)
我确实查到了这件事的真相。
\n缺少的一个小线索是我的文件内容/etc/ansible/ansible.cfg:
COLLECTIONS_PATHS(/etc/ansible/ansible.cfg) = [\'/home/ansible_collections\']\nDEFAULT_ROLES_PATH(/etc/ansible/ansible.cfg) = [\'/home/ansible_roles\']\nRun Code Online (Sandbox Code Playgroud)\n直接引用贡献者 Sloane Hertel 的话:
\n\n\n\n
ansible-galaxy如何和ansible处理集合路径之间存在错误/差异。ansible-galaxy尝试变得聪明并添加ansible_collections到路径中(如果路径尚不存在),但ansible始终加入ansible_collections到路径中 - 剧本试图在 中查找集合/home/ansible_collections/ansible_collections/。
因此,解决方案是将我的COLLECTIONS_PATHS值从更改/home/ansible_collections为/home。
从那时起,ansible-playbook将搜索路径中集合中的任何角色,/home/ansible_collections/mynamespace/roles而不是/home/ansible_collections/ansible_collections/mynamespace/roles.
我稍微改变了我的目录结构:
\nhome/\n\xe2\x94\x9c\xe2\x94\x80 ansible_collections/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 mynamespace/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 my_collection/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 roles/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 mynamespace\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 my_role1/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 meta/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 tasks/\nRun Code Online (Sandbox Code Playgroud)\n现在这意味着我的剧本文件如下所示:
\nCOLLECTIONS_PATHS(/etc/ansible/ansible.cfg) = [\'/home/ansible_collections\']\nDEFAULT_ROLES_PATH(/etc/ansible/ansible.cfg) = [\'/home/ansible_roles\']\nRun Code Online (Sandbox Code Playgroud)\n现在可以正确找到角色。
\n