add alertmanager role
This commit is contained in:
parent
bceedf79f4
commit
8e899da042
52
roles/alertmanager/defaults/main.yaml
Normal file
52
roles/alertmanager/defaults/main.yaml
Normal file
@ -0,0 +1,52 @@
|
||||
---
|
||||
alertmanager_go_arch_map:
|
||||
i386: '386'
|
||||
x86_64: 'amd64'
|
||||
|
||||
alertmanager_go_arch: "{{ alertmanager_go_arch_map[ansible_architecture] | default('amd64') }}"
|
||||
|
||||
alertmanager_service_name: alertmanager.service
|
||||
alertmanager_service_enabled: true
|
||||
alertmanager_service_state: started
|
||||
|
||||
alertmanager_version_regex: ^alertmanager, version ([\d.]+)
|
||||
|
||||
alertmanager_github_project_url: https://github.com/prometheus/alertmanager
|
||||
alertmanager_release_file: "alertmanager-{{ alertmanager_version }}.{{ ansible_system | lower }}-{{ alertmanager_go_arch }}.tar.gz"
|
||||
alertmanager_release_url: "{{ alertmanager_github_project_url }}/releases/download/v{{ alertmanager_version }}/{{ alertmanager_release_file }}"
|
||||
alertmanager_checksum_url: "{{ alertmanager_github_project_url }}/releases/download/v{{ alertmanager_version }}/sha256sums.txt"
|
||||
alertmanager_download_path: "/tmp/{{ alertmanager_release_file }}"
|
||||
alertmanager_unarchive_dest_path: /tmp
|
||||
alertmanager_extracted_path: "{{ alertmanager_download_path | replace('.tar.gz', '') }}"
|
||||
|
||||
alertmanager_user: alertmanager
|
||||
alertmanager_user_state: present
|
||||
alertmanager_user_shell: /usr/sbin/nologin
|
||||
|
||||
alertmanager_group: alertmanager
|
||||
alertmanager_group_state: "{{ alertmanager_user_state | default('present') }}"
|
||||
|
||||
alertmanager_etc_path: /etc/alertmanager
|
||||
alertmanager_etc_owner: root
|
||||
alertmanager_etc_group: root
|
||||
alertmanager_etc_mode: "0755"
|
||||
|
||||
alertmanager_var_path: /var/lib/alertmanager
|
||||
alertmanager_var_owner: "{{ alertmanager_user }}"
|
||||
alertmanager_var_group: "{{ alertmanager_group }}"
|
||||
alertmanager_var_mode: "0755"
|
||||
|
||||
alertmanager_bin_path: /usr/local/bin
|
||||
|
||||
alertmanager_web_listen_address: 0.0.0.0:9093
|
||||
alertmanager_port: "{{ alertmanager_web_listen_address.split(':')[1] }}"
|
||||
alertmanager_web_external_url:
|
||||
alertmanager_web_route_prefix:
|
||||
alertmanager_cluster_advertise_address: 0.0.0.0:9093
|
||||
|
||||
alertmanager_config:
|
||||
route:
|
||||
routes:
|
||||
receiver: dummy
|
||||
receivers:
|
||||
- name: dummy
|
6
roles/alertmanager/handlers/main.yaml
Normal file
6
roles/alertmanager/handlers/main.yaml
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
- name: restart alertmanager
|
||||
systemd:
|
||||
name: alertmanager.service
|
||||
daemon_reload: true
|
||||
state: restarted
|
56
roles/alertmanager/tasks/configure.yaml
Normal file
56
roles/alertmanager/tasks/configure.yaml
Normal file
@ -0,0 +1,56 @@
|
||||
---
|
||||
- name: create group
|
||||
group:
|
||||
name: "{{ alertmanager_group }}"
|
||||
system: true
|
||||
state: "{{ alertmanager_group_state | default('present') }}"
|
||||
|
||||
- name: create user
|
||||
user:
|
||||
name: "{{ alertmanager_user }}"
|
||||
system: true
|
||||
shell: "{{ alertmanager_user_shell }}"
|
||||
group: "{{ alertmanager_group }}"
|
||||
createhome: false
|
||||
home: "{{ alertmanager_var_path }}"
|
||||
state: "{{ alertmanager_user_state | default('present') }}"
|
||||
|
||||
- name: create etc path
|
||||
file:
|
||||
path: "{{ alertmanager_etc_path }}"
|
||||
state: directory
|
||||
owner: "{{ alertmanager_etc_owner }}"
|
||||
group: "{{ alertmanager_etc_group }}"
|
||||
mode: "{{ alertmanager_etc_mode }}"
|
||||
|
||||
- name: create var path
|
||||
file:
|
||||
path: "{{ alertmanager_var_path }}"
|
||||
state: directory
|
||||
owner: "{{ alertmanager_var_owner }}"
|
||||
group: "{{ alertmanager_var_group }}"
|
||||
mode: "{{ alertmanager_var_mode }}"
|
||||
|
||||
- name: configure
|
||||
copy:
|
||||
dest: "{{ alertmanager_etc_path }}/alertmanager.yaml"
|
||||
content: "{{ (alertmanager_config | default({})) | to_nice_yaml }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0444
|
||||
notify: restart alertmanager
|
||||
|
||||
- name: configure systemd template
|
||||
template:
|
||||
src: alertmanager.service.j2
|
||||
dest: /etc/systemd/system/alertmanager.service
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0444
|
||||
notify: restart alertmanager
|
||||
|
||||
- name: manage service
|
||||
service:
|
||||
name: "{{ alertmanager_service_name }}"
|
||||
enabled: "{{ alertmanager_service_enabled }}"
|
||||
state: "{{ alertmanager_service_state }}"
|
0
roles/alertmanager/tasks/default.yaml
Normal file
0
roles/alertmanager/tasks/default.yaml
Normal file
32
roles/alertmanager/tasks/install.yaml
Normal file
32
roles/alertmanager/tasks/install.yaml
Normal file
@ -0,0 +1,32 @@
|
||||
---
|
||||
- block:
|
||||
- name: download tar
|
||||
get_url:
|
||||
url: "{{ alertmanager_release_url }}"
|
||||
dest: "{{ alertmanager_download_path }}"
|
||||
checksum: "{{ alertmanager_checksum }}"
|
||||
register: dl
|
||||
until: dl is success
|
||||
retries: 5
|
||||
delay: 10
|
||||
|
||||
- name: extract tar
|
||||
unarchive:
|
||||
src: "{{ alertmanager_download_path }}"
|
||||
dest: "{{ alertmanager_unarchive_dest_path }}"
|
||||
creates: "{{ alertmanager_extracted_path }}/alertmanager"
|
||||
remote_src: true
|
||||
|
||||
- name: install binaries
|
||||
copy:
|
||||
src: "{{ alertmanager_extracted_path }}/{{ item }}"
|
||||
dest: "{{ alertmanager_bin_path }}/{{ item }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0755
|
||||
remote_src: true
|
||||
loop:
|
||||
- alertmanager
|
||||
- amtool
|
||||
notify: restart alertmanager
|
||||
when: alertmanager_version != alertmanager_local_version
|
30
roles/alertmanager/tasks/main.yaml
Normal file
30
roles/alertmanager/tasks/main.yaml
Normal file
@ -0,0 +1,30 @@
|
||||
---
|
||||
- name: gather os specific variables
|
||||
include_vars: "{{ lookup('first_found', possible_files) }}"
|
||||
vars:
|
||||
possible_files:
|
||||
files:
|
||||
- "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml"
|
||||
- "{{ ansible_distribution }}.yaml"
|
||||
- "{{ ansible_os_family }}.yaml"
|
||||
- "default.yaml"
|
||||
paths:
|
||||
- vars
|
||||
|
||||
- name: include os specific tasks
|
||||
include_tasks: "{{ lookup('first_found', possible_files) }}"
|
||||
vars:
|
||||
possible_files:
|
||||
files:
|
||||
- "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml"
|
||||
- "{{ ansible_distribution }}.yaml"
|
||||
- "{{ ansible_os_family }}.yaml"
|
||||
- "default.yaml"
|
||||
paths:
|
||||
- tasks
|
||||
|
||||
- include: pre.yaml
|
||||
|
||||
- include: install.yaml
|
||||
|
||||
- include: configure.yaml
|
50
roles/alertmanager/tasks/pre.yaml
Normal file
50
roles/alertmanager/tasks/pre.yaml
Normal file
@ -0,0 +1,50 @@
|
||||
---
|
||||
- name: determine if installed
|
||||
stat:
|
||||
path: "{{ alertmanager_bin_path }}/alertmanager"
|
||||
register: st
|
||||
|
||||
- name: set alertmanager_installed
|
||||
set_fact:
|
||||
alertmanager_installed: "{{ st.stat.exists | bool }}"
|
||||
|
||||
- block:
|
||||
- name: determine latest version
|
||||
uri:
|
||||
url: https://api.github.com/repos/prometheus/alertmanager/releases/latest
|
||||
return_content: true
|
||||
body_format: json
|
||||
register: _latest_version
|
||||
until: _latest_version.status == 200
|
||||
retries: 3
|
||||
|
||||
- name: set alertmanager_version
|
||||
set_fact:
|
||||
alertmanager_version: "{{ _latest_version.json['tag_name'] | regex_replace('^v', '') }}"
|
||||
|
||||
- block:
|
||||
- name: determine installed version
|
||||
command: "{{ alertmanager_bin_path }}/alertmanager --version"
|
||||
register: _installed_version_string
|
||||
changed_when: false
|
||||
|
||||
- name: set alertmanager_local_version
|
||||
set_fact:
|
||||
alertmanager_local_version: "{{ _installed_version_string.stdout | regex_search(alertmanager_version_regex, '\\1') | first }}"
|
||||
when: alertmanager_installed
|
||||
|
||||
- name: set alertmanager_local_version to 0
|
||||
set_fact:
|
||||
alertmanager_local_version: "0"
|
||||
when: not alertmanager_installed
|
||||
|
||||
- block:
|
||||
- name: get checksums
|
||||
set_fact:
|
||||
_checksums: "{{ lookup('url', alertmanager_checksum_url, wantlist=True) }}"
|
||||
|
||||
- name: set alertmanager_checksum
|
||||
set_fact:
|
||||
alertmanager_checksum: "sha256:{{ item.split(' ') | first }}"
|
||||
loop: "{{ _checksums }}"
|
||||
when: "alertmanager_release_file in item"
|
26
roles/alertmanager/templates/alertmanager.service.j2
Normal file
26
roles/alertmanager/templates/alertmanager.service.j2
Normal file
@ -0,0 +1,26 @@
|
||||
{{ ansible_managed | comment }}
|
||||
|
||||
[Unit]
|
||||
Description=Alertmanager
|
||||
After=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User={{ alertmanager_user }}
|
||||
Group={{ alertmanager_group }}
|
||||
ExecStart={{ alertmanager_bin_path }}/alertmanager \
|
||||
--config.file={{ alertmanager_etc_path }}/alertmanager.yaml \
|
||||
--storage.path={{ alertmanager_var_path }} \
|
||||
--cluster.advertise-address={{ alertmanager_cluster_advertise_address }} \
|
||||
{% if alertmanager_web_external_url %}
|
||||
--web.external-url={{ alertmanager_web_external_url }} \
|
||||
{% endif %}
|
||||
{% if alertmanager_web_route_prefix %}
|
||||
--web.route-prefix={{ alertmanager_web_route_prefix }} \
|
||||
{% endif %}
|
||||
{% if alertmanager_web_listen_address %}
|
||||
--web.listen-address={{ alertmanager_web_listen_address }} \
|
||||
{% endif %}
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
0
roles/alertmanager/vars/default.yaml
Normal file
0
roles/alertmanager/vars/default.yaml
Normal file
Loading…
Reference in New Issue
Block a user