简体   繁体   中英

Ansible + Json + Loop parsing

Hoping someone maybe able to help. Currently stuck on this puzzle:)

I am trying to write a query that allows me to grab the data stored under sc_item_option_lookup.json.result.item_option_new.display_value and combine that with sc_item_option_lookup.json.result.value

Example: Request is for:8f306117db2fa200701a79fdae9619a8 Specs:''

On other queries I have ran to parse data I dont seem to have this much problems. I am not sure if its because I am using with_items to pull down the information and registering it into 1 variable.

I have tried variations of suggestions here: Using Ansible set_fact to create a dictionary from register results Ansible loop over JSON output from URI Call

I cant seem to get the data look right on the ideas above and end up getting issues with variables not found or my array not structuring properly.

Here is the ansible layout:

---
- 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

On 2.1 I get this:

{
    "sc_item_option_2": [
        "[AnsibleUndefined, AnsibleUndefined]",
        "[AnsibleUndefined, AnsibleUndefined]",
        "[AnsibleUndefined, AnsibleUndefined]",
        "[AnsibleUndefined, AnsibleUndefined]"
    ],
    "changed": false,
    "_ansible_verbose_always": true,
    "_ansible_no_log": false
}

On 3.1 It gets closer to what I want.

{
    "changed": false,
    "_ansible_verbose_always": true,
    "sc_item_option_3": "[] + [ '[u'Request is for']' ] + [ '[u'8f306117db2fa200701a79fdae9619a8']' ] + [ '[u'Specs:']' ] + [ '[u'']' ]",
    "_ansible_no_log": false
}

Json output i see in ansible:

{
    "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
}

I'm not entirely sure I understand what you're looking for, but the following task:

    - debug:
        msg: >-
          {{ item.json.result.0.item_option_new.display_value }}
          {{ item.json.result.0.value }}
      loop: "{{ sc_item_option_lookup.results }}"
      loop_control:
        label: "{{ item.x_transaction_id }}"

Will give you:

TASK [debug] *************************************************************************
ok: [localhost] => (item=8d6d880ddb11) => {
    "msg": "Request is for 8f306117db2fa200701a79fdae9619a8"
}
ok: [localhost] => (item=7d6d888ddbdd) => {
    "msg": "Specs: "
}

...which I think matches what you're describing in the first part of your question.

If you want to store that in a list variable, then you could write:

    - set_fact:
        sc_item_option_3: >-
          {{ sc_item_option_3|default([]) +
          [ "{}{}".format(
          item.json.result.0.item_option_new.display_value,
          item.json.result.0.value
          )]}}
      loop: "{{ sc_item_option_lookup.results }}"
      loop_control:
        label: "{{ item.x_transaction_id }}"

    - debug:
        var: sc_item_option_3

Which gets you:

TASK [set_fact] **********************************************************************
ok: [localhost] => (item=8d6d880ddb11)
ok: [localhost] => (item=7d6d888ddbdd)

TASK [debug] *************************************************************************
ok: [localhost] => {
    "sc_item_option_3": [
        "Request is for8f306117db2fa200701a79fdae9619a8",
        "Specs:"
    ]
}

Notes:

  • I am setting loop_control.label here just to reduce the amount of output during each loop iteration; you could drop that and it wouldn't have any effect on the result.

  • If you haven't seen the >- operator before, that's just a YAML quoting operator, which you can read about in Ansible's YAML Basics tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM