我們有許多Ansible角色的私人git回購。回購主機有所不同,從站點到站點,例如:我可以在ansible-galaxy和requirements.yml中使用變量替換嗎?
- 站點1使用
https://gitforsite1.ourdomain.com
- 站點2使用
https://gitforsite2.ourdomain.com
我要的是有一個單一requirements.yml
文件,並替換正確的混帳回購協議。我能做到這一點的方法之一是有一個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
和則替換requirements.yml
這個值:
---
- src: {{ lookup('env', 'REPO_ROOT') }}/role1.git
name: role1
- src: {{ lookup('env', 'REPO_ROOT') }}/role.git
name: role2
這種方法提供: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: 發現in the docs有一個server
選項指向另一個星系例如,在ansible.cfg
,像這樣:
[galaxy]
server=https://gitforsite1.ourdomain.com
銀河確實使用此設置,但它必須成爲完整的Galaxy網絡應用程序,因爲它叫https://gitforsite1.ourdomain.com/api
。所以這對我也沒有幫助。
爲什麼不把'REPO_ROOT'放到不同網站庫存的'group_vars/all'文件中? – Jack
Ansible的哪個版本? 1.4和更高版本,您可以使用' - src:「{{ansible_env.REPO_ROOT}}/role1.git」'。 – Deathgrip
@Jack謝謝 - 好主意,但似乎'ansible-galaxy'忽略了這些(請參閱編輯以張貼)。 – ChalkBoard