此时,据我所知,在运行时没有自动下载角色的方法。 您最好的选择是将它们提交到您自己的仓库中,或者有适当的文档列出所有要求。 您甚至可以创建一个安装角色的飞行前剧本。 :)
您可以使用Ansible角色使用安装其他所需角色 命令模块 。
这是一个非常基本的例子 ansible-galaxy install :
ansible-galaxy install
- name: Install roles from Ansible Galaxy command: ansible-galaxy install {{ item.item }} with_items: - "{{ ansible_roles_list }}"
该 ansible_roles_list 可以作为变量或角色参数提供。
ansible_roles_list
我为此实现了一个角色,因此您也可以使用Ansible来设置(部分)控制机器: https://galaxy.ansible.com/ferrarimarco/install-roles/ 。
必须应用此角色 的 之前 强> 您要在单独的剧本中使用它安装的任何其他角色。这是因为Ansible在运行您引用它们的playbook之前检查所有角色是否可用。
另一种解决方案是使用git子模块。毕竟,Ansible Galaxy只是github存储库的目录......
我使用此命令自动添加任何Galaxy角色作为子模块:
ansible-galaxy info <package> | grep -A 1 github_repo | tr '\n' ' ' | sed -e "s/.*github_repo: \([^[:space:]]*\)[^\w]*github_user: \([^[:space:]]*\)[[:space:]]*/git submodule add git:\/\/github.com\/\2\/\1.git roles\/\2.\1/g" | sh
然后将更改提交到您的git仓库。在将来克隆您的repo时,请确保使用子模块克隆它,例如 git clone ... --recursive
git clone ... --recursive
这样做的一个优点是,git子模块总是引用特定版本(git commit-hash)。这将阻止您在生产环境中运行未经测试的更新。 Galaxy角色的新版本可能存在错误或工作方式与以前完全不同。使用git子模块,您可以决定是否以及何时将角色更新为新版本。
此外,您不必另外处理黑名单中的星系角色 .gitignore 防止将其代码提交到您的存储库。
.gitignore