for-loop - 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 语句的比较中有问题
任何想法?
解决方案
这对我有用:
- 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
推荐阅读
- python - 如何在通过 MS DevOps 运行管道时在自托管(Linux Ubuntu 18.04)代理上指定 python 版本
- flutter - Flutter ListWheelScrollView , 如何添加水平线
- bluetooth - Ble 外围断开处理程序在 gattlib c 库中不起作用
- python - 列表作为每个列表中具有多个值的字典的值
- laravel - Vuejs 在 laravel 的视图中效果不佳
- python - 如何在 Vs 代码中为 Python 使用 emmet
- excel - 为什么在粘贴变体值时我会检索数字 intead 文本?
- php - 公用文件夹移出根目录的 Laravel 停止工作
- python-3.x - Selenium Python没有定位元素
- angular - Angular 和 Spring MVC - Post 方法的问题 - 415 不受支持的媒体