Home > Back-end >  How do i select multiple if display-name is equal to in json file
How do i select multiple if display-name is equal to in json file

Time:08-24

Json Outpt I have

{
    "data": [
      {
        "agent-config": {
          "are-all-plugins-disabled": false,
          "is-management-disabled": false,
          "is-monitoring-disabled": false,
          "plugins-config": null
        },
        "availability-config": {
          "is-live-migration-preferred": null,
          "recovery-action": "RESTORE_INSTANCE"
        },
        "availability-domain": "RRSt1",
        "capacity-reservation-id": null,
        "compartment-id": "ocid1.compartmeiox33bbyq",
        "dedicated-vm-host-id": null,
        "defined-tags": {
          "Oracle-Tags": {
            "CreatedBy": "ops",
            "CreatedOn": "2022-01-05T19:34:56.443Z"
          }
        },
        "display-name": "Dnode0",
        "extended-metadata": {},
        "fault-domain": "FAULT-DOMAIN-2",
        "freeform-tags": {
          "Type": "TEST-mongo"
        },
        "id": "ocid1.instance.oc1.me-jeozlnq7t4eqkfjlaxyq6a",
        "image-id": "ocid1.image.om3jrq4i5gf4k2kq",
        "instance-options": {
          "are-legacy-imds-endpoints-disabled": false
        },
        "ipxe-script": null,
        "launch-mode": "PARAVIRTUALIZED",
        "launch-options": {
          "boot-volume-type": "PARAVIRTUALIZED",
          "firmware": "UEFI_64",
          "is-consistent-volume-naming-enabled": true,
          "is-pv-encryption-in-transit-enabled": false,
          "network-type": "PARAVIRTUALIZED",
          "remote-data-volume-type": "PARAVIRTUALIZED"
        },
        "lifecycle-state": "RUNNING",
        "metadata": {},
        "platform-config": null,
        "preemptible-instance-config": null,
        "region": "me-1",
        "shape": "VM1",
        "shape-config": {
          "baseline-ocpu-utilization": null,
          "gpu-description": null,
          "gpus": 0,
          "local-disk-description": null,
          "local-disks": 0,
          "local-disks-total-size-in-gbs": null,
          "max-vnic-attachments": 2,
          "memory-in-gbs": 15.0,
          "networking-bandwidth-in-gbps": 1.0,
          "ocpus": 1.0,
          "processor-description": "2.0 GHz Intel\u00ae Xeon\u00ae Platinum 8167M (Skylake)"
        },
        "source-details": {
          "boot-volume-size-in-gbs": null,
          "boot-volume-vpus-per-gb": null,
          "image-id": "ocid1.image.oc1.me-j3jrq4i5gf4k2kq",
          "kms-key-id": null,
          "source-type": "image"
        },
        "system-tags": {},
        "time-created": "2022-01-05T19:34:56.894000 00:00",
        "time-maintenance-reboot-due": null
      },
      {
        "agent-config": {
          "are-all-plugins-disabled": false,
          "is-management-disabled": false,
          "is-monitoring-disabled": false,
          "plugins-config": null
        },
        "availability-config": {
          "is-live-migration-preferred": null,
          "recovery-action": "RESTORE_INSTANCE"
        },
        "availability-domain": "RRS-1",
        "capacity-reservation-id": null,
        "compartment-id": "ocid1.compartmentselksvqyp2aiox33bbyq",
        "dedicated-vm-host-id": null,
        "defined-tags": {
          "Oracle-Tags": {
            "CreatedBy": "ops",
            "CreatedOn": "2022-01-05T19:38:08.746Z"
          }
        },
        "display-name": "Application-Sandbox-WEBSERVER-20220715T184918",
        "extended-metadata": {},
        "fault-domain": "FAULT-DOMAIN-2",
        "freeform-tags": {
          "Type": "TEST-mongo"
        },
        "id": "ocid1.instanceathpfbjo7le5iosl4oa",
        "image-id": "ocid1.image.oc1.mekdzloqgdctm3jrq4i5gf4k2kq",
        "instance-options": {
          "are-legacy-imds-endpoints-disabled": false
        },
        "ipxe-script": null,
        "launch-mode": "PARAVIRTUALIZED",
        "launch-options": {
          "boot-volume-type": "PARAVIRTUALIZED",
          "firmware": "UEFI_64",
          "is-consistent-volume-naming-enabled": true,
          "is-pv-encryption-in-transit-enabled": false,
          "network-type": "PARAVIRTUALIZED",
          "remote-data-volume-type": "PARAVIRTUALIZED"
        },
        "lifecycle-state": "RUNNING",
        "metadata": {},
        "platform-config": null,
        "preemptible-instance-config": null,
        "region": "me-jh-1",
        "shape": "VM.Standard2.1",
        "shape-config": {
          "baseline-ocpu-utilization": null,
          "gpu-description": null,
          "gpus": 0,
          "local-disk-description": null,
          "local-disks": 0,
          "local-disks-total-size-in-gbs": null,
          "max-vnic-attachments": 2,
          "memory-in-gbs": 15.0,
          "networking-bandwidth-in-gbps": 1.0,
          "ocpus": 1.0,
          "processor-description": "2.0 GHz Intel\u00ae Xeon\u00ae Platinum 8167M (Skylake)"
        },
        "source-details": {
          "boot-volume-size-in-gbs": null,
          "boot-volume-vpus-per-gb": null,
          "image-id": "ocid1.image.oc1.me5vkdzloqgdctm3jrq4i5gf4k2kq",
          "kms-key-id": null,
          "source-type": "image"
        },
        "system-tags": {},
        "time-created": "2022-01-05T19:38:09.186000 00:00",
        "time-maintenance-reboot-due": null
      }
      
      ]}

