【问题标题】:variable not taking in ansible jinja template变量不接受 ansible jinja 模板
【发布时间】:2021-04-16 08:29:49
【问题描述】:

我已经创建了 jinja2 模板来使用以下代码生成我的 json 文件。 我的变量如下:

network_profile:
- Internal
- Custom

如果我使用了以上 2 个变量,那么我不会得到如下所述的输出。第二个循环的 v_value 变为自定义。我希望它是内部的

如果我使用 3 个变量,那么它可以正常工作并根据我的要求低于输出。知道我想念的地方

{
    "p_data": [
        {
            "u_label": "Internal",
            "u_catalog_item": "abc",
            "u_value": "Internal"
        },
        {
            "u_label": "Custom",
            "u_catalog_item": "abc",
            "u_value": "Internal"
        }
    ]
}
{
    "p_data": [
        {% for network in network_profile %}
            {% if loop.index == loop.length %}
            {
              "u_label": "{{ network }}",
              "u_catalog_item": "{{ catalog_item }}",
              "u_value": "{{ network }}"
            }

          {% else %}
            {
              "u_label": "{{ network }}",
              "u_catalog_item": "{{ catalog_item }}",
              "u_value": "Internal"

            },
          {% endif %}
          {% endfor %}
    ]
}

【问题讨论】:

    标签: ansible-template


    【解决方案1】:

    您的loop.length2。在第二次出现循环时,索引也将是2。所以发生的事情就是你所要求的。

    我猜你的意思是:

    {
      "p_data": [
        {% for network in network_profile %}
          {% if loop.index == loop.length %}
            {
              "u_label": "{{ network }}",
              "u_catalog_item": "{{ catalog_item }}",
              "u_value": "Internal"
              {#          ^--- swapped value for the else #}
            }
          {% else %}
            {
              "u_label": "{{ network }}",
              "u_catalog_item": "{{ catalog_item }}",
              "u_value": "{{ network }}"
              {#          ^--- swapped value for the if #}
            },
          {% endif %}
        {% endfor %}
      ]
    }
    

    【讨论】:

    • 我正在寻找的是network_profile中的动态变量,如果我添加新变量,那么我的数据应该如下所示。
    【解决方案2】:

    我正在寻找的是 network_profile 中的动态变量,如果我添加新变量,那么我的数据应该如下所示。

    {
        "p_data": [
            {
                "u_label": "Internal",
                "u_catalog_item": "abc",
                "u_value": "Internal"
            },
            {
                "u_label": "Custom",
                "u_catalog_item": "abc",
                "u_value": "Internal"
            },
            {
                "u_label": "Test",
                "u_catalog_item": "abc",
                "u_value": "Test"
            }
        ]
    }
    

    只有我有条件只有“自定义”变量。每当它出现时,u_value 应该是“内部”

        "p_data": [
            {% for network in network_profile %}
                {% if loop.index == loop.length %}
                {
                  "u_label": "{{ network }}",
                  "u_catalog_item": "{{ catalog_item }}",
                  {% if network|string() != 'Custom' %}
                  "u_value": "{{ network }}"
                  {% else %}
                  "u_value": "Internal"
                  {% endif %}
                }
    
              {% else %}
                {
                  "u_label": "{{ network }}",
                  "u_catalog_item": "{{ catalog_item }}",
                  "u_value": "{{ network }}"
                },
              {% endif %}
              {% endfor %}
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多