2021年1月18日星期一

filtering ansible JSON output

I'm trying to extract specific key/values from this JSON output

{      "result": {          "results": [              {                  "changed": false,                  "datacenter_info": [                      {                          "name": "DC_1",                          "moid": "datacenter-2",                          "config_status": "gray",                          "overall_status": "gray"                      }                  ],                  "invocation": {                      "module_args": {                          "hostname": "vcenter_hostname_1",                          "username": "XXXXX",                          "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",                          "validate_certs": false                      }                  },                  "ansible_facts": {                      "discovered_interpreter_python": "/usr/libexec/platform-python"                  },                  "failed": false,                  "item": "vcenter_hostname_1",                  "ansible_loop_var": "item"              },              {                  "changed": false,                  "datacenter_info": [                      {                          "name": "DC_2_A",                          "moid": "datacenter-2",                          "config_status": "gray",                          "overall_status": "gray"                      },                      {                          "name": "DC_2_B",                          "moid": "datacenter-3",                          "config_status": "gray",                          "overall_status": "gray"                      },                      {                          "name": "DC_2_C",                          "moid": "datacenter-3",                          "config_status": "gray",                          "overall_status": "gray"                      }                  ],                  "invocation": {                      "module_args": {                          "hostname": "vcenter_hostname_2",                          "username": "XXXXX",                          "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",                          "validate_certs": false                      }                  },                  "failed": false,                  "item": "vcenter_hostname_2",                  "ansible_loop_var": "item"              }          ],          "msg": "All items completed",          "changed": false      },      "_ansible_verbose_always": true,      "_ansible_no_log": false,      "changed": false  }  

The expected output is:

{      [          {              "datacenter_info": [                  {                      "name": "DC_1",                      "moid": "datacenter-1",                      "config_status": "gray",                      "overall_status": "gray"                  }              "vcenter": "vcenter_hostname_1",          },          {              "datacenter_info": [                  {                      "name": "DC_2_A",                      "moid": "datacenter-2",                      "config_status": "gray",                      "overall_status": "gray"                  },                  {                      "name": "DC_2_B",                      "moid": "datacenter-3",                      "config_status": "gray",                      "overall_status": "gray"                  },                  {                      "name": "DC_2_C",                      "moid": "datacenter-4",                      "config_status": "gray",                      "overall_status": "gray"                  }              ],              "vcenter": "vcenter_hostname_2"          }      ]  }  

I tried:

- name: Append string to list    set_fact:      formatted: ""  

It's pretty close but I couldn't insert the key vcenter and I lost the key datacenter_info.

https://stackoverflow.com/questions/65778893/filtering-ansible-json-output January 19, 2021 at 12:59AM

没有评论:

发表评论