Ansible集合中的文件结构与基本的角色结构略有不同。集合的角色路径必须以'/collections”开头,而不是以'/roles”开头。可以使用以下命令找到集合角色的路径:
ansible-config dump | grep DEFAULT_COLLECTION_ROLE_DIRS
输出将显示'集合角色路径”,类似于以下内容:
DEFAULT_COLLECTION_ROLE_DIRS(default) = [u'/usr/local/share/ansible/collections']
在此示例中,集合的角色路径为'/usr/local/share/ansible/collections”,而不是'/usr/local/share/ansible/roles”。
因此,为了在集合中使用角色,需要将角色放置在正确的目录结构下,例如:
/collections//roles//
其中,collection_name是集合名称,role_name是角色名称。
例如,如果命名集合为'my_collection”,角色为'webserver”,则目录结构如下:
/my_collection-1.0.0/collections/my_collection/roles/webserver/
在Playbook或任务中使用集合中的角色,只需在角色名称前添加'
- name: Execute role from collection
hosts: servers
tasks:
- name: Include role from collection
include_role:
name: my_collection.webserver
这将使用名称为'webserver”的角色,该角色位于名称为'my_collection”的集合中。
上一篇:Ansible:解析命令输出