Ansible subelements()筛选器,其中子元素是空列表
尝试使用Ansible subelements()筛选器,其中子元素是空列表,ansible,jinja2,Ansible,Jinja2,尝试使用subelement()过滤器,有时列表将为空。我仍然希望得到一个列表,其中填充了item.0,但item.1可能是未定义的或无。我不确定这是否可行 示例剧本 - hosts: localhost connection: local vars: result: virtual_machine: interfaces: - name: interface 1 ip_addresses:
subelement()
过滤器,有时列表将为空。我仍然希望得到一个列表,其中填充了item.0
,但item.1
可能是未定义的或无。我不确定这是否可行
示例剧本
- hosts: localhost
connection: local
vars:
result:
virtual_machine:
interfaces:
- name: interface 1
ip_addresses:
- address: 192.168.1.22
enabled: yes
- name: interface 2
ip_addresses: []
tasks:
- name: starting var
debug: var=result
- name: filter data
set_fact:
vm_interfaces: >
{{ result.virtual_machine.interfaces
| default({})
| subelements('ip_addresses', skip_missing=True)
| list }}
- name: new filtered var
debug: var=vm_interfaces
- name: loop through new var
debug:
msg: '{{ item.0.name }} {{ item.1.address }}'
loop: '{{ vm_interfaces }}'
loop_control:
label: '{{ item.0.name }}'
样本输出
TASK [starting var] *************************************************************************************************
ok: [localhost] => {
"result": {
"virtual_machine": {
"interfaces": [
{
"ip_addresses": [
{
"address": "192.168.1.22",
"enabled": true
}
],
"name": "interface 1"
},
{
"ip_addresses": [],
"name": "interface 2"
}
]
}
}
}
TASK [filter data] **************************************************************************************************
ok: [localhost]
TASK [new filtered var] *********************************************************************************************
ok: [localhost] => {
"vm_interfaces": [
[
{
"ip_addresses": [
{
"address": "192.168.1.22",
"enabled": true
}
],
"name": "interface 1"
},
{
"address": "192.168.1.22",
"enabled": true
}
]
]
}
TASK [loop through new var] *****************************************************************************************
ok: [localhost] => (item=interface 1) => {
"msg": "interface 1 192.168.1.22"
电流较小的Ansible-y解决方案
我目前解决这个问题的方法是使用嵌套的Include_tasks
,基本上是在(2)个循环中。这是可行的,但我的理解是,人们应该努力将数据操作到可以使用循环的程度。这里有一种可能的方法。我只是将空ip列表替换为包含空对象的列表。适应你自己的需要
-hosts:localhost
收集事实:错误
变量:
结果:
虚拟机:
接口:
-名称:接口1
ip地址:
-地址:192.168.1.22
启用:是
-名称:接口2
ip_地址:[]
替换\u空\u接口:
ip_地址:[{}]
vm\u接口\u空:>
{{
result.virtual_machine.interfaces
|默认值({})
|选择attr('ip\U地址','defined')
|选择attr('ip_地址','==',[]))
|映射('combine',replace\u empty\u接口)
|名单
}}
虚拟机接口填充:>
{{
result.virtual_machine.interfaces
|默认值({})
|选择attr('ip\U地址','defined')
|rejectattr('ip_地址','==',[]))
|名单
}}
虚拟机接口:>
{{
(虚拟机接口为空+虚拟机接口为满)
|子元素(“ip_地址”)
|名单
}}
任务:
-调试:
msg:{{item.0}}{{{item.1}}
循环:“{vm_interfaces}}”
结果:
TASK [debug] ***********************************************************************************************************************************************************************************
Sunday 09 May 2021 10:14:13 +0200 (0:00:00.088) 0:00:00.088 ************
ok: [localhost] => (item=[{'name': 'interface 2', 'ip_addresses': [{}]}, {}]) => {
"msg": "{'name': 'interface 2', 'ip_addresses': [{}]} | {}"
}
ok: [localhost] => (item=[{'name': 'interface 1', 'ip_addresses': [{'address': '192.168.1.22', 'enabled': True}]}, {'address': '192.168.1.22', 'enabled': True}]) => {
"msg": "{'name': 'interface 1', 'ip_addresses': [{'address': '192.168.1.22', 'enabled': True}]} | {'address': '192.168.1.22', 'enabled': True}"
}
PLAY RECAP *************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0