将变量内容复制到事实后,Ansible 抛出错误

Ansible throws error after variable content is copied into fact

我有变量,其中包含带有其他模板引擎标记的字符串,不幸的是,它们是三个花括号。如果我直接使用这些变量,一切都很好。如果我将它们复制到事实中以便之后修改它们,Ansible 会报错。

示例剧本:

- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    foo: "header/P-Asserted-Identity=<sip:{{ '{{{another_template_engine_string}}}' }}@127.0.0.1>"
  tasks:
    - debug:
        var: foo

    - set_fact:
        bar: "{{ foo }}"

    - debug:
        var: bar

我的 ansible-playbook 运行 的输出是这样的:

PLAY [localhost] **************************************************************************************************************************************************

TASK [debug] ******************************************************************************************************************************************************
ok: [localhost] => {
    "foo": "header/P-Asserted-Identity=<sip:{{{another_template_engine_string}}}@127.0.0.1>"
}

TASK [set_fact] ***************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ******************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while templating 'header/P-Asserted-Identity=<sip:{{{another_template_engine_string}}}@127.0.0.1>'.
  Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: expected token ':', got '}'.
  String: header/P-Asserted-Identity=<sip:{{{another_template_engine_string}}}@127.0.0.1>"}

PLAY RECAP ********************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

如何格式化变量内容,以便我可以直接从 var 以及在 fact 中使用它?

感谢所有提示。

P.S.: Ansible 版本:

ansible [core 2.11.1]
  config file = None
  configured module search path = ['/Users/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/user/.venv/ansible3/lib/python3.9/site-packages/ansible
  ansible collection location = /Users/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/user/.venv/ansible3/bin/ansible
  python version = 3.9.10 (main, Jan 15 2022, 11:40:53) [Clang 13.0.0 (clang-1300.0.29.3)]
  jinja version = 3.0.1
  libyaml = False

你试过“!unsafe”吗?

像这样:

 vars:
    foo: !unsafe "header/P-Asserted-Identity=<sip:{{ '{{{anot...

文档:https://docs.ansible.com/ansible/latest/user_guide/playbooks_advanced_syntax.html#unsafe-or-raw-strings