refactor(alertmanager): delegate common tasks to _common role

Signed-off-by: gardar <gardar@users.noreply.github.com>
This commit is contained in:
gardar 2024-10-15 17:01:13 +00:00
parent 8734f607db
commit d05e3c23b5
No known key found for this signature in database
GPG key ID: 75FAE37CBA8C13C2
10 changed files with 125 additions and 232 deletions

View file

@ -1,10 +1,8 @@
---
alertmanager_version: 0.27.0
alertmanager_binary_local_dir: ''
alertmanager_binary_url: "https://github.com/{{ _alertmanager_repo }}/releases/download/v{{ alertmanager_version }}/\
alertmanager-{{ alertmanager_version }}.linux-{{ go_arch }}.tar.gz"
alertmanager-{{ alertmanager_version }}.{{ ansible_system | lower }}-{{ _alertmanager_go_ansible_arch }}.tar.gz"
alertmanager_checksums_url: "https://github.com/{{ _alertmanager_repo }}/releases/download/v{{ alertmanager_version }}/sha256sums.txt"
alertmanager_skip_install: false
alertmanager_config_dir: /etc/alertmanager
alertmanager_db_dir: /var/lib/alertmanager
@ -135,5 +133,10 @@ alertmanager_amtool_config_alertmanager_url: "{{ alertmanager_web_external_url }
# Extended output of `amtool` commands, use '' for less verbosity
alertmanager_amtool_config_output: 'extended'
alertmanager_binary_install_dir: '/usr/local/bin'
# Local path to stash the archive and its extraction
alertmanager_archive_path: /tmp
alertmanager_local_cache_path: "/tmp/alertmanager-{{ ansible_system | lower }}-{{ _alertmanager_go_ansible_arch }}/{{ alertmanager_version }}"
alertmanager_system_user: alertmanager
alertmanager_system_group: "{{ alertmanager_system_user }}"

View file

@ -11,18 +11,9 @@ argument_specs:
alertmanager_version:
description: "Alertmanager package version. Also accepts `latest` as parameter."
default: 0.27.0
alertmanager_skip_install:
description: "Alertmanager installation tasks gets skipped when set to true."
type: bool
default: false
alertmanager_binary_local_dir:
description:
- "Allows to use local packages instead of ones distributed on github."
- "As parameter it takes a directory where C(alertmanager) AND C(amtool) binaries are stored on host on which ansible is ran."
- "This overrides I(alertmanager_version) parameter"
alertmanager_binary_url:
description: "URL of the alertmanager binaries .tar.gz file"
default: "https://github.com/{{ _alertmanager_repo }}/releases/download/v{{ alertmanager_version }}/alertmanager-{{ alertmanager_version }}.linux-{{ go_arch }}.tar.gz"
default: "https://github.com/{{ _alertmanager_repo }}/releases/download/v{{ alertmanager_version }}/alertmanager-{{ alertmanager_version }}.{{ ansible_system | lower }}-{{ _alertmanager_go_ansible_arch }}.tar.gz"
alertmanager_checksums_url:
description: "URL of the alertmanager checksums file"
default: "https://github.com/{{ _alertmanager_repo }}/releases/download/v{{ alertmanager_version }}/sha256sums.txt"
@ -32,6 +23,11 @@ argument_specs:
alertmanager_web_external_url:
description: "External address on which alertmanager is available. Useful when behind reverse proxy. Ex. example.org/alertmanager"
default: "http://localhost:9093/"
alertmanager_binary_install_dir:
description:
- "I(Advanced)"
- "Directory to install binaries"
default: "/usr/local/bin"
alertmanager_config_dir:
description: "Path to directory with alertmanager configuration"
default: "/etc/alertmanager"
@ -110,6 +106,16 @@ argument_specs:
alertmanager_amtool_config_output:
description: 'Extended output, use C("") for simple output.'
default: "extended"
alertmanager_archive_path:
alertmanager_local_cache_path:
description: 'Local path to stash the archive and its extraction'
default: "/tmp"
default: "/tmp/alertmanager-{{ ansible_system | lower }}-{{ _alertmanager_go_ansible_arch }}/{{ alertmanager_version }}"
alertmanager_system_user:
description:
- "I(Advanced)"
- "alertmanager system user"
default: alertmanager
alertmanager_system_group:
description:
- "I(Advanced)"
- "System group for alertmanager"
default: alertmanager

