在 ansible 搜索测试中,有没有一种方法可以使用布尔 OR 运算符来测试多个值?

问题描述

我正在处理 ansible 2.9 版中的 ansible playbook 任务,我有一个与 when 条件配对的循环,我希望对两个值进行条件测试,而不仅仅是一个(这对我有用)。有没有办法在 when 语句中使用 search() 测试来执行布尔 OR?

这是我使用的有效方法(仅测试一个值):

- name: Test Interface Looping
  hosts: test
  vars:
    desc_search: "test"
    desc_search_c: "TEST"
  tasks:
    - name: IOS Facts
      ios_facts:
        gather_subset:
          - '!all'
          - '!min'
        gather_network_resources:
          - 'interfaces'
      register: result
    - debug: var=result
    - name: Update all descriptions
      ios_interfaces:
        config:
          - name: "{{ item.key }}"
            description: "Test Done"
        state: replaced
      loop: "{{ ansible_net_interfaces|dict2items }}"
      when: item.value.description is search(desc_search)

如果可能的话,这是我想要做的(到目前为止还没有工作):

- name: Test Interface Looping
  hosts: test
  vars:
    desc_search: "test"
    desc_search_c: "TEST"
  tasks:
    - name: IOS Facts
      ios_facts:
        gather_subset:
          - '!all'
          - '!min'
        gather_network_resources:
          - 'interfaces'
      register: result
    - debug: var=result
    - name: Update all descriptions
      ios_interfaces:
        config:
          - name: "{{ item.key }}"
            description: "Test Done"
        state: replaced
      loop: "{{ ansible_net_interfaces|dict2items }}"
      when: item.value.description is search(desc_search) or search(desc_search_c)

我也尝试在 | bool 语句的末尾添加 when,但在这两种情况下我都收到错误:The conditional check 'item.value.description is search(desc_search) or search(desc_search_c)' failed. The error was: error while evaluating conditional (item.value.description is search(desc_search) or search(desc_search_c)): 'search' is undefined...

可以做我在这里想做的事情吗?对于我可能使用的任何不正确的术语,我们深表歉意。我是一名网络工程师,所以没有接受过 ansible 或编程/脚本方面的正式教育。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)