【发布时间】:2019-11-21 18:20:41
【问题描述】:
希望有人能提供帮助。目前卡在这个难题上:)
我正在尝试编写一个查询,允许我获取存储在 sc_item_option_lookup.json.result.item_option_new.display_value 下的数据并将其与 sc_item_option_lookup.json.result.value 结合起来
示例: 请求是:8f306117db2fa200701a79fdae9619a8 规格:''
在我跑来解析数据的其他查询中,我似乎没有这么多问题。我不确定是不是因为我使用 with_items 来下拉信息并将其注册到 1 个变量中。
我在这里尝试了各种不同的建议: Using Ansible set_fact to create a dictionary from register results Ansible loop over JSON output from URI Call
我似乎无法根据上述想法让数据看起来正确,并最终遇到未找到变量或我的数组结构不正确的问题。
这里是 ansible 布局:
---
- name: sc_items_options lookup (sc_item_option_lookup)
uri:
url: https://{{ instance }}/api/now/table/sc_item_option?sysparm_display_value=true&sysparm_query=sys_id={{ item }}&sysparm_fields=item_option_new,value
user: "{{ sn_username }}"
password: "{{ sn_password }}"
method: GET
headers:
"Content-Type": "application/json"
"Accept": "application/json"
body_format: json
return_content: yes
register: sc_item_option_lookup
with_items: "{{ sc_item_option_mtom }}"
- name: 1.1-debug.1 print display value to query (sc_item_option_lookup)
debug: var=sc_item_option_lookup
- name: 1.2-debug.1 print display value to query (sc_item_option_lookup)
debug: var=sc_item_option_lookup.results
- name: 1.3-register results (sc_item_option_lookup)
set_fact:
sc_item_option: "{{ item.json.result }}"
with_items: "{{ sc_item_option_lookup.results }}"
- name: 1.4-debug.1 print display value to query (sc_item_option_lookup)
debug: var=sc_item_option
- name: 1.5-register results (sc_item_option_lookup)
set_fact:
sc_item_option_data: "{{ item }}"
with_items: "{{ sc_item_option }}"
- name: 1.6-debug.1 print display value to query (sc_item_option_lookup)
debug: var=sc_item_option_data
- name: 2-register results (sc_item_option_lookup)
set_fact:
sc_item_option_2: "{{ sc_item_option_2 }} + [ '{{ item | map(attribute='item_option_new.display_value') | list }}' ] + [ '{{ item | map(attribute='value') | list }}' ]"
with_items: "{{ sc_item_option_lookup.results | map(attribute='content') | map('from_json') | map(attribute='result') | list }}"
- name: 2.1-debug.1 print display value to query (sc_item_option_lookup)
debug: var=sc_item_option_2
- name: 2.2-debug.1 print display value to query (sc_item_option_lookup)
debug:
var: sc_item_option_2
- name: 3-register results (sc_item_option_lookup)
set_fact:
sc_item_option_3: "{{ sc_item_option_3 }} + [ '{{ item | map(attribute='item_option_new.display_value') | list }}' ] + [ '{{ item | map(attribute='value') | list }}' ]"
loop: "{{ sc_item_option_lookup.results | map(attribute='content') | map('from_json') | map(attribute='result') | list }}"
- name: 3.1-debug.1 print display value to query (sc_item_option_lookup)
debug: var=sc_item_option_3
- name: 3.1-debug.1 print display value to query (sc_item_option_lookup)
debug:
var: sc_item_option_3
- name: 4-debug.1 print display value to query (sc_item_option_lookup)
debug: var=sc_item_option_lookup.results.json.result
在 2.1 上我明白了:
{
"sc_item_option_2": [
"[AnsibleUndefined, AnsibleUndefined]",
"[AnsibleUndefined, AnsibleUndefined]",
"[AnsibleUndefined, AnsibleUndefined]",
"[AnsibleUndefined, AnsibleUndefined]"
],
"changed": false,
"_ansible_verbose_always": true,
"_ansible_no_log": false
}
在 3.1 上它更接近我想要的。
{
"changed": false,
"_ansible_verbose_always": true,
"sc_item_option_3": "[] + [ '[u'Request is for']' ] + [ '[u'8f306117db2fa200701a79fdae9619a8']' ] + [ '[u'Specs:']' ] + [ '[u'']' ]",
"_ansible_no_log": false
}
我在 ansible 中看到的 Json 输出:
{
"changed": false,
"sc_item_option_lookup": {
"msg": "All items completed",
"changed": false,
"results": [
{
"status": 200,
"cookies": {
},
"changed": false,
"pragma": "no-store,no-cache",
"set_cookie": "glide_user=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly;Secure, glide_user_session=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly;Secure, glide_user_route=glide.90e24e980d8f8fe786ea244415b54ec6; Max-Age=2147483647; Expires=Tue, 09-Dec-2087 21:03:29 GMT; Path=/; HttpOnly;Secure, glide_session_store=706D840DDB11C050EA6379FDAE96193F; Max-Age=300; Expires=Thu, 21-Nov-2019 17:54:22 GMT; Path=/; HttpOnly;Secure",
"expires": "0",
"elapsed": 1,
"msg": "OK (unknown bytes)",
"x_transaction_id": "8d6d880ddb11",
"content_type": "application/json;charset=UTF-8",
"date": "Thu, 21 Nov 2019 17:49:21 GMT",
"x_total_count": "1",
"cookies_string": ,
"ansible_loop_var": "item",
"item": "31f5a1d0db51005068c7f9baae9619f3",
"url": "https://.com/api/now/table/sc_item_option?sysparm_display_value=true&sysparm_query=sys_id=31f5a1d0db51005068c7f9baae9619f3&sysparm_fields=item_option_new,value",
"transfer_encoding": "chunked",
"strict_transport_security": "max-age=63072000; includeSubDomains",
"x_is_logged_in": "true",
"connection": "close",
"server": "ServiceNow",
"content": "{\"result\":[{\"item_option_new\":{\"display_value\":\"Request is for\",\"link\":\"https://.com/api/now/table/item_option_new/6a7b31ecdbb32600701a79fdae961903\"},\"value\":\"8f306117db2fa200701a79fdae9619a8\"}]}",
"failed": false,
"json": {
"result": [
{
"item_option_new": {
"link": "https://.com/api/now/table/item_option_new/6a7b31ecdbb32600701a79fdae961903",
"display_value": "Request is for"
},
"value": "8f306117db2fa200701a79fdae9619a8"
}
]
},
"invocation": {
"module_args": {
"directory_mode": null,
"force": false,
"remote_src": null,
"status_code": [
200
],
"follow": false,
"owner": null,
"body_format": "json",
"client_key": null,
"group": null,
"use_proxy": true,
"unix_socket": null,
"unsafe_writes": null,
"setype": null,
"content": null,
"serole": null,
"follow_redirects": "safe",
"return_content": true,
"client_cert": null,
"body": null,
"timeout": 30,
"src": null,
"dest": null,
"selevel": null,
"force_basic_auth": false,
"removes": null,
"http_agent": "ansible-httpget",
"regexp": null,
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"url": "https://.com/api/now/table/sc_item_option?sysparm_display_value=true&sysparm_query=sys_id=31f5a1d0db51005068c7f9baae9619f3&sysparm_fields=item_option_new,value",
"validate_certs": true,
"seuser": null,
"method": "GET",
"creates": null,
"headers": {
"Content-Type": "application/json",
"Accept": "application/json"
},
"delimiter": null,
"mode": null,
"attributes": null,
"backup": null
}
},
"redirected": false,
"cache_control": "no-cache,no-store,must-revalidate,max-age=-1"
},
{
"status": 200,
"cookies": {
},
"changed": false,
"pragma": "no-store,no-cache",
"set_cookie": "glide_user=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly;Secure, glide_user_session=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly;Secure, glide_user_route=glide.4c248572c7f7726ac64e64548c99cafd; Max-Age=2147483647; Expires=Tue, 09-Dec-2087 21:03:33 GMT; Path=/; HttpOnly;Secure, glide_session_store=7D6D888DDBDD805068C7F9BAAE96197E; Max-Age=300; Expires=Thu, 21-Nov-2019 17:54:26 GMT; Path=/; HttpOnly;Secure",
"expires": "0",
"elapsed": 1,
"msg": "OK (unknown bytes)",
"x_transaction_id": "7d6d888ddbdd",
"content_type": "application/json;charset=UTF-8",
"date": "Thu, 21 Nov 2019 17:49:26 GMT",
"x_total_count": "1",
"cookies_string": ,
"ansible_loop_var": "item",
"item": "7df56510db91005068c7f9baae961957",
"url": "https://.com/api/now/table/sc_item_option?sysparm_display_value=true&sysparm_query=sys_id=7df56510db91005068c7f9baae961957&sysparm_fields=item_option_new,value",
"transfer_encoding": "chunked",
"strict_transport_security": "max-age=63072000; includeSubDomains",
"x_is_logged_in": "true",
"connection": "close",
"server": "ServiceNow",
"content": "{\"result\":[{\"item_option_new\":{\"display_value\":\"Specs:\",\"link\":\"https://.com/api/now/table/item_option_new/0e508f4adb8b9b00ec2ff3671d9619fc\"},\"value\":\"\"}]}",
"failed": false,
"json": {
"result": [
{
"item_option_new": {
"link": "https://.com/api/now/table/item_option_new/0e508f4adb8b9b00ec2ff3671d9619fc",
"display_value": "Specs:"
},
"value": ""
}
]
},
"invocation": {
"module_args": {
"directory_mode": null,
"force": false,
"remote_src": null,
"status_code": [
200
],
"follow": false,
"owner": null,
"body_format": "json",
"client_key": null,
"group": null,
"use_proxy": true,
"unix_socket": null,
"unsafe_writes": null,
"setype": null,
"content": null,
"serole": null,
"follow_redirects": "safe",
"return_content": true,
"client_cert": null,
"body": null,
"timeout": 30,
"src": null,
"dest": null,
"selevel": null,
"force_basic_auth": false,
"removes": null,
"http_agent": "ansible-httpget",
"user": ,
"regexp": null,
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"url": "https://.com/api/now/table/sc_item_option?sysparm_display_value=true&sysparm_query=sys_id=7df56510db91005068c7f9baae961957&sysparm_fields=item_option_new,value",
"validate_certs": true,
"seuser": null,
"method": "GET",
"creates": null,
"headers": {
"Content-Type": "application/json",
"Accept": "application/json"
},
"delimiter": null,
"mode": null,
"attributes": null,
"backup": null
}
},
"redirected": false,
"cache_control": "no-cache,no-store,must-revalidate,max-age=-1"
}
]
},
"_ansible_verbose_always": true,
"_ansible_no_log": false
}
【问题讨论】: