如何在 Ansible 中仅打印非空列表结果
How to print only the non empty list results in Ansible
我想在输出(JSON)中只打印非空列表(pog_test)。下面的寄存器输出是另一个先前任务的结果。我什至尝试过使用 when 条件,但没有运气。是否可以在之前的任务本身(已在下面提到任务)中处理此问题,而不是创建一个单独的任务来过滤非空列表。
注册输出
{
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_facts": {
"pog_test": []
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": {
"dhcpBootOptionsEnabled": false,
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"po_id": "4654646"
}
},
{
"ansible_facts": {
"pog_test": [
{
"id": "782500435255624287",
"name": "PG-L-DE-MCC-COOLING_DMZ",
"networkIds": [],
"networkObjectIds": [
"12345678"
]
}
]
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": {
"dhcpBootOptionsEnabled": false,
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"po_id": "12345678"
}
}
],
"skipped": false
}
任务
- name: Check if the POG exists for all PO.
set_fact:
pog_test: "{{ pog | json_query(pogquery) }}"
vars:
pogquery: "[].pog[?contains(networkObjectIds, '{{ item.po_id }}')][]"
with_items: "{{ comborgpoidcool }}"
register: pogcheck
pog
[
{
"OrgId": "5555",
"OrgName": "abcd",
"pog": [
{
"id": "9546413",
"name": "test123",
"networkIds": [
"654654654"
],
"networkObjectIds": [
"12345678"
]
}
]
},
{
"OrgId": "9876546",
"OrgName": "Tdsqfsf",
"pog": []
},
{
"OrgId": "98798756",
"OrgName": "qdqsdqdqsd",
"pog": []
}
]
comborgpoidcool
[
{
"dhcpBootOptionsEnabled": false,
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"dhcpOptions": [],
"po_id": "4654646"
},
{
"dhcpBootOptionsEnabled": false,
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"po_id": "12345678"
}
]
预期输出
{
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_facts": {
"pog_test": [
{
"id": "782500435255624287",
"name": "PG-L-DE-MCC-COOLING_DMZ",
"networkIds": [],
"networkObjectIds": [
"12345678"
]
}
]
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": {
"dhcpBootOptionsEnabled": false,
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"po_id": "12345678"
}
}
],
"skipped": false
}
我最初的方法是获取 pog json 文件中的所有详细信息,该文件包含“pog”数组中的一些值。但是现在我用另一种方法来过滤长度== 0的pog数组(代码如下)。
when: (item.ansible_facts.pog_match | length == 0 )
我想在输出(JSON)中只打印非空列表(pog_test)。下面的寄存器输出是另一个先前任务的结果。我什至尝试过使用 when 条件,但没有运气。是否可以在之前的任务本身(已在下面提到任务)中处理此问题,而不是创建一个单独的任务来过滤非空列表。
注册输出
{
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_facts": {
"pog_test": []
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": {
"dhcpBootOptionsEnabled": false,
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"po_id": "4654646"
}
},
{
"ansible_facts": {
"pog_test": [
{
"id": "782500435255624287",
"name": "PG-L-DE-MCC-COOLING_DMZ",
"networkIds": [],
"networkObjectIds": [
"12345678"
]
}
]
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": {
"dhcpBootOptionsEnabled": false,
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"po_id": "12345678"
}
}
],
"skipped": false
}
任务
- name: Check if the POG exists for all PO.
set_fact:
pog_test: "{{ pog | json_query(pogquery) }}"
vars:
pogquery: "[].pog[?contains(networkObjectIds, '{{ item.po_id }}')][]"
with_items: "{{ comborgpoidcool }}"
register: pogcheck
pog
[
{
"OrgId": "5555",
"OrgName": "abcd",
"pog": [
{
"id": "9546413",
"name": "test123",
"networkIds": [
"654654654"
],
"networkObjectIds": [
"12345678"
]
}
]
},
{
"OrgId": "9876546",
"OrgName": "Tdsqfsf",
"pog": []
},
{
"OrgId": "98798756",
"OrgName": "qdqsdqdqsd",
"pog": []
}
]
comborgpoidcool
[
{
"dhcpBootOptionsEnabled": false,
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"dhcpOptions": [],
"po_id": "4654646"
},
{
"dhcpBootOptionsEnabled": false,
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"po_id": "12345678"
}
]
预期输出
{
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_facts": {
"pog_test": [
{
"id": "782500435255624287",
"name": "PG-L-DE-MCC-COOLING_DMZ",
"networkIds": [],
"networkObjectIds": [
"12345678"
]
}
]
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"item": {
"dhcpBootOptionsEnabled": false,
"dhcpHandling": "Run a DHCP server",
"dhcpLeaseTime": "1 day",
"po_id": "12345678"
}
}
],
"skipped": false
}
我最初的方法是获取 pog json 文件中的所有详细信息,该文件包含“pog”数组中的一些值。但是现在我用另一种方法来过滤长度== 0的pog数组(代码如下)。
when: (item.ansible_facts.pog_match | length == 0 )