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

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

问题描述

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

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(ip='1.1.1.1', '2.2.2.2').

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'"

但是很丑.有什么好的方法可以在 ansible 的列表的每个元素上添加引号吗?

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 处理它.之后,您可以轻松地加入它们.

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

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

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