首页 > 解决方案 > Jinja2,Ansible,如何使用 if 语句创建嵌套 for 循环并在迭代时更新列表?

问题描述

我正在尝试遍历字典列表并检查在这种类型的结构中是否有多个 Source 用于同一组:

[{"Device": "CM1LS-05B","Group": "239.216.12.8/32","Source": "10.144.12.8/32"},
 {"Device": "CM1LS-01A","Group": "239.192.9.100/32","Source": "10.144.69.7/32"}]

所以基本上我需要抓取列表中的每个字典并将其与列表中的所有其他字典进行比较,对于每个比较,如果 dict.groups 匹配,然后检查 dict.sources 是否匹配,如果它们 dict.groups 匹配和 dict。来源不匹配我需要将两个字典都附加到 final_list

这是我所拥有的:

- name: Look for Multicast Groups with More than One Source
  hosts: localhost
  connection: local
  gather_facts: false
  vars:
    final_list: []
    my_list: [
        {
            "Device": "CM1LS-05B",
            "Group": "239.216.12.8/32",
            "Source": "10.144.12.8/32"
        },
        {
            "Device": "CM1LS-01A",
            "Group": "239.192.9.100/32",
            "Source": "10.144.69.7/32"
        },
        {
            "Device": "CM1LS-05B",
            "Group": "239.216.48.229/32",
            "Source": "10.144.48.15/32"
        },
        {
            "Device": "CM1LS-05B",
            "Group": "239.216.48.40/32",
            "Source": "10.144.65.161/32"
        },
        {
            "Device": "CM1LS-01A",
            "Group": "239.208.0.202/32",
            "Source": "172.23.59.16/32"
        },
        {
            "Device": "CM1LS-05B",
            "Group": "239.216.48.229/32",
            "Source": "10.144.48.229/32"
        },
        {
            "Device": "CM1LS-01A",
            "Group": "239.208.0.203/32",
            "Source": "172.23.59.16/32"
        }
    ]

  tasks:
    - read_csv:
        path: results.csv
      register: lines

    - set_fact:
        final_list:
           "{% for i in my_list %}
               {% for j in my_list %}
                  {% if i[ 'Group' ] == j[ 'Group' ] %}
                     {{ final_list + [ i['Group']] }}
                  {% endif %}
               {% endfor %}
            {% endfor %}"
    - debug: var=final_list

输出:

TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "final_list": "   [u'239.216.12.8/32']                    [u'239.192.9.100/32']                    [u'239.216.48.229/32']        [u'239.216.48.229/32']              [u'239.216.48.40/32']                    [u'239.208.0.202/32']              [u'239.216.48.229/32']        [u'239.216.48.229/32']                    [u'239.208.0.203/32']   "
}

PLAY RECAP ************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

我认为我在 jinja2 forloop if 语句的比较中有问题

任何想法?

标签: for-loopansiblejinja2

解决方案


这对我有用:

- set_fact:
        final_list: >-
          {%- for i in my_list -%}
            {%- for j in my_list -%}
              {%- if (i['Group'] == j['Group']) and (i['Source'] != j['Source']) -%}
                {{ my_result + [i,j] }}
              {%- endif -%}
            {%- endfor -%}
          {%- endfor -%}
  debug: var= finale_list

但我没有得到一个数组。相反,我得到了一个 unicode 字符串:

TASK [debug] **************************************************************************************************************************************************
ok: [localhost] => {
    "final_list": "[{u'Device': u'CM1LS-05B', u'Source': u'10.144.48.15/32', u'Group': u'239.216.48.229/32'}, {u'Device': u'CM1LS-20A', u'Source': u'10.144.48.229/32', u'Group': u'239.216.48.229/32'}][{u'Device': u'CM1LS-20A', u'Source': u'10.144.48.229/32', u'Group': u'239.216.48.229/32'}, {u'Device': u'CM1LS-05B', u'Source': u'10.144.48.15/32', u'Group': u'239.216.48.229/32'}]"
}

PLAY RECAP ****************************************************************************************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

所需的输出需要如下所示:

TASK [debug] **************************************************************************************************************************************************
ok: [localhost] => {
    "final_list": [
        {
            "Device": "CM1LS-05B",
            "Group": "239.216.48.229/32",
            "Source": "10.144.48.15/32"
        },
        {
            "Device": "CM1LS-20A",
            "Group": "239.216.48.229/32",
            "Source": "10.144.48.229/32"
        }
    ]
}

PLAY RECAP ****************************************************************************************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


推荐阅读