|
582 | 582 | when: |
583 | 583 | - elasticsearch_security | bool |
584 | 584 | - inventory_hostname == elasticstack_ca_host |
| 585 | + |
| 586 | +# -- Persistent cluster settings via _cluster/settings API -- |
| 587 | + |
| 588 | +- name: Apply persistent cluster settings # noqa: run-once[task] |
| 589 | + when: |
| 590 | + - elasticsearch_cluster_settings | default({}) | length > 0 |
| 591 | + - not ansible_check_mode |
| 592 | + run_once: true |
| 593 | + delegate_to: "{{ elasticstack_ca_host | default(inventory_hostname) }}" |
| 594 | + block: |
| 595 | + - name: Read current persistent cluster settings |
| 596 | + ansible.builtin.uri: |
| 597 | + url: "{{ elasticsearch_http_protocol }}://{{ elasticsearch_api_host }}:{{ elasticstack_elasticsearch_http_port }}/_cluster/settings?flat_settings=true" |
| 598 | + method: GET |
| 599 | + user: "{{ 'elastic' if elasticsearch_security | bool else omit }}" |
| 600 | + password: "{{ elasticstack_password.stdout if elasticsearch_security | bool else omit }}" |
| 601 | + force_basic_auth: "{{ elasticsearch_security | bool }}" |
| 602 | + validate_certs: "{{ elasticsearch_validate_api_certs }}" |
| 603 | + return_content: true |
| 604 | + register: _es_current_cluster_settings |
| 605 | + no_log: "{{ elasticstack_no_log }}" |
| 606 | + |
| 607 | + - name: Check if settings already match |
| 608 | + ansible.builtin.set_fact: |
| 609 | + _es_cluster_settings_changed: "{{ _needs_update }}" |
| 610 | + vars: |
| 611 | + _current: "{{ _es_current_cluster_settings.json.persistent }}" |
| 612 | + _needs_update: >- |
| 613 | + {% set ns = namespace(changed=false) %} |
| 614 | + {% for key, value in elasticsearch_cluster_settings.items() %} |
| 615 | + {% if _current.get(key) is none or _current[key] | string != value | string %} |
| 616 | + {% set ns.changed = true %} |
| 617 | + {% endif %} |
| 618 | + {% endfor %} |
| 619 | + {{ ns.changed }} |
| 620 | +
|
| 621 | + - name: Apply cluster settings |
| 622 | + ansible.builtin.uri: |
| 623 | + url: "{{ elasticsearch_http_protocol }}://{{ elasticsearch_api_host }}:{{ elasticstack_elasticsearch_http_port }}/_cluster/settings" |
| 624 | + method: PUT |
| 625 | + body_format: json |
| 626 | + body: |
| 627 | + persistent: "{{ elasticsearch_cluster_settings }}" |
| 628 | + user: "{{ 'elastic' if elasticsearch_security | bool else omit }}" |
| 629 | + password: "{{ elasticstack_password.stdout if elasticsearch_security | bool else omit }}" |
| 630 | + force_basic_auth: "{{ elasticsearch_security | bool }}" |
| 631 | + validate_certs: "{{ elasticsearch_validate_api_certs }}" |
| 632 | + status_code: 200 |
| 633 | + no_log: "{{ elasticstack_no_log }}" |
| 634 | + when: _es_cluster_settings_changed | bool |
| 635 | + changed_when: true |
0 commit comments