---
- name: Add munin to hosts
  ansible.builtin.lineinfile:
    dest: /etc/hosts
    regexp: 'munin$'
    line: '127.0.0.1        munin'
    insertafter: EOF

- name: Packages for Munin CGI are installed
  ansible.builtin.apt:
    name:
      - liblwp-useragent-determined-perl
      - libcgi-fast-perl
      - spawn-fcgi
    state: present

- name: Owner for munin-cgi is set to www-data:munin
  ansible.builtin.shell:
    cmd: "chown --verbose www-data:munin /var/log/munin/munin-cgi-*"
  register: command_result
  changed_when: "'changed' in command_result.stdout"

- name: Mode for munin-cgi is set to 660
  ansible.builtin.shell:
    cmd: "chmod --verbose 660 /var/log/munin/munin-cgi-*"
  register: command_result
  changed_when: "'changed' in command_result.stdout"

- name: Systemd unit for Munin-fcgi is installed
  ansible.builtin.copy:
    src: systemd/spawn-fcgi-munin-graph.service
    dest: /etc/systemd/system/spawn-fcgi-munin-graph.service
    force: yes

# WARN: there is no (apparent) way to check if the service exists
#       so we disable this task in check mode.
- name: Systemd unit for Munin-fcgi is started
  ansible.builtin.systemd:
    name: spawn-fcgi-munin-graph
    daemon_reload: yes
    enabled: yes
    state: started
  when: not ansible_check_mode
