首页 > 解决方案 > Ansible 检查是否键入复杂字典

问题描述

我有复杂的字典结构,我想检查是否定义了子键。我什至不知道这是否可能..

这是我的字典的样子:

config: 1: client: ubuntu network_setup: - routed ports: sw1: null sw2: client: 1/0/2 lte: 1/0/5 2: client: archlinux network_setup: - bridged ports: sw1: client: 1/0/4 sw2: null ...

注意:无法定义 lte 密钥!

我想要的是检查 lte 是否在配置字典中定义。理想情况下,我需要遍历每个条目config

我可能会写一个自定义插件,因为这听起来真的很难做到..

标签: loopsdictionaryansible

解决方案


如果它是一个列表,你必须循环它,你可以试试这个条件

- debug: var=test_item.ports.sw2.lte
  when: test_item.ports.sw2.lte is defined
  with_items: "{{ config }}"
  loop_control:
    loop_var: test_item

when 条件将检查变量是否已定义。


推荐阅读