在两个列表中迭代时,将相同的值追加两次



我的可靠策略:

- hosts: localhost
connection: local
gather_facts: no
become: no
vars:
- delete: [
{
"department_link": "https://servicenowservices.com/api/now/table/da/3fb1",
"hostname": "host1",
"value": "3fb1"
},
{
"department_link": "https://servicenowservices.com/api/now/table/da/3fb1",
"hostname": "host2",
"value": "3fb1133"
}
]
tasks:
- name: connect to service now cmdb table
uri:
url: "{{item.department_link}}"
method: GET
body_format: form-urlencoded
user: "user"
password: "password"
timeout: 200
validate_certs: no
loop: "{{delete}}"
register: test
no_log: yes
- debug:
var: test.results
- name: combine
set_fact:
final: "{{ final | default([]) + [ item[0] | combine({'department': item[1].json.result.name | default(None)}) ]  }}"
when:
- item[1].json.result.sys_id | string == item[0].value | string
with_nested:
- "{{delete}}"
- "{{test.results}}"
no_log: yes
- debug:
var: final

var的调试:test.results--截断。--请转到页面末尾查看完整输出。

"test.results": [
"json": {
"result": {
"name": "dept1",
"sys_id": "3fb1",
}
},
"json": {
"result": {
"name": "dept1",
"sys_id": "3fb1",
}
}]

var的调试:final

ok: [localhost] => {
"final": [
{
"department": 'dept1",
"department_link": "https://servicenowservices.com/api/now/table/da/3fb1",
"hostname": "host1",
"value": "3fb1"
},
{
"department": "dept1",
"department_link": "https://servicenowservices.com/api/now/table/da/3fb1",
"hostname": "host1",
"value": "3fb1"
}
]
}

从上面可以看出,当我遍历两个列表,并在条件满足时将两个列表合并为一个列表时,出于某种原因,它将输出两次。我尝试了很多不同的方法,但没有成功。

所以我想要的结果是:对于var:final-我希望输出在列表中只有一个dict,就像一样

"final": [
{
"department": 'dept1",
"department_link": "https://servicenowservices.com/api/now/table/da/3fb1",
"hostname": "host1",
"value": "3fb1"
}]

请告诉我怎样才能做到这一点。

以下是test.results的完整输出,我也打印了它的类型。希望这会有所帮助!!

TASK [debug] 
ok: [localhost] => {
"test.results": [
{
"ansible_loop_var": "item",
"cache_control": "no-cache,no-store,must-revalidate,max-age=-1",
"changed": false,
"connection": "close",
"content_type": "application/json;charset=UTF-8",
"cookies": {
"cook": "random input"
},
"cookies_string": "cookies",
"date": "Thu, 06 Oct 2022 17:03:50 GMT",
"elapsed": 0,
"expires": "0",
"failed": false,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"body": null,
"body_format": "raw",
"client_cert": null,
"client_key": null,
"content": null,
"creates": null,
"delimiter": null,
"dest": null,
"directory_mode": null,
"follow": false,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": false,
"group": null,
"headers": {},
"http_agent": "ansible-httpget",
"method": "GET",
"mode": null,
"owner": null,
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"regexp": null,
"remote_src": null,
"removes": null,
"return_content": false,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
200
],
"timeout": 2000,
"unix_socket": null,
"unsafe_writes": false,
"url": "https://servicenowservices.com/api/now/table/da/3fb1",
"url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"url_username": "user",
"use_proxy": true,
"user": "user",
"validate_certs": false
}
},
"item": {
"department_link": "https://servicenowservices.com/api/now/table/da/3fb1",
"hostname": "host1",
"value": "3fb1"
},
"json": {
"result": {
"business_unit": "",
"code": "",
"cost_center": "",
"dept_head": "",
"description": "",
"head_count": "",
"id": "",
"name": "FZB2",
"parent": "",
"primary_contact": "",
"sys_created_by": "$test",
"sys_created_on": "2017-01-25 05:43:39",
"sys_id": "3fb1",
"sys_mod_count": "0",
"sys_tags": "",
"sys_updated_by": "$test",
"sys_updated_on": "2017-01-25 05:43:39"
}
},
"msg": "OK (unknown bytes)",
"pragma": "no-store,no-cache",
"redirected": false,
"server": "ServiceNow",
,
"status": 200,
"strict_transport_security": "max-age=63072000; includeSubDomains",
"transfer_encoding": "chunked",
"url": "https://servicenowservices.com/api/now/table/da/3fb1",
"x_is_logged_in": "true",
"x_transaction_id": "id"
},
{
"ansible_loop_var": "item",
"cache_control": "no-cache,no-store,must-revalidate,max-age=-1",
"changed": false,
"connection": "close",
"content_type": "application/json;charset=UTF-8",
"cookies": {
"cook": "random input",
},
"cookies_string": "cookies",
"date": "Thu, 06 Oct 2022 17:03:51 GMT",
"elapsed": 0,
"expires": "0",
"failed": false,
"invocation": {
"module_args": {
"attributes": null,
"backup": null,
"body": null,
"body_format": "raw",
"client_cert": null,
"client_key": null,
"content": null,
"creates": null,
"delimiter": null,
"dest": null,
"directory_mode": null,
"follow": false,
"follow_redirects": "safe",
"force": false,
"force_basic_auth": false,
"group": null,
"headers": {},
"http_agent": "ansible-httpget",
"method": "GET",
"mode": null,
"owner": null,
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"regexp": null,
"remote_src": null,
"removes": null,
"return_content": false,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"status_code": [
200
],
"timeout": 200,
"unix_socket": null,
"unsafe_writes": false,
"url": "https://servicenowservices.com/api/now/table/da/3fb1",
"url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"url_username": "user",
"use_proxy": true,
"user": "user",
"validate_certs": false
}
},
"item": {
"department_link": "https://servicenowservices.com/api/now/table/da/3fb1",
"hostname": "host2",
"value": "3fb1"
},
"json": {
"result": {
"business_unit": "",
"code": "",
"cost_center": "",
"dept_head": "",
"description": "",
"head_count": "",
"id": "",
"name": "FZB2",
"parent": "",
"primary_contact": "",
"sys_created_by": "$test",
"sys_created_on": "2017-01-25 05:43:39",
"sys_id": "id",
"sys_mod_count": "0",
"sys_tags": "",
"sys_updated_by": "$test",
"sys_updated_on": "2017-01-25 05:43:39"
}
},
"msg": "OK (unknown bytes)",
"pragma": "no-store,no-cache",
"redirected": false,
"server": "ServiceNow",
"set_cookie": "glide_user=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly; SameSite=None; Secure, glide_user_session=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly; SameSite=None; Secure, glide_user_route=glide.idd; Max-Age=2147483647; Expires=Tue, 24-Oct-2090 20:17:58 GMT; Path=/; HttpOnly; SameSite=None; Secure, glide_session_store=3348D7BA1B561150C71B8557EC4BCB27; Max-Age=300; Expires=Thu, 06-Oct-2022 17:08:51 GMT; Path=/; HttpOnly; SameSite=None; Secure",
"status": 200,
"strict_transport_security": "max-age=63072000; includeSubDomains",
"transfer_encoding": "chunked",
"url": "https://servicenowservices.com/api/now/table/da/3fb1",
"x_is_logged_in": "true",
"x_transaction_id": "123"
}
]
}
TASK [debug] 
ok: [localhost] => {
"test.results | type_debug": "list"
}

您面临这个问题是因为test.results-variabel有重复的json块。

我已经删除了他们使用地图和独特的功能

请尝试此修改后的任务

- name: combine
set_fact:
final: "{{ final | default([]) + [ item[0] | combine({'department': item[1].result.name | default(None)}) ]  }}"
when:
- item[1].result.sys_id | string == item[0].value | string
with_nested:
- "{{delete}}"
- "{{test.results | map(attribute='json') | unique }}"
no_log: yes

最新更新