如何从其他角色指定Ansible Galaxy角色的依赖?

时间:2017-03-29 12:54:33

标签: ansible ansible-2.x

我通过在meta/main.yml内声明它来为我的角色指定了依赖项。

---
dependencies:
  - role: angstwad.docker_ubuntu
    src: https://github.com/angstwad/docker.ubuntu
    scm: git
    version: v2.3.0

然而,当我尝试执行Ansible playbook时,我看到错误消息:

ERROR! the role 'angstwad.docker_ubuntu' was not found in /home/.../ansible/roles:/etc/ansible/roles:/home/.../ansible/roles:/home/.../ansible

The error appears to have been in '/home/.../roles/docker-node/meta/main.yml': line 5, column 5, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  - role: angstwad.docker_ubuntu
    ^ here

从错误消息中我得出结论,即使angstwad.docker_ubuntu中已明确提及外部角色docker-node/meta/main.yml,也未导入外部角色ansible-galaxy

即使我的角色本身尚未上传到Ansible Galaxy,我是否可以指定Ansible Galaxy的外部依赖项?

或者我是否需要使用{{1}}明确导入它们?

来自Ansible documentation

  

角色也可以依赖于其他角色,并且在安装时也是如此   具有依赖关系的角色,这些依赖关系将自动成为   安装。

     

通过提供a,在meta / main.yml文件中指定角色依赖项   角色列表。如果角色的来源是Galaxy,你可以简单地说   以username.role_name格式指定角色。越复杂   您也可以支持requirements.yml中使用的格式   提供src,scm,版本和名称。

通过遵循另一个SO问题(How to automatically install Ansible Galaxy roles?)的建议,听起来需要手动或通过解决方法明确地下载外部依赖项。

2 个答案:

答案 0 :(得分:0)

根据documentation you linked,您会看到:

  

ansible-galaxy遇到依赖关系时,它会自动将每个依赖关系安装到roles_path。

下载依赖项仅由ansible-galaxy处理,src元信息不会影响ansible-playbook,它只是查看你的角色目录,如果它没有看到相应的文件夹 - 它会失败。和answer in question you linked一样,你确实可以创建第一个任务,或者在每个开始之前包含在你的剧本中运行ansible galaxy。

答案 1 :(得分:0)

如果具有此依赖关系的角色本身是通过ansible-galaxy安装的,则修改meta/main.yml如下所示:

---
dependencies:
  - src: https://github.com/angstwad/docker.ubuntu.git
    scm: git
    version: v2.3.0

因此,在安装主要角色时,docker.ubuntu角色将自动安装在~/.ansible/roles/docker.ubuntu中,并在执行剧本时运行。

如果具有此依赖关系的角色将不会通过ansible-galaxy安装,也就是说,如果依赖关系是通过ansible-galaxy命令安装的唯一东西,那么docker.ubuntu将必须单独安装,例如

ansible-galaxy install git+https://github.com/angstwad/docker.ubuntu.git

ansible-galaxy install git+https://github.com/angstwad/docker.ubuntu.git -p /path_to/my_playbook/roles

这会将角色放置在您的搜索路径中(同样,如果未通过~/.ansible/roles/docker.ubuntu指定,则为-p)。在这种情况下,将meta/main.yml修改为此:

---
dependencies:
  - role: docker.ubuntu

请注意,从github上拉角色时,其登陆位置为docker.ubuntudocker_ubuntu。有关更多信息,请参见https://galaxy.ansible.com/docs/using/installing.html#dependencies。希望这会有所帮助。

相关问题