refactor(elasticsearch): add state=absent support, migrate to fully-qualified module names, split container image into parts

This commit is contained in:
transcaffeine 2024-10-28 20:24:14 +01:00
parent 10dc0bed04
commit dfff3cf08b
Signed by: transcaffeine
GPG Key ID: 03624C433676E465
3 changed files with 39 additions and 13 deletions

View File

@ -1,6 +1,6 @@
--- ---
elasticsearch_version: 7.17.7 elasticsearch_version: 7.17.7
elasticsearch_state: present
elasticsearch_base_path: /opt/elasticsearch elasticsearch_base_path: /opt/elasticsearch
elasticsearch_data_path: "{{ elasticsearch_base_path }}/data" elasticsearch_data_path: "{{ elasticsearch_base_path }}/data"
@ -10,10 +10,23 @@ elasticsearch_config_discovery_type: single-node
elasticsearch_config_boostrap_memory_lock: true elasticsearch_config_boostrap_memory_lock: true
elasticsearch_allocated_ram_mb: 512 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_tag: ~
elasticsearch_container_image: >- elasticsearch_container_image: >-2
{{ elasticsearch_container_image_name }}:{{ elasticsearch_container_image_tag | default(elasticsearch_version, true) }} {{
([
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_name: elasticsearch
elasticsearch_container_env: elasticsearch_container_env:
@ -33,3 +46,5 @@ elasticsearch_container_volumes:
elasticsearch_container_networks: ~ elasticsearch_container_networks: ~
elasticsearch_container_purge_networks: ~ elasticsearch_container_purge_networks: ~
elasticsearch_container_restart_policy: unless-stopped elasticsearch_container_restart_policy: unless-stopped
elasticsearch_container_state: >-2
{{ (elasticsearch_state == 'present') | ternary('started', 'absent') }}

View File

@ -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 - name: Ensure host directories are {{ elasticsearch_state }}
file: ansible.builtin.file:
path: "{{ item }}" path: "{{ item }}"
state: directory state: directory
mode: "0777" mode: "0777"
@ -9,15 +15,16 @@
- "{{ elasticsearch_base_path }}" - "{{ elasticsearch_base_path }}"
- "{{ elasticsearch_data_path }}" - "{{ elasticsearch_data_path }}"
- name: Ensure elastic container image is present - name: Ensure elasticsearch container image '{{ elasticsearch_container_image }}' is {{ elasticsearch_state }}
docker_image: community.docker.docker_image:
name: "{{ elasticsearch_container_image }}" name: "{{ elasticsearch_container_image }}"
state: present state: "{{ elasticsearch_state }}"
source: pull source: "{{ elasticsearch_container_image_source }}"
force_source: "{{ elasticsearch_container_image_force_source }}"
force_source: "{{ elasticsearch_container_image_tag|default(false, true)|bool }}" force_source: "{{ elasticsearch_container_image_tag|default(false, true)|bool }}"
- name: Ensure elastic container is running - name: Ensure elasticsearch container '{{ elasticsearch_container_name }}' is {{ elasticsearch_container_state }}
docker_container: community.docker.docker_container:
name: "{{ elasticsearch_container_name }}" name: "{{ elasticsearch_container_name }}"
image: "{{ elasticsearch_container_image }}" image: "{{ elasticsearch_container_image }}"
env: "{{ elasticsearch_container_env | default(omit, True) }}" env: "{{ elasticsearch_container_env | default(omit, True) }}"
@ -29,4 +36,4 @@
networks: "{{ elasticsearch_container_networks | default(omit, True) }}" networks: "{{ elasticsearch_container_networks | default(omit, True) }}"
purge_networks: "{{ elasticsearch_container_purge_networks | default(omit, True) }}" purge_networks: "{{ elasticsearch_container_purge_networks | default(omit, True) }}"
restart_policy: "{{ elasticsearch_container_restart_policy }}" restart_policy: "{{ elasticsearch_container_restart_policy }}"
state: started state: "{{ elasticsearch_container_state }}"

View File

@ -0,0 +1,4 @@
---
elasticsearch_states:
- present
- absent