在這里,我想遍歷具有多個值的字典(Input.yml)并構建預期輸出中提到的 JSON。有 2 個具有多個值的鍵(內部和外部),我什至嘗試了一些解決方案(迭代 dict Ansible,每個鍵多個值),但未能得到正確的結果。
輸入.yml
StaticRoutes:
- Internal:
- "1.1.1.1/8"
- "2.2.2.2/16"
- External:
- "5.5.5.1"
- "5.5.5.2"
gateway:
- "6.6.6.6"
劇本
- name: Create route table
set_fact:
route: >-
{{
route | default([])
[{ 'name': item.key ,
'subnet': item.value,
'gatewayIP': gateway.0}]
}}
with_dict: "{{ StaticRoutes}}"
ignore_errors: yes
電流輸出
[
{
"gatewayIP": "6.6.6.6",
"name": "Internal",
"subnet": [
"1.1.1.1/8",
"2.2.2.2/16"
]
},
{
"gatewayIP": "10.147.166.1",
"name": "External",
"subnet": [
"5.5.5.1",
"5.5.5.2"
]
}
]
預期產出
[
{
"gatewayIP": "6.6.6.6",
"name": "Internal",
"subnet": "1.1.1.1/8"
},
{
"gatewayIP": "6.6.6.6",
"name": "Internal",
"subnet": "2.2.2.2/16"
},
{
"gatewayIP": "6.6.6.6",
"name": "External",
"subnet":"5.5.5.1"
},
{
"gatewayIP": "6.6.6.6",
"name": "External",
"subnet": "5.5.5.2"
}
]
uj5u.com熱心網友回復:
例如
- set_fact:
route: "{{ route|default([]) [{'name': item.0.key,
'subnet': item.1,
'gatewayIP': gateway.0}] }}"
with_subelements:
- "{{ StaticRoutes|map('dict2items')|flatten }}"
- value
給
route:
- {gatewayIP: 6.6.6.6, name: Internal, subnet: 1.1.1.1/8}
- {gatewayIP: 6.6.6.6, name: Internal, subnet: 2.2.2.2/16}
- {gatewayIP: 6.6.6.6, name: External, subnet: 5.5.5.1}
- {gatewayIP: 6.6.6.6, name: External, subnet: 5.5.5.2}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/468175.html
標籤:循环 可靠的 神社2 ansible-2.x
