在jinja2中将引号添加到列表的元素(可翻译) [英] Add quotes to elemens of the list in jinja2 (ansible)

查看:126
本文介绍了在jinja2中将引号添加到列表的元素(可翻译)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模板中有非常简单的一行:

I have very simple line in the template:

ip={{ip|join(', ')}}

我有IP列表:

ip:
 - 1.1.1.1
 - 2.2.2.2
 - 3.3.3.3

但是应用程序需要带引号(ip ='1.1.1.1','2.2.2.2')的IP.

But application wants IPs with quotes (ip='1.1.1.1', '2.2.2.2').

我可以这样:

ip:
 - "'1.1.1.1'"
 - "'2.2.2.2'"
 - "'3.3.3.3'"

但这非常丑陋.有没有什么好方法可以在清单的每个元素上添加引号?

But it is very ugly. Is any nice way to add quotes on each element of the list in ansible?

谢谢!

推荐答案

实际上,有一个非常简单的方法可以实现这一目标:

Actually there is a very simple method to achieve this:

{{ mylist | map('quote') | join(', ') }}

过滤器map遍历每个项目,然后让quote处理它.之后,您可以轻松地join将它们在一起.

The filter map iterates over every item and let quote process it. Afterwards you can easily join them together.

这篇关于在jinja2中将引号添加到列表的元素(可翻译)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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