我正在尝试从下面的 json 输出中获取 cpucapacity/cpuused 值,Ansible 模块收到空白消息或错误

I am trying to get the cpucapacity/cpuused value from the below json output from Ansible module am getting blank msg or error

集群{ “DC0_C0”:{ “数据中心”:“DC0”, “moid”:“域-c9”, "drs_default_vm_behavior":"None", "drs_enable_vm_behavior_overrides":"None", "drs_vmotion_rate":"None", "enable_ha":"None", “enabled_drs”:是的, “enabled_vsan”:假的, "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”:错误, “主持人”:[ { “名称”:“esxi01.vsphere.local”, “文件夹”:“/DC0/host/DC0_C0” }, { "名称":"esxi02.vsphere.local", “文件夹”:“/DC0/host/DC0_C0” }, { "名称":"esxi03.vsphere.local", “文件夹”:“/DC0/host/DC0_C0” }, { "名称":"esxi04.vsphere.local", “文件夹”:“/DC0/host/DC0_C0” } ], “resource_summary”:{ “cpuCapacityMHz”:4224, “cpuUsedMHz”:87, “memCapacityMB”:6139, “memUsedMB”:1254, “pMemAvailableMB”:0, “pMemCapacityMB”:0, “存储容量MB”:33280, “storageUsedMB”:19953 }, “标签”:[ { "category_id":"urn:vmomi:InventoryServiceCategory:9fbf83de-7903-442e-8004-70fd3940297c:GLOBAL", "category_name":"sample_cluster_cat_0001", “描述”:””, "id":"urn:vmomi:InventoryServiceTag:93d680db-b3a6-4834-85ad-3e9516e8fee8:GLOBAL", “姓名”:“sample_cluster_tag_0001” } ] } }

我正在使用下面的过滤器

注册:cluster_info

您的查询无效,因为 cluster_info["clusters"] 是字典,而不是列表。要求 clusters[] 没有任何意义。试试这个:

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

您可以在 JMESPath 规范中阅读有关 wildcard expressions 的内容:

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).

我喜欢使用 jpterm 来试验 JMESPath 表达式。