使用集合中定义的角色的 Ansible 剧本
Ansible playbook which uses a role defined in a collection
这是我目前正在使用的 Ansible 剧本的示例:
---
- hosts: all
collections:
- mynamespace.my_collection
roles:
- mynamespace.my_role1
- mynamespace.my_role2
- geerlingguy.repo-remi
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"
然后我按如下方式安装集合和角色:
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_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
ansible playbook,原名converge。尝试将您的工作区转换为 yml 文件格式以获取只读数据结构。
我将此作为问题发布在 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 剧本的示例:
---
- hosts: all
collections:
- mynamespace.my_collection
roles:
- mynamespace.my_role1
- mynamespace.my_role2
- geerlingguy.repo-remi
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"
然后我按如下方式安装集合和角色:
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_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
ansible playbook,原名converge。尝试将您的工作区转换为 yml 文件格式以获取只读数据结构。
我将此作为问题发布在 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
andansible
handle the collections path.ansible-galaxy
tries to be smart and addsansible_collections
to the path if it's not there already, butansible
always joinsansible_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
现在可以正确找到角色。