View file

@ -5,7 +5,8 @@ provisioner:
inventory:
group_vars:
all:
alertmanager_binary_local_dir: '/tmp/alertmanager-linux-amd64'
alertmanager_version: 0.25.0
alertmanager_local_cache_path: "/tmp/alertmanager-linux-amd64/{{ alertmanager_version }}"
alertmanager_config_dir: /opt/am/etc
alertmanager_db_dir: /opt/am/lib
alertmanager_web_listen_address:
@ -35,6 +36,5 @@ provisioner:
peers:
- "127.0.0.1:6783"
- "alertmanager.demo.do.prometheus.io:6783"
alertmanager_version: 0.25.0
alertmanager_binary_url: "https://github.com/prometheus/alertmanager/releases/download/v{{ alertmanager_version\
\ }}/alertmanager-{{ alertmanager_version }}.linux-amd64.tar.gz"

View file

@ -1,44 +1,79 @@
---
- name: "Common configure"
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: configure.yml
vars:
_common_system_user: "{{ alertmanager_system_user }}"
_common_system_group: "{{ alertmanager_system_group }}"
_common_config_dir: "{{ alertmanager_config_dir }}"
tags:
- alertmanager
- configure
- alertmanager_configure
- name: Create alertmanager directories
ansible.builtin.file:
path: "{{ item }}"
state: directory
owner: "{{ alertmanager_system_user }}"
group: "{{ alertmanager_system_group }}"
mode: 0755
loop:
- "{{ alertmanager_config_dir }}/templates"
- "{{ alertmanager_db_dir }}"
- "{{ _alertmanager_amtool_config_dir }}"
become: true
tags:
- alertmanager
- configure
- alertmanager_configure
- name: Copy amtool config
ansible.builtin.template:
force: true
src: "{{ alertmanager_amtool_config_file }}"
dest: "{{ _alertmanager_amtool_config_dir }}/config.yml"
owner: alertmanager
group: alertmanager
owner: "{{ alertmanager_system_user }}"
group: "{{ alertmanager_system_group }}"
mode: 0644
become: true
tags:
- alertmanager
- configure
- alertmanager_configure
- name: Copy alertmanager config
ansible.builtin.template:
force: true
src: "{{ alertmanager_config_file }}"
dest: "{{ alertmanager_config_dir }}/alertmanager.yml"
owner: alertmanager
group: alertmanager
owner: "{{ alertmanager_system_user }}"
group: "{{ alertmanager_system_group }}"
mode: 0644
validate: "{{ _alertmanager_binary_install_dir }}/amtool check-config %s"
validate: "{{ alertmanager_binary_install_dir }}/amtool check-config %s"
no_log: "{{ false if (lookup('env', 'CI')) or (lookup('env', 'MOLECULE_PROVISIONER_NAME')) else true }}"
become: true
notify:
- restart alertmanager
- name: Create systemd service unit
ansible.builtin.template:
src: alertmanager.service.j2
dest: /etc/systemd/system/alertmanager.service
owner: root
group: root
mode: 0644
notify:
- restart alertmanager
tags:
- alertmanager
- configure
- alertmanager_configure
- name: Copy alertmanager template files
ansible.builtin.copy:
src: "{{ item }}"
dest: "{{ alertmanager_config_dir }}/templates/"
force: true
owner: alertmanager
group: alertmanager
owner: "{{ alertmanager_system_user }}"
group: "{{ alertmanager_system_group }}"
mode: 0644
with_fileglob: "{{ alertmanager_template_files }}"
become: true
notify:
- restart alertmanager
tags:
- alertmanager
- configure
- alertmanager_configure

View file

