无法转义 Ansible 中与变量连接的括号
我需要使用 Ansible 更新数据库表中的记录,我需要在字段中输入的值采用以下形式: ["\/{{name}}"]
where name< /code> 是一个变量。
因此,如果 name
的值为 Alex
,则 url 列的更新值将恰好为 ["\/Alex"]
。
我这样做如下:
- name: Update record
postgresql_query:
query: >
update table1 set url = %s
positional_args:
- '["\/{{name}}"]'
当我运行剧本后检查数据库时,我发现的值是: {'\\/Alex'}
;方括号被大括号替换,反斜杠被重复,双引号被单引号替换。
我尝试了多种解决方法,例如转义括号和反斜杠: '\["\\/{{name}}"\]'
我也尝试这样做: '{% raw %}["\/{% endraw %}{{name}}{% raw %}"]{% endraw %}'
(取自jinja2 文档),但没有任何效果。
I need to update records in a database table using Ansible, the value I need to put in the field is in the form : ["\/{{name}}"]
where name
is a variable.
So, if the value of name
is Alex
the updated value of the url column will be exactly ["\/Alex"]
.
I am doing this as below:
- name: Update record
postgresql_query:
query: >
update table1 set url = %s
positional_args:
- '["\/{{name}}"]'
When I check the database after running the playbook, the value I found is: {'\\/Alex'}
; the brackets are replaced by curly braces and the backslash is duplicated, and the double quotes are replaced by single ones.
I tried multiple work-arounds like escaping the brackets and the backslash: '\["\\/{{name}}"\]'
and I also tried doing this: '{% raw %}["\/{% endraw %}{{name}}{% raw %}"]{% endraw %}'
(took this from jinja2 docs), but none worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来你实际上必须将
name
变量转换为字符串,否则,Jinja 会以某种方式将你的列表项解释为 set —{'\/Alex'}
是 Python 中的集合。因此,您的正确语法将是:
鉴于剧本:
这会产生预期的结果:
It seems you actually have to cast your
name
variable in a string, otherwise, Jinja is somehow interpreting your list item as a set —{'\/Alex'}
is a set in Python.So, your correct syntax would be:
Given the playbook:
This yields, as expected: