无法转义 Ansible 中与变量连接的括号

发布于 2025-01-16 13:33:34 字数 795 浏览 3 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2025-01-23 13:33:34

看来你实际上必须将 name 变量转换为字符串,否则,Jinja 会以某种方式将你的列表项解释为 set{'\/Alex'} 是 Python 中的集合。

因此,您的正确语法将是:

- name: Update record
  postgresql_query:
    query: > 
      update table1 set url = %s
    positional_args:
      - '["\/{{ name | string }}"]'

鉴于剧本:

- hosts: localhost
  gather_facts: no

  tasks:
    - postgresql_query:
        query: >-
          update table1 set url = %s
        positional_args:
          - '["\/{{ name | string }}"]'
      vars:
        name: Alex
      register: sql

    - debug: 
        var: sql.query

这会产生预期的结果:

TASK [postgresql_query] ******************************************************
changed: [localhost]

TASK [debug] *****************************************************************
ok: [localhost] => 
  sql.query: update table1 set url = '["\/Alex"]'

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:

- name: Update record
  postgresql_query:
    query: > 
      update table1 set url = %s
    positional_args:
      - '["\/{{ name | string }}"]'

Given the playbook:

- hosts: localhost
  gather_facts: no

  tasks:
    - postgresql_query:
        query: >-
          update table1 set url = %s
        positional_args:
          - '["\/{{ name | string }}"]'
      vars:
        name: Alex
      register: sql

    - debug: 
        var: sql.query

This yields, as expected:

TASK [postgresql_query] ******************************************************
changed: [localhost]

TASK [debug] *****************************************************************
ok: [localhost] => 
  sql.query: update table1 set url = '["\/Alex"]'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文