-2

I have the following playbook, how can I clean up my returned data removing all the extra characters?

  tasks:
   - name: Health Status
     shell: curl -XGET 'http://{{ ansible_host }}:9200/_cluster/health?pretty=true'
     retries: 5
     delay: 10
     register: health

   - name: Print Health check
     debug: var=health.stdout_lines[2],health.stdout_lines[4]

Current Result

ok: [ELK22] => {
    "health.stdout_lines[2],health.stdout_lines[4]": "(u'  \"status\" : \"green\",', u'  \"number_of_nodes\" : 1,')"
}

Required Result

[ELK22] => {
status: green
number of nodes: 1
}
  • From your description snippet I understand that you like to know [How to check Elasticsearch cluster health?](https://stackoverflow.com/a/34410556/6771046), right? And you like to access/get specific keys from that JSON response only, right? – U880D May 17 '23 at 11:54
  • Yes, as you can see the current result I get the data I need but I want it sanitised removing the u,\, etc. – Peter Jackson May 17 '23 at 12:05
  • Since you get a JSON response already, you could get the key:values directly from it, I assume. Need to setup a short test since I know the syntax right now not out of my head ... – U880D May 17 '23 at 12:07
  • 1
    Seems like you are getting the string representation of a JSON, and want to convert it to a JSON with the [`from_json`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/from_json_filter.html) filter – β.εηοιτ.βε May 17 '23 at 12:21
  • @VladimirBotka I have provided the exact code I used along with the exact result. Why are you trying to close this post? – Peter Jackson May 18 '23 at 11:47
  • @VladimirBotka Do you have elasticsearch installed? – Peter Jackson May 18 '23 at 12:49

1 Answers1

1

Using the example output of How to check Elasticsearch cluster health?, a minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    health:
      stdout: |
        {
          "cluster_name" : "xxxxxxxx",
          "status" : "green",
          "timed_out" : false,
          "number_of_nodes" : 2,
          "number_of_data_nodes" : 2,
          "active_primary_shards" : 15,
          "active_shards" : 12,
          "relocating_shards" : 0,
          "initializing_shards" : 0,
          "unassigned_shards" : 0,
          "delayed_unassigned_shards" : 0,
          "number_of_pending_tasks" : 0,
          "number_of_in_flight_fetch" : 0
        }

  tasks:

  - name: Show value
    debug:
      msg: "{{ health.stdout | from_json | json_query('number_of_nodes') }}"

will result into an output of

TASK [Show value] ******
ok: [localhost] =>
  msg: '2'
U880D
  • 8,601
  • 6
  • 24
  • 40
  • Fails, get FAILED! => {"msg": "Unexpected templating type error occurred on ({{ health.stdout | from_json | json_query('number_of_nodes') }}): expected string or buffer"} – Peter Jackson May 17 '23 at 13:29
  • new error : {"msg": "Error in jmespath.search in json_query filter plugin:\n'dict object' has no attribute 'stdout'"} – Peter Jackson May 17 '23 at 15:20
  • Fails with: **FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\n** I used: - name: test debug: msg: "{{ health.stdout | from_yaml }}" - name: debug debug: var=health|type_debug – Peter Jackson May 18 '23 at 09:33
  • I know I've been told to use stout, but shouldn't I be using something that include stout_lines? – Peter Jackson May 18 '23 at 09:47
  • 1
    I have got your example working now thanks. I would like to bing back the status as well as I mentioned. But also with the text associated like: status : green number of nodes : 1 How can I add this to the jquery? – Peter Jackson May 19 '23 at 11:59
  • To [Extract several values from JSON using json_query in Ansible](https://stackoverflow.com/a/48256584/6771046) … – U880D May 19 '23 at 12:03