问题描述
我正在尝试渲染模板.我必须创建一个列表为
I am trying to render a template. I have to create a list as
host:
- '111.222.333.444'
- '555.666.777.888'
必须从另一个主机文件中获取.
which has to be taken from another host file.
我正在使用这样的东西:
I am using something like this:
{% for host in groups['hostgroup'] %}
host: {{ host }}
{% endfor %}
实现结果的正确方法是什么?
What is the correct way to achieve the result?
推荐答案
假设您希望在清单中声明一个名为 host 的变量,其中包含一个 IP 地址列表,您可以尝试:
Assuming you wish to declare a variable in your inventory called host which contains a list of ip addresses, you can try:
host: {{ groups['hostgroup'] }}
或者你可以跳过声明这个变量,直接使用 {{ groups['hostgroup'] }}
在任何你打算使用 {{ host }}
Or you could skip declaring this variable and use {{ groups['hostgroup'] }}
directly wherever you plan to use {{ host }}
查看 add-quotes-join 线程 和这个 过滤插件 如果你想要引号.
Look at add-quotes-join thread and this filter plugin if you want quotes.
编辑:假设你正在渲染一个模板,使用 ansible template
模块
EDIT:Assuming you are rendering a template, using the ansible template
module
host:
{% for host in groups['hostgroup'] %}
- '{{ host }}'
{% endfor %}
这篇关于如何在 yaml 中创建动态列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!