为什么 Ansible 不在 for 循环中评估我的 jinja2 模板变量?

问题描述 投票:0回答:0


我目前遇到关于 jinja2 模板和 ansible 的问题: 在我的 inventory.yaml 中,我有这样的东西:

all_hosts:
   vars:
      example_value: 150
   hosts:
      example_host_1:
         template_value: "host1: {{ example_value }}"
      example_host_2:
         template_value: "host2: {{ example_value }}"

在剧本中,我现在想在 for 循环中使用“template_value”,例如:

...
- name: Debug
  ansible.builtin.debug:
    msg: >
       { {% for x in all_hosts %}
          "{{ x.template_value }}"
       {% endfor %} }

我期待看到:
主机 1:150
主机 2:150

然而,我看到:
host1: {{ example_value }}
host2:{{ example_value }}

为什么?我可以明确评估“x.template_value”的结果吗? 在寻找解决方案时,我读到 {{}} 内部的 {{}} 不起作用......好吧,但我该如何绕过这个限制?这肯定是可能的。 提前致以诚挚的问候和感谢:)

ansible jinja2 ansible-inventory
© www.soinside.com 2019 - 2024. All rights reserved.