Cha*_*ard 5 bash yaml substitution ansible
我们为许多Ansible角色提供了私人git仓库。回购主机因站点而异,例如:
https://gitforsite1.ourdomain.com https://gitforsite2.ourdomain.com我想要的是只有一个requirements.yml文件,并替换正确的git repo。我可以这样做的一种方法是让bash脚本设置环境变量:
#!/bin/bash
...
if [ "$1" = "site1" ]; then
export REPO_ROOT="https://gitforsite1.ourdomain.com"
fi
if [ "$1" = "site2" ]; then
export REPO_ROOT="https://gitforsite2.ourdomain.com"
fi
... error checking if the value is not site1 or site2 ...
# Then install the roles
ansible-galaxy install -f -r config/requirements.yml -p roles
Run Code Online (Sandbox Code Playgroud)
然后将该值替换为requirements.yml:
---
- src: {{ lookup('env', 'REPO_ROOT') }}/role1.git
name: role1
- src: {{ lookup('env', 'REPO_ROOT') }}/role.git
name: role2
Run Code Online (Sandbox Code Playgroud)
这种方法给出:ERROR! Unable to load data from the requirements file建议文件结构不正确。(可能是该方法有效,并且我弄错了语法。)
任何让我设置变量的方法(环境,命令行等)都可以。或者,如果不支持此操作,是否需要requirements.yml在运行时重写文件,也许使用sed?
编辑1:ansible-galaxy在上面的bash脚本摘录中
添加了该行,以显示requirements.yml文件的使用方式。我认为这就是问题所在:ansible-galaxy没有扩展变量替换,无论是包含在group_vars/all环境中还是环境中。在Python 2.7.10中使用Ansible 2.3.1.0版。
编辑2:在文档中
发现有一个server选项可以指向中的另一个Galaxy实例ansible.cfg,如下所示:
[galaxy]
server=https://gitforsite1.ourdomain.com
Run Code Online (Sandbox Code Playgroud)
Galaxy 确实使用此设置,但它必须是完整的Galaxy Web应用程序,因为它会调用https://gitforsite1.ourdomain.com/api。因此,这也无济于事。
当它们以 开头时{,您应该引用与源关联的映射中的值。如果不是,yaml 解析器将尝试将该值解析为流式映射而不是标量:
- src: "{{ lookup('env', 'REPO_ROOT') }}/role1.git"
name: role1
Run Code Online (Sandbox Code Playgroud)
由于标量中有单引号,没有双引号,也没有任何反斜杠 ( \),因此我在标量周围使用了双引号。如果标量中没有单引号或者有任何反斜杠,则最好使用单引号。如果您有两种类型的引号,请使用单引号并将标量内的任何单引号加双引号。以下将加载与上面相同的内容:
- src: '{{ lookup(''env'', ''REPO_ROOT'') }}/role1.git'
name: role1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1191 次 |
| 最近记录: |