我正在嘗試根據另一個字典中的值替換字典中的一些變數,它在精確匹配上作業得很好,但只匹配前幾個詞就足夠了。
- 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
我嘗試同時使用“包含”和“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": ""
}
uj5u.com熱心網友回復:
回圈產品并搜索連接的模式。所有匹配項都將添加到新串列中。例如,給定刪節資料
store_os:
- inventory:
os_full: 'Cisco NX-OS(tm) nxos.x.x.x.x.bin, ...'
下面的任務
- set_fact:
store_os_short: "{{ store_os_short|d([])
[{'inventory': item.0.inventory|
combine({'os_short': item.1.key})}] }}"
with_nested:
- "{{ store_os }}"
- "{{ OS|dict2items }}"
when: item.0.inventory.os_full is search(item.1.value|join('|'))
給
store_os_short:
- inventory:
os_full: Cisco NX-OS(tm) nxos.x.x.x.x.bin, ...
os_short: nxos
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/347791.html
