【问题标题】:Ansible playbook which uses a role defined in a collectionAnsible playbook 使用集合中定义的角色
【发布时间】:2020-11-14 17:55:17
【问题描述】:

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

---
- hosts: all

  collections:
    - mynamespace.my_collection

  roles:
    - mynamespace.my_role1
    - mynamespace.my_role2
    - geerlingguy.repo-remi

mynamespace.my_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

每次我尝试运行 playbook 时都会失败并出现以下错误:

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

【问题讨论】:

  • 根据我在文档中收集的信息,您应该完全限定您的角色:mynamespace.mycollection.role1。这在本文档部分的结尾处注明:docs.ansible.com/ansible/latest/dev_guide/…
  • 谢谢。我完全同意这就是文档所说的。我已经试过了。我将编辑问题以澄清(我想我已经提到过)。

标签: ansible ansible-role


【解决方案1】:

我将此作为问题发布在 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 的话:

ansible-galaxyansible 处理集合路径的方式之间存在错误/差异。 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

我稍微改变了我的目录结构:

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

现在可以正确找到角色。

【讨论】:

    【解决方案2】:

    ansible playbook,原名converge。尝试将您的工作区转换为只读数据结构的 yml 文件格式。

    【讨论】:

    • 我使用的所有配置文件都已经使用yml文件格式。
    • 你在使用 docker 吗?
    猜你喜欢
    • 2019-03-30
    • 2019-11-20
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    相关资源
    最近更新 更多