Ansible清理任务输出

huangapple go评论56阅读模式
英文:

Ansible clean up task output

问题

[ELK22] => {
status: green
number of nodes: 1
}
英文:

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
}

答案1

得分: 1

以下是代码部分的翻译:

---
- 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') }}"

将产生以下输出:

TASK [Show value] ******
ok: [localhost] =>
  msg: '2'
英文:

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'

huangapple
  • 本文由 发表于 2023年5月17日 19:34:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定