0

I'm querying Splunk via the uri module w a certain search:

- name: splunk query
  uri:
    url: https://localhost:8089/servicesNS/admin/search/search/jobs/export?output_mode=json
    method: POST
    user: ...
    password: ...
    validate_certs: false
    body_format: form-urlencoded
    return_content: true
    headers:
      Content-Type: application/json
    body:
      - [ search, "{{ splunk_search }}" ]
  vars:
    - splunk_search: '| ...'
  register: me

Splunk returns a 200, w content:

TASK [debug] ********************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "{\n  \"title\": \"clientMessageId\",\n  \"app\": \"whatever\"\n}\n{\n  \"title\": \"a_title\",\n  \"app\": \"some_app\"\n}\n{\n  \"title\": \"another_title\",\n  \"app\": \"my_app\"\n}\n{\n  \"title\": \"title_for_app\",\n  \"app\": \"another_app\"\n}"
}

However, I can't properly parse the output, I've tried:

- name: query result from content
  debug:
    msg: "{{ me.content | json_query('title') }}"

In this case, Ansible will return an empty string. I assume something is wrong with the formatting of the output.

Q: How can I configure Ansible to properly parse the json output, so that a new list is created based on the output.

Example:

my_list:
  - title: clientMessageId
    app: whatever
  - title: a_title
    app: some_app
...

Kevin C
  • 4,851
  • 8
  • 30
  • 64

1 Answers1

0

What I did was change the url output_mode in which Splunk returns the content from json to csv.

url: https://localhost:8089/servicesNS/admin/search/search/jobs/export?output_mode=csv

Then, I wrote the output to a file and read it using the ansible read_csv module.

- name: write received content to csv file on controller
  copy:
    content: "{{ me.content }}"
    dest: "./file.csv"
    mode: '0775'
  delegate_to: localhost

- name: read csv file
  read_csv:
    path: ./file.csv
  delegate_to: localhost
  register: read_csv

- name: set fact to create list
  set_fact:
    the_output: "{{ read_csv.list }}"
Kevin C
  • 4,851
  • 8
  • 30
  • 64