gpt4 book ai didi

Ansible playbook 使用集合中定义的角色

转载 作者:行者123 更新时间:2023-12-03 14:52:36 27 4
gpt4 key购买 nike

这是我目前正在使用的 Ansible playbook 的一个示例:

---
- hosts: all

collections:
- mynamespace.my_collection

roles:
- mynamespace.my_role1
- mynamespace.my_role2
- geerlingguy.repo-remi
mynamespace.my_collection collection 是一个包含几个角色的自定义集合,即 mynamespace.my_role1mynamespace.my_role2 .
我有一个 requirements.yml文件如下:
---
collections:
- name: git@github.com:mynamespace/my_collection.git

roles:
- name: geerlingguy.repo-remi
version: "2.0.1"
我按如下方式安装集合和角色:
ansible-galaxy collection install -r /home/ansible/requirements.yml --force
ansible-galaxy role install -r /home/ansible/requirements.yml --force
每次我尝试运行剧本时,它都会失败并出现以下错误:
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
为免生疑问,我尝试了多种方式来定义剧本中的角色,包括 mynamespace.my_collection.my_role1 (集合中角色的完全限定名称)。
我怀疑我做错了什么或误解了它应该如何工作,但我的理解是一个集合可以包含多个角色,一旦安装了该集合,我应该能够在我的剧本中调用集合中的一个或多个角色使用它,但它似乎对我不起作用。
该错误似乎表明它正在寻找集合中的角色,但没有找到它。
集合安装到路径 /home/ansible_collections/mynamespace/my_collection在该目录中是 roles/my_role1roles/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

最佳答案

我将此作为问题发布在 ansible/ansible repo我们确实深入了解了这一点。
缺少的一个小线索是我的 /etc/ansible/ansible.cfg 的内容文件:

COLLECTIONS_PATHS(/etc/ansible/ansible.cfg) = ['/home/ansible_collections']
DEFAULT_ROLES_PATH(/etc/ansible/ansible.cfg) = ['/home/ansible_roles']
直接引用贡献者 Sloane Hertel 的话:

There's a bug/discrepancy between how ansible-galaxy and ansible handle the collections path. ansible-galaxy tries to be smart and adds ansible_collections to the path if it's not there already, but ansible always joins ansible_collections to the path - the playbook is trying to find the collection in /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 .
我稍微改变了我的目录结构:
home/
├─ ansible_collections/
│ ├─ mynamespace/
│ │ ├─ my_collection/
│ │ │ ├─ roles/
│ │ │ │ ├─ mynamespace
│ │ │ │ │ ├─ my_role1/
│ │ │ │ │ │ ├─ meta/
│ │ │ │ │ │ ├─ tasks/
这现在意味着我的剧本文件看起来像:
---
- hosts: all

collections:
- mynamespace.my_collection

roles:
- mynamespace.my_role1
- mynamespace.my_role2
- geerlingguy.repo-remi
现在可以正确找到角色。

关于Ansible playbook 使用集合中定义的角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64836917/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com