我有一个包含 json 的变量:
{
"ansible_facts": {
"ansible_network_resources": {
"interfaces": [
{
"description": "*** - LOCAL A - ***",
"enabled": true,
"name": "FastEthernet0"
},
{
"description": "*** - LOCAL B - ***",
"enabled": true,
"name": "GigabitEthernet1/0/1"
},
{
"description": "*** - LOCAL C - ***",
"enabled": true,
"name": "FastEthernet1"
}
]
}
}
}
当描述包含某个单词时,我需要用接口名称填充一个变量。
答案1
问:“当描述包含某个词时,用接口的名称填充变量。“
答:例如
- set_fact:
result: "{{ ansible_facts.ansible_network_resources.interfaces|
selectattr('description', 'search', pattern)|
map(attribute='name')|
list }}"
vars:
pattern: "LOCAL A"
给出
result:
- FastEthernet0
和
- set_fact:
result: "{{ ansible_facts.ansible_network_resources.interfaces|
selectattr('description', 'search', pattern)|
map(attribute='name')|
list }}"
vars:
pattern: "LOCAL"
给出
result:
- FastEthernet0
- GigabitEthernet1/0/1
- FastEthernet1