如何在 yaml 中创建动态列表? [英] How to create dynamic list in yaml?

查看:41
本文介绍了如何在 yaml 中创建动态列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试渲染模板.我必须创建一个列表为

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 中创建动态列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