Исключить json объект в списке, который не содержит вейл - PullRequest
0 голосов
/ 06 февраля 2020

Как удалить json объекты, которые не содержат заданного значения c? В json ниже, как сохранить блок объектов, который содержит «11.22.33.0/24"?

Как этого можно добиться с помощью фильтрации jinja или Ansible?

json содержание

my_data:
  description: "for load balancer access"
  group_is: "sg-1234"
  group_name: "MY GroupName"
  ip_permissions:
    - from: 80
      ip_protocol: "tcp"
      ip_ranges: []
      to_port: 80
    - from: null
      ip_protocol: "-1"
      ip_ranges:
        - cidr_ip: "11.22.33.0/24"
          description: "MY site"
      to_port: null
    - from: 22
      ip_protocol: "tcp"
      ip_ranges: []
      to_port: 22
    - from: 3306
      ip_protocol: "tcp"
      ip_ranges: []
      to_port: 3306
    - from: 3000
      ip_protocol: "tcp"
      ip_ranges:
        - cidr_ip: "11.22.33.0/24"
          description: "MY site"
      to_port: 3000
    - from: 443
      ip_protocol: "tcp"
      ip_ranges: []
      to_port: 443

enter image description here

enter image description here

enter image description here

1 Ответ

1 голос
/ 06 февраля 2020

Q: "Как сохранить блок объектов, который содержит" 11.22.33.0/24"?"

A: Учитывая содержание JSON в переменная my_data, используйте json_query для создания списка объектов, блок my_ip_permissions и combine результат. Например, приведенные ниже задачи выполняют работу

   - set_fact:
        my_ip_permissions: "{{ my_data.ip_permissions|json_query(my_query) }}"
      vars:
        my_query: "[?ip_ranges[?cidr_ip == '11.22.33.0/24']]"
    - set_fact:
        my_data: "{{ my_data|combine({'ip_permissions': my_ip_permissions}) }}"


С переменной my_data
my_data:
  description: "for load balancer access"
  group_is: "sg-1234"
  group_name: "MY GroupName"
  ip_permissions:
    - from: 80
      ip_protocol: "tcp"
      ip_ranges: []
      to_port: 80
    - from: null
      ip_protocol: "-1"
      ip_ranges:
        - cidr_ip: "11.22.33.0/24"
          description: "MY site"
      to_port: null
    - from: 22
      ip_protocol: "tcp"
      ip_ranges: []
      to_port: 22
    - from: 3306
      ip_protocol: "tcp"
      ip_ranges: []
      to_port: 3306
    - from: 3000
      ip_protocol: "tcp"
      ip_ranges:
        - cidr_ip: "11.22.33.0/24"
          description: "MY site"
      to_port: 3000
    - from: 443
      ip_protocol: "tcp"
      ip_ranges: []
      to_port: 443

на выходе было

    "my_data": {
        "description": "for load balancer access", 
        "group_is": "sg-1234", 
        "group_name": "MY GroupName", 
        "ip_permissions": [
            {
                "from": null, 
                "ip_protocol": "-1", 
                "ip_ranges": [
                    {
                        "cidr_ip": "11.22.33.0/24", 
                        "description": "MY site"
                    }
                ], 
                "to_port": null
            }, 
            {
                "from": 3000, 
                "ip_protocol": "tcp", 
                "ip_ranges": [
                    {
                        "cidr_ip": "11.22.33.0/24", 
                        "description": "MY site"
                    }
                ], 
                "to_port": 3000
            }
        ]
    }
...