@ -1,87 +0,0 @@
---
- name: Create alertmanager system group
ansible.builtin.group:
name: alertmanager
system: true
state: present
- name: Create alertmanager system user
ansible.builtin.user:
name: alertmanager
system: true
shell: "/usr/sbin/nologin"
group: alertmanager
createhome: false
- name: Create alertmanager directories
ansible.builtin.file:
path: "{{ item }}"
state: directory
owner: alertmanager
group: alertmanager
mode: 0755
with_items:
- "{{ alertmanager_config_dir }}"
- "{{ alertmanager_config_dir }}/templates"
- "{{ alertmanager_db_dir }}"
- "{{ _alertmanager_amtool_config_dir }}"
- name: Get binary
when:
- alertmanager_binary_local_dir | length == 0
- not alertmanager_skip_install
block:
- name: Download alertmanager binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ alertmanager_binary_url }}"
dest: "{{ alertmanager_archive_path }}/alertmanager-{{ alertmanager_version }}.linux-{{ go_arch }}.tar.gz"
checksum: "sha256:{{ __alertmanager_checksum }}"
mode: 0644
register: _download_archive
until: _download_archive is succeeded
retries: 5
delay: 2
# run_once: true # <-- this can't be set due to multi-arch support
delegate_to: localhost
check_mode: false
- name: Unpack alertmanager binaries
become: false
ansible.builtin.unarchive:
src: "{{ alertmanager_archive_path }}/alertmanager-{{ alertmanager_version }}.linux-{{ go_arch }}.tar.gz"
dest: "{{ alertmanager_archive_path }}"
mode: 0755
creates: "{{ alertmanager_archive_path }}/alertmanager-{{ alertmanager_version }}.linux-{{ go_arch }}/alertmanager"
delegate_to: localhost
check_mode: false
- name: Propagate official alertmanager and amtool binaries
ansible.builtin.copy:
src: "{{ alertmanager_archive_path }}/alertmanager-{{ alertmanager_version }}.linux-{{ go_arch }}/{{ item }}"
dest: "{{ _alertmanager_binary_install_dir }}/{{ item }}"
mode: 0755
owner: root
group: root
with_items:
- alertmanager
- amtool
notify:
- restart alertmanager
- name: Propagate locally distributed alertmanager and amtool binaries
ansible.builtin.copy:
src: "{{ alertmanager_binary_local_dir }}/{{ item }}"
dest: "{{ _alertmanager_binary_install_dir }}/{{ item }}"
mode: 0755
owner: root
group: root
with_items:
- alertmanager
- amtool
when:
- alertmanager_binary_local_dir | length > 0
- not alertmanager_skip_install
notify:
- restart alertmanager

View file

@ -2,56 +2,48 @@
- name: Preflight
ansible.builtin.include_tasks:
file: preflight.yml
apply:
tags:
- alertmanager_install
- alertmanager_configure
- alertmanager_run
tags:
- alertmanager
- install
- configure
- alertmanager_install
- alertmanager_configure
- alertmanager_run
- name: Install
ansible.builtin.include_tasks:
file: install.yml
apply:
become: true
tags:
- alertmanager_install
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: install.yml
vars:
_common_local_cache_path: "{{ alertmanager_local_cache_path }}"
_common_binaries: "{{ _alertmanager_binaries }}"
_common_binary_install_dir: "{{ alertmanager_binary_install_dir }}"
_common_binary_url: "{{ alertmanager_binary_url }}"
_common_checksums_url: "{{ alertmanager_checksums_url }}"
_common_system_group: "{{ alertmanager_system_group }}"
_common_system_user: "{{ alertmanager_system_user }}"
_common_config_dir: "{{ alertmanager_config_dir }}"
_common_binary_unarchive_opts: ['--strip-components=1']
tags:
- alertmanager
- install
- alertmanager_install
- name: SELinux
ansible.builtin.include_tasks:
file: selinux.yml
apply:
become: true
tags:
- alertmanager_configure
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: selinux.yml
vars:
_common_selinux_port: "{{ alertmanager_web_listen_address | urlsplit('port') }}"
when: ansible_selinux.status == "enabled"
tags:
- alertmanager
- configure
- alertmanager_configure
- name: Configure
ansible.builtin.include_tasks:
file: configure.yml
apply:
become: true
tags:
- alertmanager_configure
tags:
- alertmanager
- configure
- alertmanager_configure
- name: Ensure alertmanager service is started and enabled
become: true
ansible.builtin.systemd:
daemon_reload: true
name: alertmanager
state: started
enabled: true
tags:
- alertmanager_run
- name: Flush alertmangaer handlers after run.
ansible.builtin.meta: flush_handlers

