0

I have the below json where i want to extract each subset of rules based on the name (example: (SUP) Filesystem usage /var >85%) in my ansible playbook so i can use it as input for my following task.

{"nprd_infrastructure_disk_rules": [
      [
        {
          "schemaId": "builtin:anomaly-detection.disk-rules",
          "externalId": "string",
          "scope": "environment",
          "value": {
            "name": "(SUP) Filesystem usage /var >85%",
            "enabled": true,
            "metric": "LOW_DISK_SPACE",
            "thresholdPercent": 15,
            "sampleLimit": {
              "violatingSamples": 3,
              "samples": 3
            },
            "diskNameFilter": {
              "operator": "EQUALS",
              "value": "/var"
            },
            "tagFilters": [
              "Environment:sup",
              "Area:IT"
            ]
          }
        }
      ],
      [
        {
          "schemaId": "builtin:anomaly-detection.disk-rules",
          "externalId": "string",
          "scope": "environment",
          "value": {
            "name": "(SUP) Filesystem usage /var >95%",
            "enabled": true,
            "metric": "LOW_DISK_SPACE",
            "thresholdPercent": 5,
            "sampleLimit": {
              "violatingSamples": 3,
              "samples": 3
            },
            "diskNameFilter": {
              "operator": "EQUALS",
              "value": "/var"
            },
            "tagFilters": [
              "Environment:sup",
              "Area:IT"
            ]
          }
        }
      ]
    ]
}

The closest attempt was the below code:

       set_fact:         
         body_nprd: "{{ nprd_infrastructure_disk_rules | json_query(querystr) }}"
       vars:
         querystr: '[][value][?name == to_string(`Filesystem usage /var >95% (SUP)`)]'  

This gave me the below output where i am missing some fields.

  [
    {
      "name": "Filesystem usage /var >95% (SUP)",
      "enabled": true,
      "metric": "LOW_DISK_SPACE",
      "thresholdPercent": 5,
      "sampleLimit": {
        "violatingSamples": 3,
        "samples": 3
      },
      "diskNameFilter": {
        "operator": "EQUALS",
        "value": "/var"
      },
      "tagFilters": [
        "Environment:sup",
        "Area:IT"
      ]
    }
  ]

What i want to achieve and get is:

      [
        {
          "schemaId": "builtin:anomaly-detection.disk-rules",
          "externalId": "string",
          "scope": "environment",
          "value": {
            "name": "(SUP) Filesystem usage /var >95%",
            "enabled": true,
            "metric": "LOW_DISK_SPACE",
            "thresholdPercent": 5,
            "sampleLimit": {
              "violatingSamples": 3,
              "samples": 3
            },
            "diskNameFilter": {
              "operator": "EQUALS",
              "value": "/var"
            },
            "tagFilters": [
              "Environment:sup",
              "Area:IT"
            ]
          }
        }
      ]
Kevin
  • 143
  • 1
  • 8
  • Your input is showing `(SUP) Filesystem usage /var >95%` while your query is showing you are searching for `Filesystem usage /var >95% (SUP)`, where the `(SUP)` part moved from one end of the text to another. Are you sure all your are showing here is correct? – β.εηοιτ.βε Jan 02 '23 at 09:27

1 Answers1

0

Try the query below

  body_nprd: "{{ nprd_infrastructure_disk_rules|flatten|
                 json_query('[?value.name == `(SUP) Filesystem usage /var >95%`]') }}"

You can also use selectattr instead of json_query

  body_nprd: "{{ nprd_infrastructure_disk_rules|flatten|
                 selectattr('value.name', 'eq', '(SUP) Filesystem usage /var >95%') }}"

both options give what you want

  body_nprd:
  - externalId: string
    schemaId: builtin:anomaly-detection.disk-rules
    scope: environment
    value:
      diskNameFilter:
        operator: EQUALS
        value: /var
      enabled: true
      metric: LOW_DISK_SPACE
      name: (SUP) Filesystem usage /var >95%
      sampleLimit:
        samples: 3
        violatingSamples: 3
      tagFilters:
      - Environment:sup
      - Area:IT
      thresholdPercent: 5

Example of a complete playbook for testing

- hosts: localhost

  vars:

    nprd_infrastructure_disk_rules:
      - - externalId: string
          schemaId: builtin:anomaly-detection.disk-rules
          scope: environment
          value:
            diskNameFilter:
              operator: EQUALS
              value: /var
            enabled: true
            metric: LOW_DISK_SPACE
            name: (SUP) Filesystem usage /var >85%
            sampleLimit:
              samples: 3
              violatingSamples: 3
            tagFilters:
            - Environment:sup
            - Area:IT
            thresholdPercent: 15
      - - externalId: string
          schemaId: builtin:anomaly-detection.disk-rules
          scope: environment
          value:
            diskNameFilter:
              operator: EQUALS
              value: /var
            enabled: true
            metric: LOW_DISK_SPACE
            name: (SUP) Filesystem usage /var >95%
            sampleLimit:
              samples: 3
              violatingSamples: 3
            tagFilters:
            - Environment:sup
            - Area:IT
            thresholdPercent: 5

    body_nprd: "{{ nprd_infrastructure_disk_rules|flatten|
                   selectattr('value.name', 'eq', '(SUP) Filesystem usage /var >95%') }}"

    body_npr2: "{{ nprd_infrastructure_disk_rules|flatten|
                   json_query('[?value.name == `(SUP) Filesystem usage /var >95%`]') }}"

  tasks:

    - debug:
        var: body_nprd
    - debug:
        var: body_npr2
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • If I use your solution i get no results in my result. Also i do not want it flattend but remains as a json so i can use it easily in my next task. { "changed": false, "ansible_facts": { "body_nprd": [] }, "_ansible_no_log": false } – Kevin Dec 30 '22 at 07:14
  • 1) Update your question and put the complete output of running my playbook. See [mre]. 2) Keep *nprd_infrastructure_disk_rules* in JSON if you want to 3) *nprd_infrastructure_disk_rules* in the playbook is YAML. It's not flattened, and nobody is telling you you should flatten it. – Vladimir Botka Dec 30 '22 at 07:20