我有变量,其中包含带有其他模板引擎标记的字符串,不幸的是它们是三个花括号。如果我直接使用这些变量,一切都很好。如果我将它们复制到事实中以便随后修改它们,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 以及事实上使用它?
感谢所有提示。
PS: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...
Docs
- 高级语法 - 不安全或原始字符串 https://docs.ansible.com/ansible/latest/user_guide/playbooks_advanced_syntax.html#unsafe-or-raw-strings
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)