commit ffe27507d02c0a88d670509d343f0442914a9f59 Author: Robert Kaussow Date: Thu Jun 3 00:01:24 2021 +0200 initial commit diff --git a/.drone.jsonnet b/.drone.jsonnet new file mode 100644 index 0000000..7eb7c0b --- /dev/null +++ b/.drone.jsonnet @@ -0,0 +1,137 @@ +local PipelineLinting = { + kind: 'pipeline', + name: 'linting', + platform: { + os: 'linux', + arch: 'amd64', + }, + steps: [ + { + name: 'ansible-later', + image: 'thegeeklab/ansible-later', + commands: [ + 'ansible-later', + ], + }, + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'], + }, +}; + +local PipelineDeployment(scenario='centos7') = { + kind: 'pipeline', + name: 'testing-' + scenario, + platform: { + os: 'linux', + arch: 'amd64', + }, + concurrency: { + limit: 1, + }, + workspace: { + base: '/drone/src', + path: '${DRONE_REPO_NAME}', + }, + steps: [ + { + name: 'ansible-molecule', + image: 'thegeeklab/molecule:3', + environment: { + HCLOUD_TOKEN: { from_secret: 'hcloud_token' }, + }, + commands: [ + 'molecule test -s ' + scenario, + ], + }, + ], + depends_on: [ + 'linting', + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**'], + }, +}; + +local PipelineDocumentation = { + kind: 'pipeline', + name: 'documentation', + platform: { + os: 'linux', + arch: 'amd64', + }, + steps: [ + { + name: 'generate', + image: 'thegeeklab/ansible-doctor', + environment: { + ANSIBLE_DOCTOR_LOG_LEVEL: 'INFO', + ANSIBLE_DOCTOR_FORCE_OVERWRITE: true, + ANSIBLE_DOCTOR_EXCLUDE_FILES: 'molecule/', + ANSIBLE_DOCTOR_TEMPLATE: 'hugo-book', + ANSIBLE_DOCTOR_ROLE_NAME: '${DRONE_REPO_NAME#*.}', + ANSIBLE_DOCTOR_OUTPUT_DIR: '_docs/', + }, + }, + { + name: 'publish', + image: 'plugins/gh-pages', + settings: { + remote_url: 'https://gitea.rknet.org/ansible/${DRONE_REPO_NAME}', + netrc_machine: 'gitea.rknet.org', + username: { from_secret: 'gitea_username' }, + password: { from_secret: 'gitea_token' }, + pages_directory: '_docs/', + target_branch: 'docs', + }, + when: { + ref: ['refs/heads/master'], + }, + }, + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'], + }, + depends_on: [ + 'testing-centos7', + ], +}; + +local PipelineNotification = { + kind: 'pipeline', + name: 'notification', + platform: { + os: 'linux', + arch: 'amd64', + }, + clone: { + disable: true, + }, + steps: [ + { + name: 'matrix', + image: 'plugins/matrix', + settings: { + homeserver: { from_secret: 'matrix_homeserver' }, + roomid: { from_secret: 'matrix_roomid' }, + template: 'Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}', + username: { from_secret: 'matrix_username' }, + password: { from_secret: 'matrix_password' }, + }, + }, + ], + depends_on: [ + 'documentation', + ], + trigger: { + status: ['success', 'failure'], + ref: ['refs/heads/master', 'refs/tags/**'], + }, +}; + +[ + PipelineLinting, + PipelineDeployment(scenario='centos7'), + PipelineDocumentation, + PipelineNotification, +] diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..501d4d3 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,136 @@ +--- +kind: pipeline +name: linting + +platform: + os: linux + arch: amd64 + +steps: +- name: ansible-later + image: thegeeklab/ansible-later + commands: + - ansible-later + +trigger: + ref: + - refs/heads/master + - refs/tags/** + - refs/pull/** + +--- +kind: pipeline +name: testing-centos7 + +platform: + os: linux + arch: amd64 + +concurrency: + limit: 1 + +workspace: + base: /drone/src + path: ${DRONE_REPO_NAME} + +steps: +- name: ansible-molecule + image: thegeeklab/molecule:3 + commands: + - molecule test -s centos7 + environment: + HCLOUD_TOKEN: + from_secret: hcloud_token + +trigger: + ref: + - refs/heads/master + - refs/tags/** + +depends_on: +- linting + +--- +kind: pipeline +name: documentation + +platform: + os: linux + arch: amd64 + +steps: +- name: generate + image: thegeeklab/ansible-doctor + environment: + ANSIBLE_DOCTOR_EXCLUDE_FILES: molecule/ + ANSIBLE_DOCTOR_FORCE_OVERWRITE: true + ANSIBLE_DOCTOR_LOG_LEVEL: INFO + ANSIBLE_DOCTOR_OUTPUT_DIR: _docs/ + ANSIBLE_DOCTOR_ROLE_NAME: ${DRONE_REPO_NAME#*.} + ANSIBLE_DOCTOR_TEMPLATE: hugo-book + +- name: publish + image: plugins/gh-pages + settings: + netrc_machine: gitea.rknet.org + pages_directory: _docs/ + password: + from_secret: gitea_token + remote_url: https://gitea.rknet.org/ansible/${DRONE_REPO_NAME} + target_branch: docs + username: + from_secret: gitea_username + when: + ref: + - refs/heads/master + +trigger: + ref: + - refs/heads/master + - refs/tags/** + - refs/pull/** + +depends_on: +- testing-centos7 + +--- +kind: pipeline +name: notification + +platform: + os: linux + arch: amd64 + +clone: + disable: true + +steps: +- name: matrix + image: plugins/matrix + settings: + homeserver: + from_secret: matrix_homeserver + password: + from_secret: matrix_password + roomid: + from_secret: matrix_roomid + template: "Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}" + username: + from_secret: matrix_username + +trigger: + ref: + - refs/heads/master + - refs/tags/** + status: + - success + - failure + +depends_on: +- documentation + +--- +kind: signature +hmac: 52e0e958af6f1bc31666f2e6d9667ef5028f82ed1255cc2f5ef1bedc99142d02 + +... diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..b41e1fc --- /dev/null +++ b/.flake8 @@ -0,0 +1,5 @@ +[flake8] +ignore = D101, D102, D103, D105, D107, E402, W503 +max-line-length = 99 +inline-quotes = double +exclude = .git,.tox,__pycache__,build,dist,tests,*.pyc,*.egg-info,.cache,.eggs,env* diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d97b7cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# ---> Ansible +*.retry +plugins +library + +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + diff --git a/.later.yml b/.later.yml new file mode 100644 index 0000000..0efe5d5 --- /dev/null +++ b/.later.yml @@ -0,0 +1,19 @@ +--- +ansible: + custom_modules: + - iptables_raw + - openssl_pkcs12 + - proxmox_kvm + - ucr + - corenetworks_dns + - corenetworks_token + +rules: + exclude_files: + - molecule/ + - "LICENSE*" + - "**/*.md" + - "**/*.ini" + + exclude_filter: + - LINT0009 diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..ef05acb --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +.drone* diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8e54586 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Robert Kaussow + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0b9222f --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# xoxys.alertmanager + +[![Build Status](https://img.shields.io/drone/build/ansible/xoxys.alertmanager?logo=drone&server=https%3A%2F%2Fdrone.rknet.org)](https://drone.rknet.org/ansible/xoxys.alertmanager) +[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?label=license)](LICENSE) + +Setup Prometheus [Alertmanager](https://prometheus.io/docs/alerting/latest/alertmanager/) service. You can find the full documentation at [https://galaxy.geekdocs.de](https://galaxy.geekdocs.de/roles/cloud/alertmanager/). + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..ac4177a --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,94 @@ +--- +alertmanager_version: 0.22.0 +alertmanager_user: "alertmanager_adm" +alertmanager_user_home: "/home/{{ alertmanager_user }}" +alertmanager_group: "{{ alertmanager_user }}" +alertmanager_extra_groups: [] + +alertmanager_packages: [] + +alertmanager_base_dir: "/opt/alertmanager" +alertmanager_config_dir: "{{ alertmanager_base_dir }}/conf" +alertmanager_templates_dir: "{{ alertmanager_config_dir }}/templates" +alertmanager_data_dir: "{{ alertmanager_base_dir }}/data" +alertmanager_read_only_dirs: [] + +alertmanager_web_bind_ip: 127.0.0.1 +alertmanager_web_bind_port: 61001 +alertmanager_web_external_url: "http://localhost:61001/" + +alertmanager_web_tls_enabled: False +alertmanager_web_tls_cert_path: "{{ alertmanager_base_dir }}/tls/certs/mycert.pem" +alertmanager_web_tls_key_path: "{{ alertmanager_base_dir }}/tls/private/mykey.pem" +alertmanager_web_tls_cert_source: mycert.pem +alertmanager_web_tls_key_source: mykey.pem + +# @var alertmanager_web_http_server:description: See official [documentation](https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md). +# @var alertmanager_web_http_server: $ "_unset_" + +# @var alertmanager_web_basic_auth_users:description: See official [documentation](https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md). +# @var alertmanager_web_basic_auth_users: $ "_unset_" + +alertmanager_log_level: error + +alertmanager_template_files: + - alertmanager/templates/*.tmpl + +alertmanager_resolve_timeout: 3m + +alertmanager_config_flags_extra: [] +# @var alertmanager_config_flags_extra:example: > +# alertmanager_config_flags_extra: +# - name: data.retention +# value: 10 +# @end + +# @var alertmanager_smtp_host:description: Mail server used to send alerts. Need to be defined to enable Mail notifications. +# @var alertmanager_smtp_host: $ "_unset_" +alertmanager_smtp_port: 587 +alertmanager_smtp_from: alerts@example.com +alertmanager_smtp_auth_username: mail +alertmanager_smtp_auth_password: secure +alertmanager_smtp_require_tls: "True" + +alertmanager_route_group_by: + - alertname + - service + +alertmanager_route_group_wait: 30s +alertmanager_route_group_interval: 5m +alertmanager_route_repeat_interval: 3h + +# @var alertmanager_route_default_receiver:default: $ "_unset_" +# @var alertmanager_route_default_receiver:example: $ "slack" + +alertmanager_route_routes: [] + +alertmanager_receivers: [] +# @var alertmanager_receivers:example: > +# alertmanager_receivers: +# - name: slack +# slack_configs: +# - send_resolved: true +# channel: '#alerts' +# @end + +alertmanager_inhibit_rules: [] +# @ alertmanager_inhibit_rules:example: > +# alertmanager_inhibit_rules: +# - target_match: +# label: value +# source_match: +# label: value +# equal: ['dc', 'rack'] +# - target_match_re: +# label: value1|value2 +# source_match_re: +# label: value3|value5 +# @end + +# @var alertmanager_amtool_config_alertmanager_url:description: Location (URL) of the alertmanager. +alertmanager_amtool_config_alertmanager_url: "{{ alertmanager_web_external_url }}" + +# @var alertmanager_amtool_config_output:description: Extended output of `amtool` commands, use '' for less verbosity. +alertmanager_amtool_config_output: "extended" diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..70c2972 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,10 @@ +--- +- name: Restart Alertmanager Service + systemd: + name: alertmanager + state: restarted + daemon_reload: yes + enabled: yes + listen: __alertmanager_restart + become: True + become_user: root diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..6eb4948 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,23 @@ +# Standards: 0.2 +--- +galaxy_info: + # @meta author:value: [Robert Kaussow](https://gitea.rknet.org/xoxys) + author: "Robert Kaussow " + namespace: xoxys + role_name: alertmanager + # @meta description: > + # [![Source Code](https://img.shields.io/badge/gitea-source%20code-blue?logo=gitea&logoColor=white)](https://gitea.rknet.org/ansible/xoxys.alertmanager) + # [![Build Status](https://img.shields.io/drone/build/ansible/xoxys.alertmanager?logo=drone&server=https%3A%2F%2Fdrone.rknet.org)](https://drone.rknet.org/ansible/xoxys.alertmanager) + # [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?label=license)](https://gitea.rknet.org/ansible/xoxys.alertmanager/src/branch/master/LICENSE) + # + # Setup Prometheus [Alertmanager](https://prometheus.io/docs/alerting/latest/alertmanager/) service. + # @end + description: Setup Prometheus Alertmanager service + license: MIT + min_ansible_version: 2.10 + platforms: + - name: EL + versions: + - 7 + galaxy_tags: [] +dependencies: [] diff --git a/molecule/centos7/converge.yml b/molecule/centos7/converge.yml new file mode 100644 index 0000000..3d62967 --- /dev/null +++ b/molecule/centos7/converge.yml @@ -0,0 +1,9 @@ +--- +- name: Converge + hosts: all + vars: + alertmanager_route_default_receiver: dummy + alertmanager_receivers: + - name: "{{ alertmanager_route_default_receiver }}" + roles: + - role: xoxys.alertmanager diff --git a/molecule/centos7/create.yml b/molecule/centos7/create.yml new file mode 100644 index 0000000..719600d --- /dev/null +++ b/molecule/centos7/create.yml @@ -0,0 +1,120 @@ +--- +- name: Create + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + vars: + ssh_port: 22 + ssh_user: root + ssh_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key" + tasks: + - name: Create SSH key + user: + name: "{{ lookup('env', 'USER') }}" + generate_ssh_key: true + ssh_key_file: "{{ ssh_path }}" + force: true + register: generated_ssh_key + + - name: Register the SSH key name + set_fact: + ssh_key_name: "molecule-generated-{{ 12345 | random | to_uuid }}" + + - name: Register SSH key for test instance(s) + hcloud_ssh_key: + name: "{{ ssh_key_name }}" + public_key: "{{ generated_ssh_key.ssh_public_key }}" + state: present + + - name: Create molecule instance(s) + hcloud_server: + name: "{{ item.name }}" + server_type: "{{ item.server_type }}" + ssh_keys: + - "{{ ssh_key_name }}" + image: "{{ item.image }}" + location: "{{ item.location | default(omit) }}" + datacenter: "{{ item.datacenter | default(omit) }}" + user_data: "{{ item.user_data | default(omit) }}" + api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" + state: present + register: server + loop: "{{ molecule_yml.platforms }}" + async: 7200 + poll: 0 + + - name: Wait for instance(s) creation to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: hetzner_jobs + until: hetzner_jobs.finished + retries: 300 + loop: "{{ server.results }}" + + - name: Create volume(s) + hcloud_volume: + name: "{{ item.name }}" + server: "{{ item.name }}" + location: "{{ item.location | default(omit) }}" + size: "{{ item.volume_size | default(10) }}" + api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" + state: "present" + loop: "{{ molecule_yml.platforms }}" + when: item.volume | default(False) | bool + register: volumes + async: 7200 + poll: 0 + + - name: Wait for volume(s) creation to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: hetzner_volumes + until: hetzner_volumes.finished + retries: 300 + when: volumes.changed + loop: "{{ volumes.results }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config dict + set_fact: + instance_conf_dict: + { + "instance": "{{ item.hcloud_server.name }}", + "ssh_key_name": "{{ ssh_key_name }}", + "address": "{{ item.hcloud_server.ipv4_address }}", + "user": "{{ ssh_user }}", + "port": "{{ ssh_port }}", + "identity_file": "{{ ssh_path }}", + "volume": "{{ item.item.item.volume | default(False) | bool }}", + } + loop: "{{ hetzner_jobs.results }}" + register: instance_config_dict + when: server.changed | bool + + - name: Convert instance config dict to a list + set_fact: + instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" + when: server.changed | bool + + - name: Dump instance config + copy: + content: | + # Molecule managed + + {{ instance_conf | to_nice_yaml(indent=2) }} + dest: "{{ molecule_instance_config }}" + when: server.changed | bool + + - name: Wait for SSH + wait_for: + port: "{{ ssh_port }}" + host: "{{ item.address }}" + search_regex: SSH + delay: 10 + loop: "{{ lookup('file', molecule_instance_config) | from_yaml }}" + + - name: Wait for VM to settle down + pause: + seconds: 30 \ No newline at end of file diff --git a/molecule/centos7/destroy.yml b/molecule/centos7/destroy.yml new file mode 100644 index 0000000..ed0b2ed --- /dev/null +++ b/molecule/centos7/destroy.yml @@ -0,0 +1,78 @@ +--- +- name: Destroy + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + tasks: + - name: Check existing instance config file + stat: + path: "{{ molecule_instance_config }}" + register: cfg + + - name: Populate the instance config + set_fact: + instance_conf: "{{ (lookup('file', molecule_instance_config) | from_yaml) if cfg.stat.exists else [] }}" + + - name: Destroy molecule instance(s) + hcloud_server: + name: "{{ item.instance }}" + api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" + state: absent + register: server + loop: "{{ instance_conf }}" + async: 7200 + poll: 0 + + - name: Wait for instance(s) deletion to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: hetzner_jobs + until: hetzner_jobs.finished + retries: 300 + loop: "{{ server.results }}" + + - pause: + seconds: 5 + + - name: Destroy volume(s) + hcloud_volume: + name: "{{ item.instance }}" + server: "{{ item.instance }}" + api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" + state: "absent" + register: volumes + loop: "{{ instance_conf }}" + when: item.volume | default(False) | bool + async: 7200 + poll: 0 + + - name: Wait for volume(s) deletion to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: hetzner_volumes + until: hetzner_volumes.finished + retries: 300 + when: volumes.changed + loop: "{{ volumes.results }}" + + - name: Remove registered SSH key + hcloud_ssh_key: + name: "{{ instance_conf[0].ssh_key_name }}" + state: absent + when: (instance_conf | default([])) | length > 0 + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config + set_fact: + instance_conf: {} + + - name: Dump instance config + copy: + content: | + # Molecule managed + + {{ instance_conf | to_nice_yaml(indent=2) }} + dest: "{{ molecule_instance_config }}" + when: server.changed | bool \ No newline at end of file diff --git a/molecule/centos7/molecule.yml b/molecule/centos7/molecule.yml new file mode 100644 index 0000000..f93fa9f --- /dev/null +++ b/molecule/centos7/molecule.yml @@ -0,0 +1,24 @@ +--- +dependency: + name: galaxy + options: + role-file: molecule/requirements.yml + requirements-file: molecule/requirements.yml + env: + ANSIBLE_GALAXY_DISPLAY_PROGRESS: "false" +driver: + name: delegated +platforms: + - name: centos7-alertmanager + image: centos-7 + server_type: cx11 +lint: | + /usr/local/bin/flake8 +provisioner: + name: ansible + env: + ANSIBLE_FILTER_PLUGINS: ${ANSIBLE_FILTER_PLUGINS:-./plugins/filter} + ANSIBLE_LIBRARY: ${ANSIBLE_LIBRARY:-./library} + log: False +verifier: + name: testinfra diff --git a/molecule/centos7/prepare.yml b/molecule/centos7/prepare.yml new file mode 100644 index 0000000..183f4d3 --- /dev/null +++ b/molecule/centos7/prepare.yml @@ -0,0 +1,15 @@ +--- +- name: Prepare + hosts: all + gather_facts: false + tasks: + - name: Bootstrap python for Ansible + raw: | + command -v python3 python || ( + (test -e /usr/bin/dnf && sudo dnf install -y python3) || + (test -e /usr/bin/apt && (apt -y update && apt install -y python-minimal)) || + (test -e /usr/bin/yum && sudo yum -y -qq install python3) || + echo "Warning: Python not boostrapped due to unknown platform." + ) + become: true + changed_when: false diff --git a/molecule/centos7/tests/test_default.py b/molecule/centos7/tests/test_default.py new file mode 100644 index 0000000..f754f90 --- /dev/null +++ b/molecule/centos7/tests/test_default.py @@ -0,0 +1,20 @@ +import os +import warnings + +import testinfra.utils.ansible_runner + +warnings.filterwarnings("ignore", category=DeprecationWarning) + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ["MOLECULE_INVENTORY_FILE"]).get_hosts("all") + + +def test_alertmanager_running_and_enabled(host): + alertmanager = host.service("alertmanager") + assert alertmanager.is_running + assert alertmanager.is_enabled + + +def test_alertmanager_socket(host): + # Verify the socket is listening for HTTP traffic + assert host.socket("tcp://127.0.0.1:61001").is_listening diff --git a/molecule/default b/molecule/default new file mode 120000 index 0000000..2fdf3e8 --- /dev/null +++ b/molecule/default @@ -0,0 +1 @@ +centos7 \ No newline at end of file diff --git a/molecule/pytest.ini b/molecule/pytest.ini new file mode 100644 index 0000000..c24fe5b --- /dev/null +++ b/molecule/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +filterwarnings = + ignore::DeprecationWarning diff --git a/molecule/requirements.yml b/molecule/requirements.yml new file mode 100644 index 0000000..46da115 --- /dev/null +++ b/molecule/requirements.yml @@ -0,0 +1,6 @@ +--- +collections: + - name: https://gitea.rknet.org/ansible/xoxys.general/releases/download/v2.1.1/xoxys-general-2.1.1.tar.gz + - name: community.general + +roles: [] diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..605234e --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,6 @@ +--- +- include_tasks: prepare.yml +- import_tasks: tls.yml + when: alertmanager_web_tls_enabled | bool + tags: tls_renewal +- include_tasks: setup.yml diff --git a/tasks/prepare.yml b/tasks/prepare.yml new file mode 100644 index 0000000..bb7e947 --- /dev/null +++ b/tasks/prepare.yml @@ -0,0 +1,36 @@ +--- +- name: Set current Alertmanager version + set_fact: + alertmanager_current_version: "{{ (ansible_local.alertmanager | default (dict(version='0.0.0')))['version'] }}" + +- debug: + msg: Current version is '{{ alertmanager_current_version }}' + +- block: + - name: Create group '{{ alertmanager_group }}' + group: + name: "{{ alertmanager_group }}" + state: present + + - name: Create user '{{ alertmanager_user }}' + user: + comment: Alertmanager + name: "{{ alertmanager_user }}" + home: "{{ alertmanager_user_home }}" + group: "{{ alertmanager_group }}" + groups: "{{ alertmanager_extra_groups | join(',') }}" + + - name: Install dependencies + package: + name: "{{ item }}" + state: present + loop: + - "{{ alertmanager_packages }}" + + - name: Setup local facts + file: + path: /etc/ansible/facts.d + state: directory + mode: 0755 + become: True + become_user: root diff --git a/tasks/setup.yml b/tasks/setup.yml new file mode 100644 index 0000000..d17a889 --- /dev/null +++ b/tasks/setup.yml @@ -0,0 +1,91 @@ +--- +- name: Prepare base folder + file: + path: "{{ alertmanager_base_dir }}" + state: directory + owner: "{{ alertmanager_user }}" + group: "{{ alertmanager_user }}" + mode: 0750 + become: True + become_user: root + +- block: + - name: Prepare folder structure + file: + path: "{{ item }}" + state: directory + mode: 0750 + loop: + - "{{ alertmanager_config_dir }}" + - "{{ alertmanager_templates_dir }}" + - "{{ alertmanager_data_dir }}" + + - name: Download and extract Alertmanager tarball + unarchive: + src: "https://github.com/prometheus/alertmanager/releases/download/v{{ alertmanager_version }}/alertmanager-{{ alertmanager_version }}.linux-amd64.tar.gz" + dest: "{{ alertmanager_base_dir }}" + extra_opts: + - --strip-components=1 + remote_src: yes + exclude: + - alertmanager-{{ alertmanager_version }}.linux-amd64/LICENSE + - alertmanager-{{ alertmanager_version }}.linux-amd64/NOTICE + - alertmanager-{{ alertmanager_version }}.linux-amd64/alertmanager.yml + notify: __alertmanager_restart + when: alertmanager_version is version(alertmanager_current_version, ">") or alertmanager_current_version is version('0.0.0', "=") + become: True + become_user: "{{ alertmanager_user }}" + +- block: + - name: Copy alertmanager config file + template: + src: "conf/alertmanager.yml.j2" + dest: "{{ alertmanager_config_dir }}/alertmanager.yml" + owner: "{{ alertmanager_user }}" + group: "{{ alertmanager_user }}" + mode: 0640 + validate: "{{ alertmanager_base_dir }}/amtool check-config %s" + notify: __alertmanager_restart + + - name: Copy alertmanager web config file + template: + src: "conf/web.yml.j2" + dest: "{{ alertmanager_config_dir }}/web.yml" + owner: "{{ alertmanager_user }}" + group: "{{ alertmanager_user }}" + mode: 0640 + + - name: Copy custom Alertmanager template files + copy: + src: "{{ item }}" + dest: "{{ alertmanager_templates_dir }}" + owner: "{{ alertmanager_user }}" + group: "{{ alertmanager_user }}" + mode: 0640 + with_fileglob: "{{ alertmanager_template_files }}" + notify: __alertmanager_restart + + - name: Copy systemd unit file + template: + src: "etc/systemd/system/alertmanager.service.j2" + dest: "/etc/systemd/system/alertmanager.service" + mode: 0640 + notify: __alertmanager_restart + + - name: Ensure alertmanager service is up and running + systemd: + name: alertmanager + daemon_reload: yes + enabled: yes + state: started + + - name: Set current version to custom fact + template: + src: etc/ansible/facts.d/alertmanager.fact.j2 + dest: /etc/ansible/facts.d/alertmanager.fact + mode: 0644 + owner: root + group: root + when: alertmanager_version is version(alertmanager_current_version, ">") or alertmanager_current_version is version('0.0.0', "=") + become: True + become_user: root diff --git a/tasks/tls.yml b/tasks/tls.yml new file mode 100644 index 0000000..dd17070 --- /dev/null +++ b/tasks/tls.yml @@ -0,0 +1,32 @@ +--- +- block: + - name: Create tls folder structure + file: + path: "{{ item }}" + state: directory + owner: "{{ alertmanager_user }}" + group: "{{ alertmanager_group }}" + recurse: True + loop: + - "{{ alertmanager_web_tls_cert_path | dirname }}" + - "{{ alertmanager_web_tls_key_path | dirname }}" + become: True + become_user: root + +- block: + - name: Copy certs and private key + copy: + src: "{{ item.src }}" + dest: "{{ item.dest }}" + mode: "{{ item.mode }}" + loop: + - src: "{{ alertmanager_web_tls_key_source }}" + dest: "{{ alertmanager_web_tls_key_path }}" + mode: "0600" + - src: "{{ alertmanager_web_tls_cert_source }}" + dest: "{{ alertmanager_web_tls_cert_path }}" + mode: "0750" + loop_control: + label: "{{ item.dest }}" + become: True + become_user: "{{ alertmanager_user }}" diff --git a/templates/conf/alertmanager.yml.j2 b/templates/conf/alertmanager.yml.j2 new file mode 100644 index 0000000..bceb1f7 --- /dev/null +++ b/templates/conf/alertmanager.yml.j2 @@ -0,0 +1,44 @@ +#jinja2: lstrip_blocks: True +{{ ansible_managed | comment }} + +global: + resolve_timeout: {{ alertmanager_resolve_timeout | quote }} + {% if alertmanager_smtp_host is defined %} + smtp_smarthost: "{{ alertmanager_smtp_host }}:{{ alertmanager_smtp_port }}" + smtp_from: {{ alertmanager_smtp_from }} + smtp_auth_username: {{ alertmanager_smtp_auth_username }} + smtp_auth_password: '{{ alertmanager_smtp_auth_password }}' + smtp_require_tls: {{ alertmanager_smtp_require_tls | bool | lower }} + {% endif %} + +templates: +- '{{ alertmanager_config_dir }}/templates/*.tmpl' + +route: + {% if alertmanager_route_group_by | length > 0 %} + group_by: + {{ alertmanager_route_group_by | to_nice_yaml(indent=2) | indent(2, False) }} + + {% endif %} + group_wait: {{ alertmanager_route_group_wait }} + group_interval: {{ alertmanager_route_group_interval }} + repeat_interval: {{ alertmanager_route_repeat_interval }} + {% if alertmanager_route_default_receiver is defined %} + + receiver: {{ alertmanager_route_default_receiver }} + {% endif %} + {% if alertmanager_route_routes | length > 0 %} + + routes: + {{ alertmanager_route_routes | to_nice_yaml(indent=2) | indent(2, False) }} + {% endif %} +{% if alertmanager_receivers | length > 0 %} + +receivers: +{{ alertmanager_receivers | to_nice_yaml(indent=2) }} +{% endif %} +{% if alertmanager_inhibit_rules | length > 0 %} + +inhibit_rules: +{{ alertmanager_inhibit_rules | to_nice_yaml(indent=2) }} +{% endif %} diff --git a/templates/conf/web.yml.j2 b/templates/conf/web.yml.j2 new file mode 100644 index 0000000..e69eebe --- /dev/null +++ b/templates/conf/web.yml.j2 @@ -0,0 +1,26 @@ +#jinja2: lstrip_blocks: True +{{ ansible_managed | comment }} + +{% if alertmanager_web_tls_enabled | bool %} +tls_server_config: + cert_file: {{ alertmanager_web_tls_cert_path }} + key_file: {{ alertmanager_web_tls_key_path }} +{% else %} +tls_server_config: {} +{% endif %} +{% if alertmanager_web_http_server is defined %} + +http_server_config: + {{ alertmanager_web_http_server | to_nice_yaml(indent=2) | indent(2,False) }} +{% else %} + +http_server_config: {} +{% endif %} +{% if alertmanager_web_basic_auth_users is defined %} + +basic_auth_users: + {{ alertmanager_web_basic_auth_users | to_nice_yaml(indent=2) | indent(2,False) }} +{% else %} + +basic_auth_users: {} +{% endif %} diff --git a/templates/etc/ansible/facts.d/alertmanager.fact.j2 b/templates/etc/ansible/facts.d/alertmanager.fact.j2 new file mode 100644 index 0000000..d9dbc6e --- /dev/null +++ b/templates/etc/ansible/facts.d/alertmanager.fact.j2 @@ -0,0 +1,4 @@ +{ + "comment" : "{{ ansible_managed }}", + "version" : "{{ alertmanager_version }}" +} diff --git a/templates/etc/systemd/system/alertmanager.service.j2 b/templates/etc/systemd/system/alertmanager.service.j2 new file mode 100644 index 0000000..11959bb --- /dev/null +++ b/templates/etc/systemd/system/alertmanager.service.j2 @@ -0,0 +1,51 @@ +{{ ansible_managed | comment }} +[Unit] +Description=Alertmanager +After=network-online.target +Requires=local-fs.target +After=local-fs.target + +[Service] +Type=simple +Environment="GOMAXPROCS={{ ansible_processor_vcpus | default(ansible_processor_count) }}" +User={{ alertmanager_user }} +Group={{ alertmanager_group }} +WorkingDirectory={{ alertmanager_base_dir }} +ExecReload=/bin/kill -HUP $MAINPID +ExecStart={{ alertmanager_base_dir }}/alertmanager \ + --storage.path={{ alertmanager_data_dir }} \ + --web.config.file={{ alertmanager_config_dir }}/web.yml \ + --web.listen-address={{ alertmanager_web_bind_ip }}:{{ alertmanager_web_bind_port }} \ + --web.external-url={{ alertmanager_web_external_url }} \ + --cluster.listen-address= \ +{% for flag in alertmanager_config_flags_extra %} +{% if flag.value is not defined %} + --{{ flag.name }} \ +{% elif flag.value is string %} + --{{ flag.name }}={{ flag.value }} \ +{% elif flag.value is sequence %} +{% for flag_value_item in flag.value %} + --{{ flag.name }}={{ flag_value_item }} \ +{% endfor %} +{% endif %} +{% endfor %} + --log.level={{ alertmanager_log_level }} \ + --config.file={{ alertmanager_config_dir }}/alertmanager.yml + +LimitNOFILE=65000 +NoNewPrivileges=true +PrivateDevices=true +PrivateTmp=true +ProtectHome=true + +ReadWriteDirectories={{ alertmanager_data_dir }} +{% for path in alertmanager_read_only_dirs %} +ReadOnlyDirectories={{ path }} +{% endfor %} + +ProtectSystem=full +SyslogIdentifier=alertmanager +Restart=on-failure + +[Install] +WantedBy=multi-user.target