diff --git a/roles/elasticsearch/defaults/main.yml b/roles/elasticsearch/defaults/main.yml index de21da6..4276ba4 100644 --- a/roles/elasticsearch/defaults/main.yml +++ b/roles/elasticsearch/defaults/main.yml @@ -1,6 +1,6 @@ --- - elasticsearch_version: 7.17.7 +elasticsearch_state: present elasticsearch_base_path: /opt/elasticsearch elasticsearch_data_path: "{{ elasticsearch_base_path }}/data" @@ -10,10 +10,23 @@ elasticsearch_config_discovery_type: single-node elasticsearch_config_boostrap_memory_lock: true elasticsearch_allocated_ram_mb: 512 -elasticsearch_container_image_name: docker.elastic.co/elasticsearch/elasticsearch-oss +elasticsearch_container_image_registry: docker.elastic.co +elasticsearch_container_image_namespace: elasticsearch +elasticsearch_container_image_name: elasticsearch-oss elasticsearch_container_image_tag: ~ -elasticsearch_container_image: >- - {{ elasticsearch_container_image_name }}:{{ elasticsearch_container_image_tag | default(elasticsearch_version, true) }} +elasticsearch_container_image: >-2 + {{ + ([ + elasticsearch_container_image_registry | default([], true), + elasticsearch_container_image_namespace | default([], true), + elasticsearch_container_image_name, + ] | flatten | join('/')) + + ':' + + (elasticsearch_container_image_tag | default(elasticsearch_version, true)) + }} +elasticsearch_container_image_source: pull +elasticsearch_container_image_force_source: >-2 + {{ elasticsearch_container_image_tag | default(false, true) | bool }} elasticsearch_container_name: elasticsearch elasticsearch_container_env: @@ -33,3 +46,5 @@ elasticsearch_container_volumes: elasticsearch_container_networks: ~ elasticsearch_container_purge_networks: ~ elasticsearch_container_restart_policy: unless-stopped +elasticsearch_container_state: >-2 + {{ (elasticsearch_state == 'present') | ternary('started', 'absent') }} diff --git a/roles/elasticsearch/tasks/main.yml b/roles/elasticsearch/tasks/main.yml index 76595bf..69a41ef 100644 --- a/roles/elasticsearch/tasks/main.yml +++ b/roles/elasticsearch/tasks/main.yml @@ -1,7 +1,13 @@ --- +- name: Check if state is valid + ansible.builtin.fail: + msg: >-2 + Unsupported state '{{ elasticsearch_state }}'. Supported + states are {{ elasticsearch_states | join(', ') }}. + when: elasticsearch_state not in elasticsearch_states -- name: Ensure host directories are present - file: +- name: Ensure host directories are {{ elasticsearch_state }} + ansible.builtin.file: path: "{{ item }}" state: directory mode: "0777" @@ -9,15 +15,16 @@ - "{{ elasticsearch_base_path }}" - "{{ elasticsearch_data_path }}" -- name: Ensure elastic container image is present - docker_image: +- name: Ensure elasticsearch container image '{{ elasticsearch_container_image }}' is {{ elasticsearch_state }} + community.docker.docker_image: name: "{{ elasticsearch_container_image }}" - state: present - source: pull + state: "{{ elasticsearch_state }}" + source: "{{ elasticsearch_container_image_source }}" + force_source: "{{ elasticsearch_container_image_force_source }}" force_source: "{{ elasticsearch_container_image_tag|default(false, true)|bool }}" -- name: Ensure elastic container is running - docker_container: +- name: Ensure elasticsearch container '{{ elasticsearch_container_name }}' is {{ elasticsearch_container_state }} + community.docker.docker_container: name: "{{ elasticsearch_container_name }}" image: "{{ elasticsearch_container_image }}" env: "{{ elasticsearch_container_env | default(omit, True) }}" @@ -29,4 +36,4 @@ networks: "{{ elasticsearch_container_networks | default(omit, True) }}" purge_networks: "{{ elasticsearch_container_purge_networks | default(omit, True) }}" restart_policy: "{{ elasticsearch_container_restart_policy }}" - state: started + state: "{{ elasticsearch_container_state }}" diff --git a/roles/elasticsearch/vars/main.yml b/roles/elasticsearch/vars/main.yml new file mode 100644 index 0000000..0d032cd --- /dev/null +++ b/roles/elasticsearch/vars/main.yml @@ -0,0 +1,4 @@ +--- +elasticsearch_states: + - present + - absent