我正在尝试根据另一个字典中的值替换一些字典中的变量,它在完全匹配时运行良好,但仅需匹配前几个单词即可。
- debug:
msg: "{{OS_short}}"
loop: "{{store_os}}"
vars:
_dict: "{{ OS|dict2items }}"
query: "[?contains(value, '{{ item.os_full }}')].key"
OS_short: "{{ _dict|json_query(query) | join ('\n')}}"
when: item.inventory.os_full is defined
需要匹配的字典:
OS:
ios:
- IOS
- ios
nxos:
- NX-OS
- nx-os
- Cisco NX-OS(tm)
#- Cisco NX-OS(tm) nxos.bla bla bla
我尝试使用“contains”和“starts_with”,但是使用“starts_with”时出现了错误信息:“expected one of: ['string'], received: \"array\""}"。” 使用“contains”时,我只得到了一个空答案。
ok: [localhost] => (item={'os_full': 'Cisco NX-OS(tm) nxos.x.x.x.x.bin, Software (nxos), Version x.y(z), RELEASE SOFTWARE Copyright (c) 2002-2019 by Cisco Systems, Inc. Compiled 3/5/2019 12:00:00'}) => {
"msg": ""
}