Ansible在变量内容被复制成事实后抛出错误



我有一些变量,其中包含带有其他模板引擎标记的字符串,不幸的是,这些标记是三个花括号。如果我直接处理这些变量,一切都没问题。如果我将它们复制到一个事实中,以便之后修改它们,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以及在一个事实中使用它?

谢谢你的提示。

注:: Ansible version:

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...

文档

  • 高级语法-不安全或原始字符串

最新更新