- name: Creating alertmanager user group
  group: name="alertmanager"
  become: true

- name: Creating alertmanager user
  user:
    name: "alertmanager"
    group: "alertmanager"
    system: yes
    shell: "/sbin/nologin"
    comment: "alertmanager nologin User"
    createhome: "no"
    state: present

- name: AlertManager... Download alertmanager archive
  get_url:
    url:  "{{ alertmanager_url }}/{{ alertmanager_tarball }}.tar.gz"
    dest: /tmp/{{ alertmanager_tarball }}.tar.gz
    checksum: "sha256:{{ alertmanager_url }}/sha256sums.txt"
  register: _download_archive
  until: _download_archive is succeeded
  retries: 5
  delay: 2
  when: not alertmanager_skip_install

- name: AlertManager... unpack alertmanager binaries
  unarchive:
    src: "/tmp/{{ alertmanager_tarball }}.tar.gz"
    dest: "/tmp"
    creates: "/tmp/{{ alertmanager_tarball }}/alertmanager"
    remote_src: true
  when: not alertmanager_skip_install

- name: Copy alertmanager executable to bin
  copy:
    src: "/tmp/{{ alertmanager_tarball }}/alertmanager"
    dest: "/usr/local/bin/alertmanager"
    owner: alertmanager
    group: alertmanager
    remote_src: yes
    mode: 0755

- name: Delete alertmanager tmp folder
  file:
    path: '/tmp/{{ alertmanager_tarball }}'
    state: absent

- name: Creates data directory
  file: 
    path: "/data/alertmanager/"
    state: directory
    owner: alertmanager
    group: alertmanager
    mode: 0755

- name: Creates config directory
  file: 
    path: "/etc/alertmanager/"
    state: directory
    owner: alertmanager
    group: alertmanager
    mode: 0755

- name: Copy config file
  copy:
    src: "{{ role_path }}/files/alertmanager.yml"
    dest: /etc/alertmanager/alertmanager.yml

- name: AlertManager... Copy systemd init file
  copy:
    dest: /etc/systemd/system/alertmanager.service
    src: files/alertmanager.service
    owner: root
    group: root
    mode: 0640
  notify: systemd_reload

- name: Start alertmanager service
  service:
    name: alertmanager
    state: started
    enabled: yes

- name: Check if alertmanager is accessible
  uri:
    url: http://localhost:9093
    method: GET
    status_code: 200
