我有一个空字典'ixes',应该填充从外部源收集到'peeringdb_response_net'的数据。
vars:
ixes:
ix_id_001:
ix_short_name: 'ix1'
ixlan_id: '331'
ix_neighbors:
ix_id_002:
ix_short_name: 'ix2'
ixlan_id: '222'
ix_neighbors:
{
"peeringdb_response_net": {
"data": [
{
"aka": "BIX.BG",
"netixlan_set": [
{
"asn": 15669,
"ipaddr4": "193.169.199.10",
"ipaddr6": "2001:7f8:58::3d35:0:2",
"ixlan_id": 331,
"operational": true,
"status": "ok",
},
{
"asn": 15669,
"ipaddr4": "193.169.198.10",
"ipaddr6": "2001:7f8:58::3d35:0:1",
"ixlan_id": 331,
"operational": true,
"status": "ok",
}
],
}
],
}
}
请帮助我找到获得以下结果的方法。只有当&;status&;; &;ok&;; &;operational&;;;;平等的。'ix_neighbor_short_name'应该被索引。
vars:
ixes:
ix_id_001:
ix_short_name: 'ix1'
ixlan_id: '331'
ix_neighbors:
- { ix_neighbor_ipv4_addr: '193.169.199.10', ix_neighbor_short_name: 'rs1' }
- { ix_neighbor_ipv4_addr: '193.169.198.10', ix_neighbor_short_name: 'rs2' }
ix_id_002:
ix_short_name: 'ix2'
ixlan_id: '222'
ix_neighbors:
不知道怎么写
给定数据
peeringdb_response_net:
data:
- aka: BIX.BG
netixlan_set:
- asn: 15669
ipaddr4: 193.169.199.10
ipaddr6: 2001:7f8:58::3d35:0:2
ixlan_id: 331
operational: true
status: ok
- asn: 15669
ipaddr4: 193.169.198.10
ipaddr6: 2001:7f8:58::3d35:0:1
ixlan_id: 331
operational: true
status: ok
选择集合
sets: "{{ peeringdb_response_net.data|
map(attribute='netixlan_set')|
map('groupby', 'ixlan_id')|
list }}"
为
sets:
- - - 331
- - asn: 15669
ipaddr4: 193.169.199.10
ipaddr6: 2001:7f8:58::3d35:0:2
ixlan_id: 331
operational: true
status: ok
- asn: 15669
ipaddr4: 193.169.198.10
ipaddr6: 2001:7f8:58::3d35:0:1
ixlan_id: 331
operational: true
status: ok
使用Jinja创建结构并将其从YAML转换为
ixes_str: |
{% for set in sets %}
ix_id_{{ "%03d"|format(loop.index) }}:
ix_short_name: 'ix{{ loop.index }}'
ixlan_id: '{{ set.0.0 }}'
ix_neighbors: [
{% for neighbor in set.0.1 %}
{% if neighbor.status == 'ok' and neighbor.operational %}
{ix_neighbor_ipv4_addr: '{{ neighbor.ipaddr4 }}',
ix_neighbor_short_name: 'rs{{ loop.index }}'},
{% endif %}
{% endfor %}
]
{% endfor %}
ixes: "{{ ixes_str|from_yaml }}"
为
ixes:
ix_id_001:
ix_neighbors:
- ix_neighbor_ipv4_addr: 193.169.199.10
ix_neighbor_short_name: rs1
- ix_neighbor_ipv4_addr: 193.169.198.10
ix_neighbor_short_name: rs2
ix_short_name: ix1
ixlan_id: '331'
你可以使用默认的字典
ixes_default:
ix_id_001:
ix_short_name: 'ix1'
ixlan_id: '331'
ix_neighbors:
ix_id_002:
ix_short_name: 'ix2'
ixlan_id: '222'
ix_neighbors:
和组合字典
ixes: "{{ ixes_default|combine(ixes_str|from_yaml) }}"
为
ixes:
ix_id_001:
ix_neighbors:
- ix_neighbor_ipv4_addr: 193.169.199.10
ix_neighbor_short_name: rs1
- ix_neighbor_ipv4_addr: 193.169.198.10
ix_neighbor_short_name: rs2
ix_short_name: ix1
ixlan_id: '331'
ix_id_002:
ix_neighbors: null
ix_short_name: ix2
ixlan_id: '222'
用于测试的完整剧本示例
- hosts: localhost
vars:
peeringdb_response_net:
data:
- aka: BIX.BG
netixlan_set:
- asn: 15669
ipaddr4: 193.169.199.10
ipaddr6: 2001:7f8:58::3d35:0:2
ixlan_id: 331
operational: true
status: ok
- asn: 15669
ipaddr4: 193.169.198.10
ipaddr6: 2001:7f8:58::3d35:0:1
ixlan_id: 331
operational: true
status: ok
sets: "{{ peeringdb_response_net.data|
map(attribute='netixlan_set')|
map('groupby', 'ixlan_id')|
list }}"
ixes_str: |
{% for set in sets %}
ix_id_{{ "%03d"|format(loop.index) }}:
ix_short_name: 'ix{{ loop.index }}'
ixlan_id: '{{ set.0.0 }}'
ix_neighbors: [
{% for neighbor in set.0.1 %}
{% if neighbor.status == 'ok' and neighbor.operational %}
{ix_neighbor_ipv4_addr: '{{ neighbor.ipaddr4 }}',
ix_neighbor_short_name: 'rs{{ loop.index }}'},
{% endif %}
{% endfor %}
]
{% endfor %}
ixes1: "{{ ixes_str|from_yaml }}"
ixes2: "{{ ixes_default|combine(ixes_str|from_yaml) }}"
ixes_default:
ix_id_001:
ix_short_name: 'ix1'
ixlan_id: '331'
ix_neighbors:
ix_id_002:
ix_short_name: 'ix2'
ixlan_id: '222'
ix_neighbors:
tasks:
- debug:
var: peeringdb_response_net
- debug:
var: sets
- debug:
var: ixes_str
- debug:
var: ixes1
- debug:
var: ixes2