检查Ansible中Dict中的键的值
我想在keys(检查Ansible中Dict中的键的值,ansible,Ansible,我想在keys(name)值中找到字符串(vlan)中运行Ansible任务。i、 e dict 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
name
)值中找到字符串(vlan
)中运行Ansible任务。i、 e
dict
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()
这就是我所拥有的,但目前,这不起作用。您的表达式存在一些问题:
interfaces\u l3.values()
应该放大,因为interfaces\u l3
是一个列表,而列表没有.values()
方法vlan
的变量,而不是字符串“vlan”
接口\u l3
列表中的任何项是否在名称
属性的值中包含字符串“vlan”
。你可以这样做:
---
- 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
您的表达式存在一些问题:
interfaces\u l3.values()
应该放大,因为interfaces\u l3
是一个列表,而列表没有.values()
方法vlan
的变量,而不是字符串“vlan”
接口\u l3
列表中的任何项是否在名称
属性的值中包含字符串“vlan”
。你可以这样做:
---
- 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
显示实际的错误消息总是很有帮助的,而不仅仅是说它“不工作”。在这种情况下,问题是相当明显的,但有时错误消息对确定问题的原因非常有帮助。显示实际的错误消息总是很有帮助的,而不仅仅是说它“不工作”。在本例中,问题相当明显,但有时错误消息对确定问题的原因非常有帮助。