#####################################
### someone"s ansible provisioner ###
#####################################
# Part of: https://git.somenet.org/root/pub/somesible.git
# 2017-2026 by someone <someone@somenet.org>
#
# Install munin-node and munin-async.
#
---
- name: install munin
  apt:
    pkg:
    - munin-async
    - munin-node
    - munin-plugins-extra
    state: present
    policy_rc_d: 101
  when: munin_node_setup | bool
  tags: "online"


- name: create munin-plugins dir
  file:
    path: "/opt/somesible/munin-plugins"
    state: directory
    mode: 0755
    owner: "root"
    group: "root"
  when: munin_node_setup | bool


- name: copy custom plugins
  copy:
    src:  "{{item.src}}"
    dest: "/opt/somesible/munin-plugins/{{item.path}}"
    mode: 0755
    owner: "root"
    group: "root"
  with_filetree:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/plugins.somesible/"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/plugins.somesible/"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/plugins.somesible/"
    - "default/plugins.somesible/"
  when: munin_node_setup | bool and item.state == "file"
  notify: restart munin-node.service and munin-async.service


- name: list files in /etc/munin/plugins
  shell: "ls -1 /etc/munin/plugins"
  register: contents
  when: munin_node_setup | bool
  changed_when: False


- name: remove unmanaged files in /etc/munin/plugins
  file:
    path: "/etc/munin/plugins/{{item}}"
    state: absent
  with_items: "{{contents.stdout_lines}}"
  when: munin_node_setup | bool and item not in munin_node_plugins.keys() | list and item not in munin_node_plugins_extra.keys() | list
  notify: restart munin-node.service and munin-async.service


- name: symlink new files into /etc/munin/plugins
  file:
    dest: "/etc/munin/plugins/{{item.key}}"
    src: "{{item.value}}"
    force: yes
    owner: "root"
    group: "root"
    state: link
  loop: "{{ lookup('dict', munin_node_plugins|combine(munin_node_plugins_extra)) }}"
  when: munin_node_setup | bool
  notify: restart munin-node.service and munin-async.service


- name: copy plugin-conf.d/munin-node
  copy:
    src:  "{{item}}"
    dest: "/etc/munin/plugin-conf.d/munin-node"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/plugin-confd.munin-node.conf"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/plugin-confd.munin-node.conf"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/plugin-confd.munin-node.conf"
    - "default/plugin-confd.munin-node.conf"
  when: munin_node_setup | bool
  notify: restart munin-node.service and munin-async.service


- name: copy munin-node.service to /etc/systemd/system/
  copy:
    src: "{{item}}"
    dest: "/etc/systemd/system/munin-node.service"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/munin-node.service"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/munin-node.service"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/munin-node.service"
    - "default/munin-node.service"
  when: munin_node_setup | bool
  notify: restart munin-node.service and munin-async.service


- name: create munin-async home and fix ownership
  file:
    path: "/var/lib/munin-async/"
    state: directory
    recurse: yes
    owner: "munin-async"
    group: "munin-async"
  when: munin_node_setup | bool


- name: create .ssh-dir
  file:
    path: "/var/lib/munin-async/.ssh"
    state: directory
    mode: 0700
    owner: "munin-async"
    group: "munin-async"
  when: munin_node_setup | bool


- name: copy authorized_keys to /var/lib/munin-async/.ssh/authorized_keys
  copy:
    src: "{{item}}"
    dest: "/var/lib/munin-async/.ssh/authorized_keys"
    mode: 0600
    owner: "munin-async"
    group: "munin-async"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/authorized_keys"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/authorized_keys"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/authorized_keys"
    - "default/authorized_keys"
  when: munin_node_setup | bool


- name: copy munin-async.service to /etc/systemd/system/
  copy:
    src: "{{item}}"
    dest: "/etc/systemd/system/munin-async.service"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/munin-async.service"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/munin-async.service"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/munin-async.service"
    - "default/munin-async.service"
  when: munin_node_setup | bool
  notify: restart munin-node.service and munin-async.service


- name: enable and start munin-node.service
  include_role: name="base/systemd/enable-and-start"
  vars:
    service_name: munin-node.service
  when: munin_node_setup | bool


- name: enable and start munin-async.service
  include_role: name="base/systemd/enable-and-start"
  vars:
    service_name: munin-async.service
  when: munin_node_setup | bool