Ansible debug: where jsondata.data returns the json output

ictimecreated: "{{ jsondata.data| selectattr('display-name', 'equalto', 'MasimoSafetyNet-Sandbox-*-*' ) | list}}" 
 

Now this is actually huge list where I have Key value like "display-name": "Application-Sandbox-WEBSERVER-20220715T184918"

Now i want to create list form json object by selecting only if display-name has Application-Sandbox-(*wildcard)-(*wildcard)

This first two term remains same only rest should be wildcard which changes. How do i create list from json object. I try to search jmespath and jinja2 template, i also tried but didnt work for me.

After the list is return I should able to get list of image_id

[ imageid_1 , imageid2_]

Only image ids where display name has Application-Sandbox

*PS If euqual to is "Application-Sandbox-WEBSERVER-20220715T184918" The element get selected but I want to add wildcard instead of WEBSERVER - 20222 So i can other similar elements


I also trying to "{{ jsondata.data| (selectattr('display-name', 'search', 'Application-Sandbox--' ) ) and (selectattr('lifecycle-state', 'equalto', 'RUNNING' ) )| list}}"

CodePudding user response:

Use tests search if you want to test a wildcard. For example,

selectattr('display-name', 'search', 'MasimoSafetyNet-Sandbox-*-*' )

Then map the attribute you want

map(attribute='image-id')

Example of a complete playbook for testing

shell> cat pb.yml
- hosts: localhost
  vars:
    ictimecreated: "{{ jsondata.data|
                       selectattr('display-name', 'search', 'Application-Sandbox-*-*' )|
                       map(attribute='image-id')|
                       list }}"
  tasks:
    - include_vars:
        file: data7.json
        name: jsondata
    - debug:
        var: ictimecreated

gives

ictimecreated:
  - ocid1.image.oc1.mekdzloqgdctm3jrq4i5gf4k2kq

Q: "Make multiple conditions"

A: Simplify the data for testing. For example,

    data:
      - availability-domain: R1
        display-name: a-s-node0
        image-id: ocid1.image.a
        lifecycle-state: RUNNING
      - availability-domain: R2
        display-name: a-s-node0
        image-id: ocid1.image.b
        lifecycle-state: STOPPED
      - availability-domain: R3
        display-name: n-s-node1
        image-id: ocid1.image.c
        lifecycle-state: STOPPED
      - availability-domain: R4
        display-name: n-s-node2
        image-id: ocid1.image.d
        lifecycle-state: RUNNING

Then, select the items by attribute display-name and pick the attribute image-id

    d2: "{{ data|
            selectattr('display-name', 'search', 'a-s-*' )|
            map(attribute='image-id')|
            list }}"

gives

  d2:
  - ocid1.image.a
  - ocid1.image.b

You can pipe the selection. For example,

    d3: "{{ data|
            selectattr('display-name', 'search', 'a-s-*' )|
            selectattr('lifecycle-state', 'eq', 'RUNNING' )|
            map(attribute='image-id')|
            list }}"

gives

  d3:
  - ocid1.image.a

You might want to simplify such complex structures before testing. For example, select keys from the dictionaries by using ansible.utils.keep_keys

test_keys: [availability-domain, display-name, image-id, lifecycle-state]
data: "{{ jsondata.data|map('ansible.utils.keep_keys', target=test_keys)|list }}"
  • Related