【发布时间】:2021-07-16 21:27:48
【问题描述】:
我正在 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)
我也尝试在when 语句的末尾添加| bool,但在这两种情况下我都会收到错误: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 或编程/脚本方面的正式教育。
【问题讨论】: