我试图解析以下JSON输出并提取volume_id_attributes.name
和volume_space_attributes.size_total
,但它一直在说:
fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'str object' has no attribute 'volume_id_attributes'"}
以下是我想要解析的JSON输出片段:
ok: [localhost] => {
"msg": {
"results": [
{
"item": "Application_Backups",
"ontap_info": {
"volume_info": {
"alfred_backup:Application_Backups": {
"volume_id_attributes": {
"name": "alfred_backup"
},
"volume_space_attributes": {
"size_total": "357019156480",
}
},
"alfred_prod:Application_Backups": {
"volume_id_attributes": {
"name": "alfred_prod"
},
"volume_space_attributes": {
"size_total": "397512166710",
}
},
"item": "Database_backups",
"ontap_info": {
"volume_info": {
"backup:Database_backups": {
"volume_id_attributes": {
"name": "backup"
},
"volume_space_attributes": {
"size_total": "754219156480",
}
},
"prod:Database_backups": {
"volume_id_attributes": {
"name": "prod"
},
"volume_space_attributes": {
"size_total": "398952211210",
}
}
]
这是jinja2脚本:
{% for svm in volumes.results %}
{% set svm_name = svm.item %}
<td style="text-align: center;" rowspan={{ volumes|length }} class="confluenceTd">{{ svm_name }}</td>
{% for volume in svm.ontap_info.volume_info %}
{% set volume_name = volume.volume_id_attributes.name %}
{% set volume_size_total = volume.volume_space_attributes.size_total %}
当我只打印{{ volume }}
时,它会打印alfred_backup:Application_Backups
和alfred_prod:Application_Backups
,但它不想进入它们的列表。
你能告诉我我做错了什么吗?
当在Jinja中对列表执行for循环时,您确实得到了您所期望的,这就是循环{% for svm in volumes.results %}
工作的原因。
当你在字典上循环时,如果你像你一样循环,你只会得到字典的键:{% for volume in svm.ontap_info.volume_info %}
,所以volume
将是你的字典键。
你想要实现的仍然是可能的,以的形式
{% for key, value in dict.items() %}
如Jinja的文件所示:https://jinja.palletsprojects.com/en/2.11.x/templates/#for
所以完整的工作剧本应该是
- hosts: all
gather_facts: no
tasks:
- debug:
msg: |
{% for svm in volumes.results %}
{% set svm_name = svm.item %}
<td style="text-align: center;" rowspan={{ volumes|length }} class="confluenceTd">{{ svm_name }}</td>
{% for key, volume in svm.ontap_info.volume_info.items() %}
{% set volume_name = volume.volume_id_attributes.name %}
{% set volume_size_total = volume.volume_space_attributes.size_total %}
<td>{{ volume_name }}</td>
<td>{{ volume_size_total }}</td>
{% endfor %}
{% endfor %}
vars:
{
"volumes": {
"results": [{
"item": "Application_Backups",
"ontap_info": {
"volume_info": {
"alfred_backup:Application_Backups": {
"volume_id_attributes": {
"name": "alfred_backup"
},
"volume_space_attributes": {
"size_total": "357019156480"
}
},
"alfred_prod:Application_Backups": {
"volume_id_attributes": {
"name": "alfred_prod"
},
"volume_space_attributes": {
"size_total": "397512166710"
}
}
}
}
},
{
"item": "Database_backups",
"ontap_info": {
"volume_info": {
"backup:Database_backups": {
"volume_id_attributes": {
"name": "backup"
},
"volume_space_attributes": {
"size_total": "754219156480"
}
},
"prod:Database_backups": {
"volume_id_attributes": {
"name": "prod"
},
"volume_space_attributes": {
"size_total": "398952211210"
}
}
}
}
}
]
}
}
因此,它给出了HTML片段:
<td style="text-align: center;" rowspan=1 class="confluenceTd">Application_Backups</td>
<td>alfred_backup</td>
<td>357019156480</td>
<td>alfred_prod</td>
<td>397512166710</td>
<td style="text-align: center;" rowspan=1 class="confluenceTd">Database_backups</td>
<td>backup</td>
<td>754219156480</td>
<td>prod</td>
<td>398952211210</td>