我正在尝试使用ansible来获取一个简单的yaml文件的内容并将它们模板化为一个更复杂的文件(测试厨房的.kitchen.yml
,因为它发生了)
我的配置文件看起来像这样
test_platforms:
- ubuntu-16.04
- ubuntu-14.04
我想将此传递到.kitchen.yml
并将{{ test_platforms }}
中的键与特定图像相关联,例如
......
platforms:
- name: ubuntu-16.04
driver_config:
image: ubuntu:16.04 <--- This is the associated image
platform: ubuntu
- name: ubuntu-14.04
driver_config:
image: ubuntu:14.04 <--- This is the associated image
platform: ubuntu
......
我以为我可以通过查找完成此操作,例如:
platforms:
{% for platform in test_platforms %}
- name: {{ platform }}
driver_config:
image: {{ lookup('ini', 'test_platforms section=docker file=platforms.ini') }}
platform: ubuntu
{% endfor %}
...给出platforms.ini
:
[docker]
ubuntu-16.04=solita/ubuntu-systemd:16.04
ubuntu-14.04=ubuntu-upstart:14.04
我希望我可以使用参数化查找(即“jinja中的测试平台”for“loop将读入{{ test platforms }}
变量中的值列表,但这似乎不起作用有没有解决这个问题的方法,或者更好的方法呢?看起来有人已经解决了这个问题之一,但是相当广泛的谷歌搜索没有发现任何东西,而且似乎从文档。
答案 0 :(得分:1)
我认为你的模板中只有一些拼写错误。在以下块中:
platforms:
{% for platform in test_platforms %}
- name: {{ platform }}
driver_config:
image: {{ lookup('ini', 'test_platforms section=docker file=platforms.ini') }}
platform: ubuntu
{% endfor %}
有两个问题:(a)您引用test_platforms
(平台列表)而不是platform
(您的循环变量),以及(b)您是实际上并没有将platform
变量的值替换为查找表达式。试试这个:
platforms:
{% for platform in test_platforms %}
- name: {{ platform }}
driver_config:
image: {{ lookup('ini', platform + ' section=docker file=platforms.ini') }}
platform: ubuntu
{% endfor %}
如果我在名为input.yml
的文件中有这个,我使用这个剧本:
- hosts: localhost
vars:
test_platforms:
- ubuntu-16.04
- ubuntu-14.04
tasks:
- template:
src: ./input.yml
dest: ./output.yml
我得到了输出:
platforms:
- name: ubuntu-16.04
driver_config:
image: solita/ubuntu-systemd:16.04
platform: ubuntu
- name: ubuntu-14.04
driver_config:
image: ubuntu-upstart:14.04
platform: ubuntu