分享Ansible角色

问题描述 投票:0回答:2

我有Ansible角色包,我想将其导入到项目中。

如果我通过子目录组织它们,我会遇到与依赖相对路径有关的问题

即,如果共享角色使用元依赖关系,则需要知道其安装位置的相对位置

我希望能够将所有内容引用到正在运行的playbook目录中,但这不起作用

roles/roleA/meta
 ---
 dependencies:
   - { role: "{{ playbook_dir }}/roles/shared_roles/roleB"}

roles/shared_roles/roleB
...

我尝试了多种选择并且没有想法。

我调查了角色 - 路径http://docs.ansible.com/intro_configuration.html#roles-path

虽然我真的不想对所有角色进行唯一命名,因为它们应该被命名空间/分组。

谢谢

ansible ansible-playbook
2个回答
1
投票

您基本上可以使用ansible-galaxy~/.ansible/roles/路径中安装角色。

.ansible在我的macOS machine的路径是:~/.ansible/roles/

您需要做的就是创建一个requirements.yml文件,如下所示:

- src: git+https://<githubURL-Master-role>.git
  scm: git
  name: Master-role

- src: git+https://<githubURL-dependent1-role>.git
  scm: git
  name: dependent1-role

- src: git+https://<githubURL-dependent2-role>.git
  scm: git
  name: dependent2-role

运行此命令以在~/.ansible/roles/路径中安装您的角色:

sudo ansible-galaxy -vvv -r install requirements.yml

这将基本上在~/.ansible/roles/路径中设置您的角色。要更好地理解它,请遵循此文档https://docs.ansible.com/ansible/latest/reference_appendices/galaxy.html

然后,就像你已经提到的那样,你应该在执行##path/master-role/meta/main.yml命令之前在anisble-galaxy文件中添加所有依赖角色,以便在运行master-role playbook时在master-role之前执行依赖角色。

##path/master-role/meta/main.yml文件看起来像这样:

galaxy_info:
  author: Jithendra
  description: blah...blah...blah
  min_ansible_version: 1.2
  galaxy_tags:'[]'
    # List tags for your role here, one per line. A tag is
    # a keyword that describes and categorizes the role.
    # Users find roles by searching for tags. Be sure to
    # remove the '[]' above if you add tags to this list.
    # NOTE: A tag is limited to a single word comprised of
    # alphanumeric characters. Maximum 20 tags per role.
dependencies:
  - { role: 'Master-role, when: blah == "true"'}         #when condition is optional
  - { role: 'dependent1-role, when blah == "false"'}
  - { role: 'dependent2-role, when blah == "false"'}

0
投票

通过强制所有共享模块引用所有依赖项中的共享目录的名称来解决它。

我想把它留给调用项目,并希望依赖项与角色目录相关。

该解决方案可以工作并提供命名空间,因此我可以包含多个具有相同名称的角色,只要它们位于不同的目录中即可。

我现在有一个包含3个独立角色包的项目

 PLAY RECAP ******************************************************************** 
staging : ok=214  changed=5    unreachable=0    failed=0 

它全部使用Maven构建,每个共享组都是maven依赖。如果有人读到这个,它有助于我分享结构/ poms等。

© www.soinside.com 2019 - 2024. All rights reserved.