大网数据广告活动
服务器运维问答

Search a list of Dicts using a list of items and find if the item exists under the attribute 'value'. Store only the Items NOT found as a new list

· 52CE 我爱测

This one has been stomping me for a good while now. Hoping you guys can help. I have a list of dicts registered like the example below (These are 'objects' from a Firewall) "objects": [ { "name": "Test1", "type": "ip-netmask", "v…

问题原文

原帖链接 · Drusstheledg3 · CC BY-SA 4.0

This one has been stomping me for a good while now. Hoping you guys can help.

I have a list of dicts registered like the example below (These are 'objects' from a Firewall)

"objects": [
      { 
         "name": "Test1",  
         "type": "ip-netmask", 
         "value": "8.8.8.8"
      },
      { 
         "name": "Test2", 
         "type": "ip-netmask", 
         "value": "8.8.4.4"
      }
   ]
}

Using a list of IP addresses called 'sources' I am currently able to search the 'values' in 'objects' to see if they exist. If they are found, I create a new list using set_fact and populate this with the 'name' of the object associated with the found 'value'. This is working OK.

The problem I am trying to solve is how I can create a new list with the 'sources' that are NOT found in my search of the values.

My working code for finding existing 'sources' and storing as names is as below:

vars
sources: ['8.8.8.8','8.8.4.4']

tasks:

  - name: Fetch all objects and store result
     panos_object_facts:
      provider: "{{ cli }}"
      device_group: DG_Test
      name_regex: '.*'
      object_type: 'address'
      register: result 

 - name: Search result for our sources and store as list if found
     set_fact:
        existing_source_addr: "{{  existing_source_addr|default([]) + [(result.objects | selectattr('value', 'search', item ) | list  | first).name ] }}"   
      with_items: "{{ sources }}"
    - debug: var=existing_source_addr

This returns an example as below:

"existing_source_addr": "['Test1', 'Test2']"

The below example is the code I am testing to create a new list which only contains the 'sources' which are NOT found. This is not working as expected.

vars
sources: ['8.8.8.8','8.8.4.4','4.4.4.4'] 

# 4.4.4.4 does not exist in our list of dicts 'objects' #

tasks:

  - name: Fetch all objects and store result
     panos_object_facts:
      provider: "{{ cli }}"
      device_group: DG_Test
      name_regex: '.*'
      object_type: 'address'
      register: result 

  - name: Search result for our sources and store as list if NOT found 
      set_fact:
        non_existing_source_addr: "{{ non_existing_source_addr|default([]) + [item] }}"
      with_items: "{{ sources }}"
      when: item not in result.objects | selectattr('value', 'search', item ) | list
   - debug: var=non_existing_source_addr

This returns an example as below:

"non_existing_source_addr": "[u'8.8.8.8', u'8.8.4.4', AnsibleUndefined]"

The condition is true as the 3rd item is not found as expected, but my variable is being set with all items from the list 'sources' + 'AnsibleUndefined' for the entry which does not exist.

Is there a way to have this new list ONLY contain the item which was not found '4.4.4.4'? This would allow me to then use the new list to create the missing objects.

已采纳解答

原帖链接 · Vladimir Botka · CC BY-SA 4.0

Q: "New list only contains the item which was not found '4.4.4.4'."

A: For example

    - debug:
        msg: "{{ sources|difference(_values) }}"
      vars:
        _values: "{{ objects|map(attribute='value')|list }}"

gives

  msg:
  - 4.4.4.4

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-07-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。