View file

@ -1,24 +1,8 @@
---
- name: Assert usage of systemd as an init system
ansible.builtin.assert:
that: ansible_service_mgr == 'systemd'
msg: "This module only works with systemd"
- name: Install package fact dependencies
become: true
ansible.builtin.package:
name: "{{ _pkg_fact_req }}"
state: present
when: (_pkg_fact_req)
vars:
_pkg_fact_req: "{% if (ansible_pkg_mgr == 'apt') %}\
{{ ('python-apt' if ansible_python_version is version('3', '<') else 'python3-apt') }}
{% else %}\
{% endif %}"
- name: Gather package facts
ansible.builtin.package_facts:
when: "not 'packages' in ansible_facts"
- name: Common preflight
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: preflight.yml
- name: Assert that used version supports listen address type
ansible.builtin.assert:
@ -50,28 +34,12 @@
retries: 10
when:
- alertmanager_version == "latest"
- alertmanager_binary_local_dir | length == 0
- not alertmanager_skip_install
- name: Get alertmanager binary checksum
when:
- alertmanager_binary_local_dir | length == 0
- not alertmanager_skip_install
block:
- name: "Get checksum list"
ansible.builtin.set_fact:
__alertmanager_checksums: "{{ lookup('url', alertmanager_checksums_url, headers=_github_api_headers, wantlist=True) | list }}"
run_once: true
until: __alertmanager_checksums is search('linux-' + go_arch + '.tar.gz')
retries: 10
- name: "Get checksum for {{ go_arch }}"
ansible.builtin.set_fact:
__alertmanager_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __alertmanager_checksums }}"
when:
- "('linux-' + go_arch + '.tar.gz') in item"
tags:
- alertmanager
- install
- alertmanager_install
- download
- alertmanager_download
- name: Fail when extra config flags are duplicating ansible variables
ansible.builtin.fail:

View file

@ -1,23 +0,0 @@
---
- name: Install selinux python packages [RedHat]
ansible.builtin.package:
name: "{{ ['libselinux-python', 'policycoreutils-python']
if ansible_python_version is version('3', '<') else
['python3-libselinux', 'python3-policycoreutils'] }}"
state: present
register: _install_selinux_packages
until: _install_selinux_packages is success
retries: 5
delay: 2
when: ansible_os_family | lower == "redhat"
- name: Install selinux python packages [clearlinux]
ansible.builtin.package:
name: sysadmin-basic
state: present
register: _install_selinux_packages
until: _install_selinux_packages is success
retries: 5
delay: 2
when:
- ansible_distribution | lower == "clearlinux"

View file

@ -21,7 +21,7 @@ PIDFile=/var/run/alertmanager.pid
User=alertmanager
Group=alertmanager
ExecReload=/bin/kill -HUP $MAINPID
ExecStart={{ _alertmanager_binary_install_dir }}/alertmanager \
ExecStart={{ alertmanager_binary_install_dir }}/alertmanager \
{% for option, value in (alertmanager_cluster.items() | sort) %}
{% if option == "peers" %}
{% for peer in value %}

View file

@ -1,15 +1,14 @@
---
go_arch_map:
i386: '386'
x86_64: 'amd64'
aarch64: 'arm64'
armv7l: 'armv7'
armv6l: 'armv6'
go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}"
_alertmanager_binary_install_dir: '/usr/local/bin'
_alertmanager_go_ansible_arch: "{{ {'i386': '386',
'x86_64': 'amd64',
'aarch64': 'arm64',
'armv7l': 'armv7',
'armv6l': 'armv6'}.get(ansible_architecture, ansible_architecture) }}"
# The expected location of the amtool configuration file
_alertmanager_amtool_config_dir: '/etc/amtool'
_alertmanager_repo: "prometheus/alertmanager"
_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}"
_alertmanager_binaries:
- alertmanager
- amtool