diff --git a/roles/alertmanager/defaults/main.yml b/roles/alertmanager/defaults/main/container.yml similarity index 75% rename from roles/alertmanager/defaults/main.yml rename to roles/alertmanager/defaults/main/container.yml index fc02ab2..f1fe2c5 100644 --- a/roles/alertmanager/defaults/main.yml +++ b/roles/alertmanager/defaults/main/container.yml @@ -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') }} diff --git a/roles/alertmanager/defaults/main/main.yml b/roles/alertmanager/defaults/main/main.yml new file mode 100644 index 0000000..043cd28 --- /dev/null +++ b/roles/alertmanager/defaults/main/main.yml @@ -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: [] diff --git a/roles/alertmanager/handlers/main.yml b/roles/alertmanager/handlers/main.yml index 9b75620..64eba6f 100644 --- a/roles/alertmanager/handlers/main.yml +++ b/roles/alertmanager/handlers/main.yml @@ -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' diff --git a/roles/alertmanager/meta/main.yml b/roles/alertmanager/meta/main.yml new file mode 100644 index 0000000..9a8f495 --- /dev/null +++ b/roles/alertmanager/meta/main.yml @@ -0,0 +1,10 @@ +--- +allow_duplicates: true +dependencies: [] +galaxy_info: + role_name: alertmanager + description: Deploy and configure prometheus alertmanager + galaxy_tags: + - prometheus + - alertmanager + - observability diff --git a/roles/alertmanager/tasks/deploy-docker.yml b/roles/alertmanager/tasks/deploy-docker.yml new file mode 100644 index 0000000..3ae6439 --- /dev/null +++ b/roles/alertmanager/tasks/deploy-docker.yml @@ -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 }}" diff --git a/roles/alertmanager/tasks/main.yml b/roles/alertmanager/tasks/main.yml index ca30c37..34c67df 100644 --- a/roles/alertmanager/tasks/main.yml +++ b/roles/alertmanager/tasks/main.yml @@ -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') }}" @@ -26,26 +39,10 @@ owner: "{{ alertmanager_user_info.uid | default(alertmanager_user) }}" owner: "{{ alertmanager_user_info.uid | 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" diff --git a/roles/alertmanager/vars/main.yml b/roles/alertmanager/vars/main.yml new file mode 100644 index 0000000..e6338c9 --- /dev/null +++ b/roles/alertmanager/vars/main.yml @@ -0,0 +1,6 @@ +--- +alertmanager_states: + - present + - absent +alertmanager_deployment_methods: + - docker