繁体   English   中英

Jinja2,Ansible,如何使用 if 语句创建嵌套的 for 循环并在迭代时更新列表?

[英]Jinja2, Ansible, How to create a nested for loop with if statement and update a list while iterating?

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

[{"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 语句的比较中有问题

任何的想法?

你快到了! 同时请注意,这是一个非常冗长的模板表达式。 开发自定义过滤器可能更容易,您可以在其中直接在 python 中管理数据。

但我们走了。 这是一个可能的“仅 ansible”解决方案,只是修复您的实际任务。 我使用了 yaml 标量折叠块,以便我们可以删除外部引号并在几行上写在最后删除新行。 我还在您的 jinja2 模板中引入了空白控制标记,以确保我们不会在输出中得到任何可能使构造变量解析膨胀的额外字符。 最后,您不能在尝试时直接连接 var。 您需要计算结果并将其输出,以便将其分配给 var。

    - set_fact:
        final_list: >-
          {%- set my_result = [] -%}
          {%- for i in my_list -%}
            {%- for j in my_list -%}
              {%- if i['Group'] == j['Group'] -%}
                {{ my_result.append(i['Group']) }}
              {%- endif -%}
            {%- endfor -%}
          {%- endfor -%}
          {{ my_result }}

这是对我有用的:

- 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM