Reputation: 504
My ansible playbook:
- 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
debug of the var: test.results
-- truncated. -- please go to the end of the page for full output.
"test.results": [
"json": {
"result": {
"name": "dept1",
"sys_id": "3fb1",
}
},
"json": {
"result": {
"name": "dept1",
"sys_id": "3fb1",
}
}]
debug of the 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"
}
]
}
You can see from the above, when I iterate though two lists and combine two list into one when the condition is satisfied, it for some reason put the output twice. I have tried many different ways but no luck.
So the result I want is:
for the var: final
- I want the ouput to only be one dict in the list like so
"final": [
{
"department": 'dept1",
"department_link": "https://servicenowservices.com/api/now/table/da/3fb1",
"hostname": "host1",
"value": "3fb1"
}]
Please let me know how I can achieve this.
Below is the full output of test.results
and I printed its type as well. Hope this will help!!
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"
}
Upvotes: 1
Views: 94
Reputation: 76
You are facing this issue because test.results varialbe have duplicate json blocks.
I have removed them using map and unique functions
Please try this modified task
- 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
Upvotes: 2