From be0ae44a32430bdcc37d969145bf448eb0efda93 Mon Sep 17 00:00:00 2001 From: Ilya Kanyukov Date: Fri, 26 Jul 2024 17:52:39 +0500 Subject: [PATCH 1/3] Added variable vic_vm_alert_service_name --- roles/vmalert/README.md | 1 + roles/vmalert/defaults/main.yml | 1 + roles/vmalert/handlers/main.yml | 6 ++++-- roles/vmalert/tasks/configure.yml | 15 ++++++++------- roles/vmalert/templates/systemd-service.j2 | 2 +- roles/vmalert/templates/upstart.j2 | 8 ++++---- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/roles/vmalert/README.md b/roles/vmalert/README.md index be3ff89..5825b8f 100644 --- a/roles/vmalert/README.md +++ b/roles/vmalert/README.md @@ -27,3 +27,4 @@ Installs `vmalert` as binary running with systemd | vic_vm_alert_service_args | Passes options defined above to `vmalert`. | see [defaults.yml](./defaults/main.yml) | | vic_vm_alert_service_args | Passes options defined above to `vmalert`. | see [defaults.yml](./defaults/main.yml) | | vic_vm_alert_rules | Rules | see [defaults.yml](./defaults/main.yml) | +| vic_vm_alert_service_name | Service name that will be created by systemd or init | see [defaults.yml](./defaults/main.yml) \ No newline at end of file diff --git a/roles/vmalert/defaults/main.yml b/roles/vmalert/defaults/main.yml index 6a9cf1d..b731ca9 100644 --- a/roles/vmalert/defaults/main.yml +++ b/roles/vmalert/defaults/main.yml @@ -3,6 +3,7 @@ vic_vm_alert_version: "v1.102.0" vic_vm_alert_enterprise: false vic_vm_alert_license_key: "" vic_vm_alert_license_key_file: "" +vic_vm_alert_service_name: vic-vmalert vic_vm_alert_repo_url: "https://github.com/VictoriaMetrics/VictoriaMetrics" vic_vm_alert_platform: "{% if vic_vm_alert_version.replace('v', '') is version('1.79.0', '>=') %}-linux{% endif %}" diff --git a/roles/vmalert/handlers/main.yml b/roles/vmalert/handlers/main.yml index 269074e..a392ff5 100644 --- a/roles/vmalert/handlers/main.yml +++ b/roles/vmalert/handlers/main.yml @@ -1,7 +1,9 @@ # handlers file for VictoriaMetrics --- -- name: Restart VMalert service +- name: "Restart VMalert service {{ vic_vm_alert_service_name }}" become: true ansible.builtin.service: - name: vic-vmalert + name: "{{ vic_vm_alert_service_name }}" state: restarted + ignore_errors: '{{ ansible_check_mode }}' + listen: "Restart VMalert service" diff --git a/roles/vmalert/tasks/configure.yml b/roles/vmalert/tasks/configure.yml index af4945b..8f3fd05 100644 --- a/roles/vmalert/tasks/configure.yml +++ b/roles/vmalert/tasks/configure.yml @@ -5,13 +5,13 @@ - name: "Systemd | Copy VMalert systemd unit file" ansible.builtin.template: src: systemd-service.j2 - dest: /etc/systemd/system/vic-vmalert.service + dest: "/etc/systemd/system/{{ vic_vm_alert_service_name }}.service" owner: root group: root mode: 0644 register: config_template notify: Restart VMalert service - no_log: True + # no_log: True - name: "Systemd | daemon-reload VMalert service" # noqa: no-handler become: true @@ -22,25 +22,26 @@ - name: Ensure VMalert service is enabled on boot become: true ansible.builtin.systemd: - name: vic-vmalert + name: "{{ vic_vm_alert_service_name }}" enabled: true + ignore_errors: '{{ ansible_check_mode }}' - name: Configure upstart when: ansible_service_mgr == "upstart" block: - - name: "Upstart | Install vic-vmalert service file" + - name: "Upstart | Install {{ vic_vm_alert_service_name }} service file" ansible.builtin.template: src: "upstart.j2" - dest: "/etc/init.d/vic-vmalert" + dest: "/etc/init.d/{{ vic_vm_alert_service_name }}" mode: "0755" owner: root group: root notify: Restart VMalert service register: config_template - - name: "Upstart | Enable vic-vmalert service" + - name: "Upstart | Enable {{ vic_vm_alert_service_name }} service" ansible.builtin.service: - name: "vic-vmalert" + name: "{{ vic_vm_alert_service_name }}" enabled: "yes" - name: Prepare configuration dir diff --git a/roles/vmalert/templates/systemd-service.j2 b/roles/vmalert/templates/systemd-service.j2 index 9264689..991d0d1 100644 --- a/roles/vmalert/templates/systemd-service.j2 +++ b/roles/vmalert/templates/systemd-service.j2 @@ -10,7 +10,7 @@ User={{ vic_vm_alert_system_user }} Group={{ vic_vm_alert_system_group }} ExecStart=/usr/local/bin/vmalert-prod {% for flag, flag_value in vic_vm_alert_service_args.items() %}--{{ flag }}={{ flag_value }} {% endfor %} -SyslogIdentifier=vic-vmalert +SyslogIdentifier={{ vic_vm_alert_service_name }} Restart=always PrivateTmp=yes diff --git a/roles/vmalert/templates/upstart.j2 b/roles/vmalert/templates/upstart.j2 index fb5173e..c3c53de 100644 --- a/roles/vmalert/templates/upstart.j2 +++ b/roles/vmalert/templates/upstart.j2 @@ -1,21 +1,21 @@ #! /bin/sh ### BEGIN INIT INFO -# Provides: vic-vmalert +# Provides: {{ vic_vm_alert_service_name }} # Required-Start: $syslog # Required-Stop: $syslog # Should-Start: $local_fs # Should-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 -# Short-Description: vic-vmalert - VictoriaMetrics scrape agent -# Description: vic-vmalert - VictoriaMetrics scrape agent +# Short-Description: {{ vic_vm_alert_service_name }} - VictoriaMetrics scrape agent +# Description: {{ vic_vm_alert_service_name }} - VictoriaMetrics scrape agent ### END INIT INFO PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/bin/VMalert-prod DAEMON_ARGS="{% for flag, flag_value in vic_vm_alert_service_args.items() %}--{{ flag }}={{ flag_value }} {% endfor %}" -NAME=vic-vmalert +NAME={{ vic_vm_alert_service_name }} DESC="VictoriaMetrics scrape agent" test -x $DAEMON || exit 0 From 38ea5eb4c4b9c1df869abeb923820dd76deea69a Mon Sep 17 00:00:00 2001 From: Ilya Kanyukov Date: Fri, 26 Jul 2024 17:56:53 +0500 Subject: [PATCH 2/3] Returned no_log to templating --- roles/vmalert/tasks/configure.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/vmalert/tasks/configure.yml b/roles/vmalert/tasks/configure.yml index 8f3fd05..4ccd480 100644 --- a/roles/vmalert/tasks/configure.yml +++ b/roles/vmalert/tasks/configure.yml @@ -11,7 +11,7 @@ mode: 0644 register: config_template notify: Restart VMalert service - # no_log: True + no_log: True - name: "Systemd | daemon-reload VMalert service" # noqa: no-handler become: true From 75231a0862019a11b19943df852001e9d97de110 Mon Sep 17 00:00:00 2001 From: Ilya Kanyukov Date: Fri, 26 Jul 2024 18:28:08 +0500 Subject: [PATCH 3/3] Move variable to the end of task name --- roles/vmalert/tasks/configure.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roles/vmalert/tasks/configure.yml b/roles/vmalert/tasks/configure.yml index 4ccd480..eafe473 100644 --- a/roles/vmalert/tasks/configure.yml +++ b/roles/vmalert/tasks/configure.yml @@ -29,7 +29,7 @@ - name: Configure upstart when: ansible_service_mgr == "upstart" block: - - name: "Upstart | Install {{ vic_vm_alert_service_name }} service file" + - name: "Upstart | Install service file {{ vic_vm_alert_service_name }}" ansible.builtin.template: src: "upstart.j2" dest: "/etc/init.d/{{ vic_vm_alert_service_name }}" @@ -39,7 +39,7 @@ notify: Restart VMalert service register: config_template - - name: "Upstart | Enable {{ vic_vm_alert_service_name }} service" + - name: "Upstart | Enable service {{ vic_vm_alert_service_name }}" ansible.builtin.service: name: "{{ vic_vm_alert_service_name }}" enabled: "yes"