0

How do you remove json objects that do not contain a specific value? In the json below how do you keep objects block that contains "11.22.33.0/24"?

How can this be achieved with jinja or Ansible filtering?

json content

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

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63

1 Answers1

1

Q: "How do you keep objects block that contains "11.22.33.0/24"?"

A: Given the JSON content in the variable my_data, use json_query to create the list of objects block my_ip_permissions and combine the result. For example, the tasks below do the job

   - 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}) }}"


With the variable 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

the output was

    "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
            }
        ]
    }
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • I'm getting this output `code`:{ "description": "for load balancer access", "group_id": "sg-12345", "group_name": "my-LoadBalancer", "ip_permissions": "" }`code` How can I add the from_port, ip_protocol and to_port? – user3728043 Feb 06 '20 at 04:16
  • I don't know. I've typed your data and the result fits the requirement. – Vladimir Botka Feb 06 '20 at 06:09
  • I updated my Ansible to the current version. thanks! @Vladimir Bokta – user3728043 Feb 06 '20 at 16:26