无法从 json 输出中提取值 [英] Failed to extract value from json output
本文介绍了无法从 json 输出中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从以下列表中提取 snapshot_policy_schedules.snapshot_schedule_info
的值:
I am trying to extract the value of snapshot_policy_schedules.snapshot_schedule_info
from the below list:
[
{
'enabled': 'true',
'policy': 'default-DR',
'policy_owner': 'vserver-admin',
'snapshot_policy_schedules': {
'snapshot_schedule_info': [
{
'count': '6',
'prefix': 'hourly',
'schedule': 'hourly',
'snapmirror_label': '-'
},
{
'count': '2',
'prefix': 'daily',
'schedule': 'daily',
'snapmirror_label': 'daily'
},
{
'count': '2',
'prefix': 'weekly',
'schedule': 'weekly',
'snapmirror_label': 'weekly'
}
]
},
'total_schedules': '3',
'vserver_name': 'net'
}
]
但是我收到错误:
fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'list object' has no attribute 'snapshot_policy_schedules'"}
这是我的代码:
{% for schedule in policy.snapshot_policy_schedules.snapshot_schedule_info %}
{% if schedule.schedule == 'weekly' %}
{% set policy_weekly = schedule.count %}
{% endif %}
{% if schedule.schedule == 'daily' %}
{% set policy_daily = schedule.count %}
{% endif %}
{% if schedule.schedule == 'hourly' %}
{% set policy_hourly = schedule.count %}
{% endif %}
{% endfor %}
推荐答案
Q: "'list object' has no attribute 'snapshot_policy_schedules'"
Q: "'list object' has no attribute 'snapshot_policy_schedules'"
A:'list object',即变量 policy 是一个列表.您可能想要解决第一个元素
A: The 'list object' i.e. the variable policy is a list. You probably want to address the first element
{% for schedule in policy.0.snapshot_policy_schedules.snapshot_schedule_info %}
<小时>
例如,简化的 JSON
For example, simplified JSON
{
"policy": [{
"schedules": {
"info": [
{"count": "6","prefix": "hourly"},
{"count": "2","prefix": "daily"},
{"count": "2","prefix": "weekly"}]
}
}]
}
和简化的Jinja
- debug:
msg: |-
{% for item in policy.0.schedules.info %}
{{ item.count }} {{ item.prefix }}
{% endfor %}
给予
msg: |-
6 hourly
2 daily
2 weekly
这篇关于无法从 json 输出中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文