获取不同迭代的值-Jinja2



我在jinja中有此模板:

{% for value in id_ip.results %}
    {
       "title": "asd",
       "rel":"adas",
       "type":"application/nalip+json",
       "href": "https://www.api.com:443/api/{{item.item.stdout}}/{{item.stdout}}"
    }{% if loop.last %},{% endif %}
    {% endfor %}

变量是从JSON列表中收集的,wich来自带有2个迭代的Ansible任务:

- name: Define templatesssss
  template:
      src: requestpayload.j2
      dest: /tmp/requestpayload.j2
      owner: root
      group: root
      mode: 0644
   with_items:
      - "{{id_ip.results}}"

id_ip.results有两个迭代。

  "invocation": {
        "module_args": {
            "var": "item"
        },
        "module_name": "debug"
    },
    "item": {
        "changed": true,
        "invocation": {
            "module_args": {
            "warn": true
            },
            "module_name": "command"
        },
        "item": {
            "changed": true,
            "invocation": {
                "module_args": {
                    "warn": true
                },
                "module_name": "command"
            },
            "item": {
                "gateway": "1",
                "ip": "2"
            },
            "stdout": "62",
            "stdout_lines": [
                "62"
            ],
            "warnings": []
        },
        "stdout": "316",
        "stdout_lines": [
            "316"
        ],
        "warnings": []
    }
}

我对stdout's感兴趣,因为是我需要在模板中占用的值。模板的结果是:

  {
   "title": "asd",
   "rel":"adas",
   "type":"application/vnd.externalip+json",
   "href": "https://servicios.com:443/api/limits/2/externalnetworks/62/ips/316"
}        {
   "title": "asd",
   "rel":"adas",
   "type":"application/vnd.externalip+json",
   "href": "https://servicios.com:443/api/limits/2/externalnetworks/62/ips/316"

您可以看到这些值是 62 316 在这两个项目中,如果它只是一个迭代,那很好,但我需要通过第二个迭代值到第二扇区。

我该怎么做?

尝试:

% for value in id_ip.results %}
    {
       "title": "asd",
       "rel":"adas",
       "type":"application/nalip+json",
       "href": "https://www.api.com:443/api/{{value.item.item.stdout}}/{{value.item.stdout}}"
    }{% if loop.last %},{% endif %}
    {% endfor %}

最新更新