我正在尝试使用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 %}...given a platforms.ini
[docker]
ubuntu-16.04=solita/ubuntu-systemd:16.04
ubuntu-14.04=ubuntu-upstart:14.04我希望我可以使用参数化的查找(即“jinja中的测试平台”for )循环会在{{ test platforms }}变量中的值列表中读取,但这似乎不起作用。这是否有一个修复方法,或者更好的方法?似乎有人已经解决了其中一件事情,但我在谷歌上相当广泛的搜索并没有发现任何东西,从文档中也看不出来。
发布于 2017-09-29 16:48:34
我想你的模板上有一些排字。在以下块中:
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: ubuntuhttps://stackoverflow.com/questions/46491652
复制相似问题