Ansible snippet to update Elasticsearch cluster settings

Ansible snippet to update Elasticsearch cluster settings.

Updates settings only if required, works correctly with check mode.

Uses curl, because ansible get_url module does not support saving request result anywhere except a file.

- name: get elasticsearch settings
  shell: curl --silent ""
    warn: false
  register: get_es_settings
  check_mode: False
  changed_when: False
  tags: elk-elasticsearch-cluster-settings

- set_fact:
    es_settings: "{{ get_es_settings.stdout | from_json }}"
  check_mode: no
  tags: elk-elasticsearch-cluster-settings

- name: set elasticsearch settings only if it differs
  shell: >-
    curl --silent -XPUT '' -H 'Content-Type: application/json'
    --data '{ "persistent": { "{{ item.key }}": "{{ item.value }}" } }'
    warn: false
  loop: "{{ elk_elasticsearch_cluster_settings | dict2items }}"
  when: es_settings['persistent'].{{ item.key }} != "{{ item.value }}"
  tags: elk-elasticsearch-cluster-settings

License: This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License Share: share on reddit share on hacker news share on facebook share on twitter share on linkedin share on slashdot

No comments

You today

Comments are closed