Ansible playbook to retrieve Percentage Volume Consumed Size of a Azure Netapp file volume

Viewed 102

I am looking to get the value in attribute average from the output.

- name: Prt volumelogicalsize
  debug:
    msg: "{{ (volumelogicalsize.stdout| from_json).value }}"

Below is the output

ok: [ansiblehost] => {

    "msg": [
        {
            "displayDescription": "The percentage of the volume consumed including snapshots.",
            "errorCode": "Success",
            "id": "/subscriptions//resourceGroups//providers/Microsoft.NetApp/netAppAccounts/SAP-ANF-SHARED-PROD1/capacityPools//providers/Microsoft.Insights/metrics/VolumeConsumedSizePercentage",
            "name": {
                "localizedValue": "Percentage Volume Consumed Size",
                "value": "VolumeConsumedSizePercentage"
            },
            "resourceGroup": "RG-CVS-USE2-SAP-ANF-Shared-Prod-1",
            "timeseries": [
                {
                    "data": [
                        {
                            "average": 71.25216012022325,
                            "count": null,
                            "maximum": null,
                            "minimum": null,
                            "timeStamp": "2021-03-17T04:00:00+00:00",
                            "total": null
                        }
                    ],
                    "metadatavalues": []
                }
            ],
            "type": "Microsoft.Insights/metrics",
            "unit": "Percent"
        }
    ]
}

I am expecting the output like 71.25216012022325 or 71.25.

1 Answers

Based on what you are giving us in your output, you can access it doing:

- debug:
    msg: "{{ (volumelogicalsize.stdout| from_json).value.0.timeseries.0.data.0.average }}"

This would give:

msg: '71.25216012022325'

And if you want only two decimal, you can use the format filter:

- debug:
    msg: "{{ '%.2f' | format((volumelogicalsize.stdout| from_json).value.0.timeseries.0.data.0.average) }}"

That would give:

msg: '71.25'
Related