简体   繁体   English

Jinja2 无法检索字典中键的值

[英]Jinja2 failing to retrieve value of a key in a dictionary

I am trying to extract the value of volume_snapshot_schedules but I can't seem to make it right.我正在尝试提取volume_snapshot_schedules的值,但我似乎无法做到这一点。

This key is a part of a dictionary where I am able to extract the values of its other keys but not this one.此键是字典的一部分,我可以在其中提取其他键的值,但不能提取此键的值。

This is the dictionary as printed by ansible (but simplified):这是 ansible 打印的字典(但已简化):

policy: [{
'comment': 'Default policy with hourly, daily & weekly schedules.', 
'enabled': 'true', 
'policy': 'default', 
'policy_owner': 'cluster-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'}
]}
}]

If I print the output of {{ policy.value }} , the following is printed:如果我打印{{ policy.value }}的 output ,则会打印以下内容:

{
'comment': 'Default policy with hourly, daily & weekly schedules.', 
'enabled': 'true', 
'policy': 'default', 
'policy_owner': 'cluster-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'}
}

If I print the value of {{ policy.value.comment }} or {{ policy.value.policy }} , I get the appropriate value as expected.如果我打印{{ policy.value.comment }}{{ policy.value.policy }}的值,我会按预期得到适当的值。

However, when I print the value of {{ policy.value.snapshot_policy_schedules }} , it fails with error:但是,当我打印{{ policy.value.snapshot_policy_schedules }}的值时,它会失败并出现错误:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'snapshot_policy_schedules'"}

I then tried to convert it to a list {{ policy.value.snapshot_policy_schedules|list }} but that showed the same error message.然后我尝试将其转换为列表{{ policy.value.snapshot_policy_schedules|list }}但显示了相同的错误消息。

Edit: When I try {{ policy.value.snapshot_policy_schedules[0] }} , the following error is shown:编辑:当我尝试{{ policy.value.snapshot_policy_schedules[0] }}时,显示以下错误:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: dict object has no element 0"}

I think you have to access the position of the list like that:我认为您必须像这样访问列表的 position:

{{ policy.value.snapshot_policy_schedules[0] }}

because its an array and it can have multiple lists, another option that I don't know is that you have to call it like that:因为它是一个数组并且它可以有多个列表,我不知道的另一个选项是你必须这样称呼它:

{ policy.value.snapshot_policy_schedules.snapshot_schedule_info[0]}}

The reason you are getting correct values on the policy is that it's a single value and not a list/array, so you don't need to access any position because it only have 1 value.您在策略上获得正确值的原因是它是单个值而不是列表/数组,因此您不需要访问任何 position 因为它只有 1 个值。

I dont think you can access the values with {{ policy.value.comment }} or {{ policy.value.policy }} as they are in inline list.
There are multiple ways to get the values inside list. I think the easy one is json_query filter
For example if I recreate your scenario, to get the values for 'comment' and 'snapshot_policy_schedules', use json_query as below
- hosts: localhost
  gather_facts: no
  vars: 
    policy: [{
      'comment': 'Default policy with hourly, daily & weekly schedules.', 
      'enabled': 'true', 
      'policy': 'default', 
      'policy_owner': 'cluster-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'}
      ]}
      }]
  tasks:
  - debug: 
      var: policy|json_query('[*].comment')
  - debug: 
      var: policy|json_query('[*].snapshot_policy_schedules')
------------------------------------------------------------------------------------------------------------------------------
*OUTPUT
TASK [debug] ************************************************************************************************************************************************************
ok: [localhost] => {
    "policy|json_query('[*].comment')": [
        "Default policy with hourly, daily & weekly schedules."
    ]
}
TASK [debug] ************************************************************************************************************************************************************
ok: [localhost] => {
    "policy|json_query('[*].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"
                }
            ]
        }
    ]
}*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM