使用ansible解析api返回



我在这件事上被困了两天。我从一个api中得到了下面的列表,并希望从tower中打印出对最终用户更可读的内容。它应该类似于交换机列表(infraAccPortP.attributes.name(,如果有子列表,则是一个关键字为infraHPortS.attributes.name和值为infraPortBlk.attributes.fromPort的dict这是我第一次尝试使用ansible来检索数据并对其进行操作,我通常只是用它来构建东西。。。似乎更容易

"result.imdata": [
{
"infraAccPortP": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"dn": "uni/infra/accportprof-101_102",
"extMngdBy": "",
"lcOwn": "local",
"modTs": "2020-09-10T14:18:01.362+00:00",
"monPolDn": "uni/fabric/monfab-default",
"name": "101_102",
"nameAlias": "",
"ownerKey": "",
"ownerTag": "",
"status": "",
"uid": "15374"
}
}
},
{
"infraAccPortP": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"dn": "uni/infra/accportprof-101",
"extMngdBy": "",
"lcOwn": "local",
"modTs": "2020-09-10T14:18:01.775+00:00",
"monPolDn": "uni/fabric/monfab-default",
"name": "101",
"nameAlias": "",
"ownerKey": "",
"ownerTag": "",
"status": "",
"uid": "15374"
},
"children": [
{
"infraHPortS": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"extMngdBy": "",
"lcOwn": "local",
"modTs": "2020-09-16T23:09:40.856+00:00",
"monPolDn": "uni/fabric/monfab-default",
"name": "test",
"nameAlias": "",
"ownerKey": "",
"ownerTag": "",
"rn": "hports-test-typ-range",
"status": "",
"type": "range",
"uid": "23110"
},
"children": [
{
"infraPortBlk": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"extMngdBy": "",
"fromCard": "1",
"fromPort": "12",
"lcOwn": "local",
"modTs": "2020-09-16T23:09:40.856+00:00",
"monPolDn": "uni/fabric/monfab-default",
"name": "block2",
"nameAlias": "",
"rn": "portblk-block2",
"status": "",
"toCard": "1",
"toPort": "12",
"uid": "23110"
}
}
}
]
}
},
{
"infraHPortS": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"extMngdBy": "",
"lcOwn": "local",
"modTs": "2020-09-16T13:33:07.108+00:00",
"monPolDn": "uni/fabric/monfab-default",
"name": "101_fex",
"nameAlias": "",
"ownerKey": "",
"ownerTag": "",
"rn": "hports-101_fex-typ-range",
"status": "",
"type": "range",
"uid": "23110"
},
"children": [
{
"infraRsAccBaseGrp": {
"attributes": {
"annotation": "",
"childAction": "",
"extMngdBy": "",
"fexId": "101",
"forceResolve": "yes",
"lcOwn": "local",
"modTs": "2020-09-16T13:33:07.108+00:00",
"monPolDn": "uni/fabric/monfab-default",
"rType": "mo",
"rn": "rsaccBaseGrp",
"state": "formed",
"stateQual": "none",
"status": "",
"tCl": "infraFexBndlGrp",
"tDn": "uni/infra/fexprof-101_fex/fexbundle-101_fex",
"tType": "mo",
"uid": "23110"
}
}
}
]
}
}
]
}
},
{
"infraAccPortP": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"dn": "uni/infra/accportprof-102",
"extMngdBy": "",
"lcOwn": "local",
"modTs": "2020-09-10T14:18:02.163+00:00",
"monPolDn": "uni/fabric/monfab-default",
"name": "102",
"nameAlias": "",
"ownerKey": "",
"ownerTag": "",
"status": "",
"uid": "15374"
},
"children": [
{
"infraHPortS": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"extMngdBy": "",
"lcOwn": "local",
"modTs": "2020-09-16T18:30:37.333+00:00",
"monPolDn": "uni/fabric/monfab-default",
"name": "test",
"nameAlias": "",
"ownerKey": "",
"ownerTag": "",
"rn": "hports-test-typ-range",
"status": "",
"type": "range",
"uid": "23110"
},
"children": [
{
"infraPortBlk": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"extMngdBy": "",
"fromCard": "1",
"fromPort": "7",
"lcOwn": "local",
"modTs": "2020-09-16T18:30:37.333+00:00",
"monPolDn": "uni/fabric/monfab-default",
"name": "block2",
"nameAlias": "",
"rn": "portblk-block2",
"status": "",
"toCard": "1",
"toPort": "7",
"uid": "23110"
}
}
}
]
}
},
{
"infraHPortS": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"extMngdBy": "",
"lcOwn": "local",
"modTs": "2020-09-11T15:26:44.501+00:00",
"monPolDn": "uni/fabric/monfab-default",
"name": "9k_b25b_1-32",
"nameAlias": "",
"ownerKey": "",
"ownerTag": "",
"rn": "hports-9k_b25b_1-32-typ-range",
"status": "",
"type": "range",
"uid": "23110"
},
"children": [
{
"infraPortBlk": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"extMngdBy": "",
"fromCard": "1",
"fromPort": "48",
"lcOwn": "local",
"modTs": "2020-09-11T15:26:44.501+00:00",
"monPolDn": "uni/fabric/monfab-default",
"name": "block2",
"nameAlias": "",
"rn": "portblk-block2",
"status": "",
"toCard": "1",
"toPort": "48",
"uid": "23110"
}
}
}
]
}
}
]
}
}
]
}

预期结果如下:

switch:  101
ports:
- srvName: test 
port: 12
- srvName: test2
port:13
switch: 102
ports:
- srvName: test3
port: 12
- srvName: test4
port:13

Q:"交换机列表(infraAccPortP.attributes.name(">

A:下面的任务

- debug:
msg: "{{ result.imdata|json_query('[].*.*.name')|flatten }}"

给出

msg:
- '101_102'
- '101'
- '102'

最新更新