---
- name: Create webhook install directory
  ansible.builtin.file:
    path: "{{ webhook_install_dir }}"
    state: directory
    owner: "{{ webhook_user }}"
    group: "{{ webhook_user }}"
    mode: '0755'

- name: Deploy webhook script
  ansible.builtin.copy:
    src: webhook.py
    dest: "{{ webhook_install_dir }}/webhook.py"
    owner: "{{ webhook_user }}"
    group: "{{ webhook_user }}"
    mode: '0755'
  notify: Restart alertmanager-webhook

- name: Deploy webhook systemd service
  ansible.builtin.template:
    src: alertmanager-webhook.service.j2
    dest: /etc/systemd/system/alertmanager-webhook.service
    mode: '0644'
  notify: Restart alertmanager-webhook

- name: Enable and start alertmanager-webhook
  ansible.builtin.systemd:
    name: alertmanager-webhook
    enabled: true
    state: started
    daemon_reload: true

- name: Open webhook port in nftables
  ansible.builtin.lineinfile:
    path: /etc/nftables.conf
    insertafter: '# prometheus'
    line: "    tcp dport {{ webhook_port }} ip saddr 192.168.10.0/24 accept comment \"alertmanager-webhook\""
    state: present
  notify: Reload nftables
  ignore_errors: true
