【发布时间】:2020-09-18 04:33:13
【问题描述】:
我在这个问题上被困了 2 天。我有来自 api 的以下列表,并希望从塔打印最终用户更易读的内容。它应该类似于一个开关列表(infraAccPortP.attributes.name),如果有一个子列表,那么一个带有 infraHPortS.attributes.name 键和值 infraPortBlk.attributes.fromPort 的字典 这是我第一次尝试使用 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
【问题讨论】:
-
Ansible 公开了一组过滤器来处理不同类型的数据——其中一个是
from_json过滤器:stackoverflow.com/questions/40844720/json-parsing-in-ansible -
I'm terribly stuck for 2 days on this [...] this is my first attempt at using ansible to retrieve data and manipulate it=> 请分享您尝试过的方法以及实现结果的确切问题。 -
有“孩子的孩子”。添加预期结果。
-
我尝试创建一个 jinja2 模板,然后使用 lookup('template') 函数打印到输出。然后我尝试使用一些 set_fact 和 map(attribute="")。这开始起作用,但它变得越来越复杂。当编写它们的人难以理解事情时,是时候寻找另一种方式了。哈哈。那是一个我以前没有使用过的新过滤器,所以我会尝试一下,然后与您一起返回结果。谢谢
-
预期结果不是有效的 YAML。它看起来像一个字典,但是 1) 缺少根键,2) 其他键不唯一,以及 3) 缩进错误。它应该是字典列表吗?使其成为有效的YAML!
标签: ansible