refactor(alertmanager): add state=absent support

This commit is contained in:
transcaffeine 2024-10-14 18:01:29 +02:00
parent d8a3b0ab75
commit fb0915841d
Signed by: transcaffeine
GPG Key ID: 03624C433676E465
8 changed files with 80 additions and 40 deletions

View File

@ -18,3 +18,4 @@ tags:
- prometheus
- victoriametrics
- grafana
- alertmanager

View File

@ -1,11 +1,4 @@
---
alertmanager_user: alertmanager
alertmanager_version: 0.27.0
alertmanager_base_path: /opt/alertmanager
alertmanager_config_path: "{{ alertmanager_base_path }}/config"
alertmanager_config_file: "{{ alertmanager_config_path }}/alertmanager.yml"
alertmanager_data_path: "{{ alertmanager_base_path }}/data"
alertmanager_container_name: alertmanager
alertmanager_container_image_name: alertmanager
alertmanager_container_image_namespace: prometheus/
@ -22,7 +15,7 @@ alertmanager_container_image_reference: >-
alertmanager_container_image_repository + ':'
+ (alertmanager_container_image_tag | default('v' + alertmanager_version))
}}
alertmanager_container_image_source: pull
alertmanager_container_image_force_pull: "{{ alertmanager_container_image_tag is defined }}"
alertmanager_container_default_volumes:
@ -32,8 +25,5 @@ alertmanager_container_volumes: >-
{{ alertmanager_container_default_volumes
+ alertmanager_container_extra_volumes | default([]) }}
alertmanager_container_restart_policy: "unless-stopped"
alertmanager_config:
global: {}
route: {}
receivers: []
alertmanager_container_state: >-2
{{ (alertmanager_state == 'present') | ternary('started', 'absent') }}

View File

@ -0,0 +1,15 @@
---
alertmanager_user: alertmanager
alertmanager_version: 0.27.0
alertmanager_state: present
alertmanager_deployment_method: docker
alertmanager_base_path: /opt/alertmanager
alertmanager_config_path: "{{ alertmanager_base_path }}/config"
alertmanager_config_file: "{{ alertmanager_config_path }}/alertmanager.yml"
alertmanager_data_path: "{{ alertmanager_base_path }}/data"
alertmanager_config:
global: {}
route: {}
receivers: []

View File

@ -1,8 +1,8 @@
---
- name: Ensure alertmanager is restarted
community.docker.docker_container:
name: "{{ alertmanager_container_name }}"
state: started
state: "{{ alertmanager_container_state }}"
restart: true
listen: restart-alertmanager
when: alertmanager_deployment_method == 'docker'

View File

@ -0,0 +1,10 @@
---
allow_duplicates: true
dependencies: []
galaxy_info:
role_name: alertmanager
description: Deploy and configure prometheus alertmanager
galaxy_tags:
- prometheus
- alertmanager
- observability

View File

@ -0,0 +1,21 @@
---
- name: Ensure container image is {{ alertmanager_state }} on host
community.docker.docker_image:
name: "{{ alertmanager_container_image_reference }}"
state: "{{ alertmanager_state }}"
source: "{{ alertmanager_container_image_source }}"
force_source: "{{ alertmanager_container_image_force_pull | bool }}"
- name: Ensure container '{{ alertmanager_container_name }}' is {{ alertmanager_container_state }}
community.docker.docker_container:
name: "{{ alertmanager_container_name }}"
image: "{{ alertmanager_container_image_reference }}"
env: "{{ alertmanager_container_env | default(omit) }}"
user: "{{ alertmanager_user_info.uid | default(alertmanager_user) }}"
ports: "{{ alertmanager_container_ports | default(omit) }}"
volumes: "{{ alertmanager_container_volumes | default(omit) }}"
networks: "{{ alertmanager_container_networks | default(omit) }}"
purge_networks: "{{ alertmanager_container_purge_networks | default(omit) }}"
etc_hosts: "{{ alertmanager_container_etc_hosts | default(omit) }}"
restart_policy: "{{ alertmanager_container_restart_policy }}"
state: "{{ alertmanager_container_state }}"

View File

@ -1,16 +1,29 @@
---
- name: Ensure state is valid
ansible.builtin.fail:
msg: >-2
Invalid state '{{ alertmanager_state }}'! Valid
states are {{ alertmanager_states | join(', ') }}.
when: alertmanager_state not in alertmanager_states
- name: Ensure alertmanager user '{{ alertmanager_user }}' exists
- name: Ensure deployment method is valid
ansible.builtin.fail:
msg: >-2
Invalid deployment method {{ alertmanager_deployment_method }}!
Supported deployment methods are {{ alertmanager_deployment_methods | join(', ') }}.
when: alertmanager_deployment_method not in alertmanager_deployment_methods
- name: Ensure alertmanager user '{{ alertmanager_user }}' is {{ alertmanager_state }}
ansible.builtin.user:
name: "{{ alertmanager_user }}"
state: present
state: "{{ alertmanager_state }}"
system: true
register: alertmanager_user_info
- name: Ensure mounts are created
- name: Ensure mounts are {{ alertmanager_state }}
ansible.builtin.file:
dest: "{{ item.path }}"
state: directory
state: "{{ (alertmanager_state == 'present') | ternary('directory', 'absent') }}"
owner: "{{ item.owner | default(alertmanager_user_info.uid | default(alertmanager_user)) }}"
group: "{{ item.owner | default(alertmanager_user_info.group | default(alertmanager_user)) }}"
mode: "{{ item.mode | default('0755') }}"
@ -24,28 +37,12 @@
dest: "{{ alertmanager_config_file }}"
content: "{{ alertmanager_config | to_nice_yaml }}"
owner: "{{ alertmanager_user_info.uid | default(alertmanager_user) }}"
owner: "{{ alertmanager_user_info.uid | default(alertmanager_user) }}"
group: "{{ alertmanager_user_info.group | default(alertmanager_user) }}"
mode: "0640"
when: alertmanager_state == 'present'
notify:
- restart-alertmanager
- name: Ensure container image is present on host
community.docker.docker_image:
name: "{{ alertmanager_container_image_reference }}"
state: present
source: pull
force_source: "{{ alertmanager_container_image_force_pull | bool }}"
- name: Ensure container '{{ alertmanager_container_name }}' is running
community.docker.docker_container:
name: "{{ alertmanager_container_name }}"
image: "{{ alertmanager_container_image_reference }}"
env: "{{ alertmanager_container_env | default(omit) }}"
user: "{{ alertmanager_user_info.uid | default(alertmanager_user) }}"
ports: "{{ alertmanager_container_ports | default(omit) }}"
volumes: "{{ alertmanager_container_volumes | default(omit) }}"
networks: "{{ alertmanager_container_networks | default(omit) }}"
purge_networks: "{{ alertmanager_container_purge_networks | default(omit) }}"
etc_hosts: "{{ alertmanager_container_etc_hosts | default(omit) }}"
restart_policy: "{{ alertmanager_container_restart_policy }}"
state: started
- name: Deploy alertmanager using {{ alertmanager_deployment_method }}
ansible.builtin.include_tasks:
file: "deploy-{{ alertmanager_deployment_method }}.yml"

View File

@ -0,0 +1,6 @@
---
alertmanager_states:
- present
- absent
alertmanager_deployment_methods:
- docker