【问题标题】:Populating Jinja2 Template with Ansible Variables?使用 Ansible 变量填充 Jinja2 模板?
【发布时间】:2020-03-20 19:18:51
【问题描述】:

在我的 Ansible 剧本中,我尝试设置自定义变量,然后使用这些变量填充我在 json 中拥有的许可证文件。我的想法是这样的。

   vars:
     customFields:
        unique_key: unique_value
        unique_key1: unique_value1

然后我会以这种方式使用模板模块和 jinja2 填充 license.json:

{
    "customer": "{{ customer_name }}",
    "validTil": "{{ lic_valid_till }}",
    {% for field in customFields %}
    "customFields": {
        "{{ field.key }}":"{{ field.value }}"
    }
    {% endfor %}
}

我这样做的方式是否正确,是否受支持?

【问题讨论】:

    标签: ansible jinja2


    【解决方案1】:

    是的。支持。使用items()

        {% for key, value in customFields.items() %}
        "customFields": {
            "{{ key }}":"{{ value }}"
        }
        {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 2021-11-24
      • 2016-04-10
      • 1970-01-01
      相关资源
      最近更新 更多