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

标签 ansible ansible-facts ansible-template

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

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

最佳答案

您尝试过!不安全吗?

像这样:

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

文档

关于将变量内容复制到事实中后,Ansible 会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72029659/

相关文章:

ansible - 检查数组是否已定义并且在ansible中不为空

ansible - 当 inventory_host 作为变量插入时,无法访问主机变量

ansible - 在 Ansible 中使用 Ad-Hoc 命令将字符串写入文件

jenkins - 在 jenkins 管道上运行 ansible playbook

Ansible:将命令的标准输出存储在新变量中?

ansible - 在ansible中迭代两个列表

python - 在模块代码中使用 ansible_facts

Ansible ping 不工作

ansible - 扩展ansible角色?