0

clusters{ "DC0_C0":{ "datacenter":"DC0", "moid":"domain-c9", "drs_default_vm_behavior":"None", "drs_enable_vm_behavior_overrides":"None", "drs_vmotion_rate":"None", "enable_ha":"None", "enabled_drs":true, "enabled_vsan":false, "ha_admission_control_enabled":"None", "ha_failover_level":"None", "ha_host_monitoring":"None", "ha_restart_priority":"None", "ha_vm_failure_interval":"None", "ha_vm_max_failure_window":"None", "ha_vm_max_failures":"None", "ha_vm_min_up_time":"None", "ha_vm_monitoring":"None", "ha_vm_tools_monitoring":"None", "vsan_auto_claim_storage":false, "hosts":[ { "name":"esxi01.vsphere.local", "folder":"/DC0/host/DC0_C0" }, { "name":"esxi02.vsphere.local", "folder":"/DC0/host/DC0_C0" }, { "name":"esxi03.vsphere.local", "folder":"/DC0/host/DC0_C0" }, { "name":"esxi04.vsphere.local", "folder":"/DC0/host/DC0_C0" } ], "resource_summary":{ "cpuCapacityMHz":4224, "cpuUsedMHz":87, "memCapacityMB":6139, "memUsedMB":1254, "pMemAvailableMB":0, "pMemCapacityMB":0, "storageCapacityMB":33280, "storageUsedMB":19953 }, "tags":[ { "category_id":"urn:vmomi:InventoryServiceCategory:9fbf83de-7903-442e-8004-70fd3940297c:GLOBAL", "category_name":"sample_cluster_cat_0001", "description":"", "id":"urn:vmomi:InventoryServiceTag:93d680db-b3a6-4834-85ad-3e9516e8fee8:GLOBAL", "name":"sample_cluster_tag_0001" } ] } }

I am using the below the filter

register: cluster_info

  • debug: msg="{{ cluster_info.json_query('clusters[].resource_summary.cpuCapacityMHz')}}"
  • 1
    The syntax for using a filter is `variable|filter`, not `variable.filter`, which is what it looks like you're trying in that final `debug` task. – larsks Jul 02 '21 at 02:53
  • tried debug: msg="{{ cluster_info | json_query('clusters[].resource_summary.cpuCapacityMHz')}}" and still the same, I am able to print the results when i do this debug: msg="{{ cluster_info | json_query('clusters')}}". when i try to go further it is not printing. – ansiblenewbie Jul 02 '21 at 03:00

1 Answers1

0

Your query isn't working because cluster_info["clusters"] is a dictionary, not a list. Asking for clusters[] doesn't make any sense. Try this:

- debug:
    msg: "{{ cluster_info|json_query('clusters.*.resource_summary[].cpuCapacityMHz') }}"

You can read about wildcard expressions in the JMESPath specification:

The * syntax (referred to as a hash wildcard expression) will return a list of the hash element’s values. Any subsequent expression will be evaluated against each individual element in the list (this is also referred to as a projection).

I like to use jpterm for experimenting with JMESPath expressions.

larsks
  • 277,717
  • 41
  • 399
  • 399