英文:
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'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论