我想在键(vlan
)值中找到一个字符串(name
)中运行Ansible Task。即
字典
interfaces_l3:
- name: vlan101
ipv4: 192.168.1.100/24
state: present
任务
- name: Enable Features
nxos_feature:
feature: interface-vlan
state: enabled
when: vlan in interfaces_l3.values()
这是我所拥有的,但目前无法正常工作。
答案 0 :(得分:2)
您的表情存在一些问题:
interfaces_l3.values()
应该爆炸了,因为interfaces_l3
是一个列表,并且列表没有.values()
方法。
您所指的是名为vlan
的变量,而不是字符串"vlan"
。
您要询问interfaces_l3
列表中是否有任何项目在"vlan"
属性的值中包含字符串name
。您可以执行以下操作:
---
- hosts: localhost
gather_facts: false
vars:
interfaces_l3_with_vlan:
- name: vlan101
ipv4: 192.168.1.100/24
state: present
interfaces_l3_without_vlan:
- name: something else
ipv4: 192.168.1.100/24
state: present
tasks:
- name: this should run
debug:
msg: "enabling features"
when: "interfaces_l3_with_vlan|selectattr('name', 'match', 'vlan')|list"
- name: this should be skipped
debug:
msg: "enabling features"
when: "interfaces_l3_without_vlan|selectattr('name', 'match', 'vlan')|list"
哪个会产生以下输出:
PLAY [localhost] ******************************************************************************************************************************************************************************
TASK [this should run] ************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "enabling features"
}
TASK [this should be skipped] *****************************************************************************************************************************************************************
skipping: [localhost]
PLAY RECAP ************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0