【问题标题】:Ansible check if key in complex dictionnaryAnsible 检查复杂字典中的键
【发布时间】:2018-08-08 08:20:03
【问题描述】:

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

这是我的字典的样子:

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 中的每个条目。

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

【问题讨论】:

    标签: loops dictionary ansible


    【解决方案1】:

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

    - 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 条件将检查变量是否已定义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      相关资源
      最近更新 更多