简体   繁体   English

无法从 ansible 剧本中的字典中获取值

[英]Fail to get values from dictionary in ansible playbook

I'm trying to get some values from a dictionary in ansible playbook.我正在尝试从 ansible 剧本中的字典中获取一些值。 First, I build the dictionary and it runs correctly.首先,我构建了字典并且它运行正确。 Then, I would compute the output_dict evaluating the values of this dictionary but I have the issue above.然后,我会计算 output_dict 来评估这个字典的值,但我有上面的问题。

- set_fact:
    state_service: "{{ state_service | default({}) | combine({ item.key : item.value }) }}"
  with_items:
    - { 'key': 'op_state', 'value': "{{Check_Operative_State.results[0].stdout}}"}
    - { 'key': 'ad_state', 'value': "{{Check_Administrative_State.results[0].stdout}}"}

- name: Display the state of service
  debug: msg="{{state_service}}"

- set_fact:
    output_dict: '{{output_dict | default([]) + ["CHECK ERRORS IN SERVICE ************PASSED" if ((item.op_state == "enabled" and item.ad_state == "unlocked") or (item.op_state == "disabled" and item.ad_state == "locked")) else "CHECK ERRORS IN SERVICE **********FAILED"] }}'
  with_items: "{{state_service}}"

The error:错误:

**************************************************
2022-02-04 19:04:45,341 p=4478 u=abcd n=ansible | ok: [localhost] => {
    "msg": {
        "ad_state": "unlocked",
        "op_state": "enabled"
    }
}
2022-02-04 19:04:45,366 p=4478 u=abcd n=ansible | TASK [check : set_fact] ************************************************************************************************************************************************************************
2022-02-04 19:04:45,366 p=4478 u=abcd n=ansible | fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'op_state'\n\nThe error appears to be in '/home/abcd/health/service/script/roles/health/tasks/main.yaml': line 483, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- set_fact:\n  ^ here\n"}
2022-02-04 19:04:45,368 p=4478 u=abcd n=ansible | PLAY RECAP *****************************************************************************************************************************************

Given the list below to simplify the example给出下面的列表以简化示例

  l1:
    - {key: op_state, value: enabled}
    - {key: ad_state, value: unlocked}

Use the filter items2dict to create a dictionary from the list, eg使用过滤器items2dict从列表中创建字典,例如

  state_service: "{{ l1|items2dict }}"

gives

  state_service:
    ad_state: unlocked
    op_state: enabled

You can't iterate a dictionary, but you can use dict2items to convert the dictionary to a list, eg您不能迭代字典,但可以使用dict2items将字典转换为列表,例如

    - debug:
        var: item
      loop: "{{ state_service|dict2items }}"

gives (abridged)给出(删节)

  item:
    key: op_state
    value: enabled

  item:
    key: ad_state
    value: unlocked

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

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