---


# ---
# Set some facts
# ---


- name: (modify-munin-ip) Set fact_sshd_kexalgorithms (comma separated list)
  set_fact:
    fact_munin_node_ipv4: "{{ munin_remote_ipv4 | replace('.', '\\.') }}"
  when:
    - munin_remote_ipv4 is defined and munin_remote_ipv4 | length > 0

- name: (modify-munin-ip - main.yml)) Set fact_sshd_kexalgorithms (comma separated list)
  set_fact:
    fact_munin_node_ipv4_old: "{{ munin_remote_ipv4_old | replace('.', '\\\\.') }}"
  when:
    - munin_remote_ipv4_old is defined and munin_remote_ipv4_old | length > 0

  # ---
  # Some Checks
  # ---

- name: (modify-munin-ip) Check if file '/etc/ipt-firewall/main_ipv4.conf' exists
  stat:
    path: /etc/ipt-firewall/main_ipv4.conf
  register: main_ipv4_exists


- name: Check if /etc/ipt-firewall/interfaces_ipv4.conf are present
  stat:
    path: /etc/ipt-firewall/interfaces_ipv4.conf
  register: interfaces_ipv4_exists


- name: Check if /etc/ipt-firewall/interfaces_ipv6.conf are present
  stat:
    path: /etc/ipt-firewall/interfaces_ipv6.conf
  register: interfaces_ipv6_exists


- name: Check if file '/etc/ipt-firewall/main_ipv6.conf' exists
  stat:
    path: /etc/ipt-firewall/main_ipv6.conf
  register: main_ipv6_exists


- name: (modify-munin-ip) Check if file '/etc/munin/munin-node.conf' exists
  stat:
    path: /etc/munin/munin-node.conf
  register: munin_node_exists


- name: (modify-munin-ip) /etc/ipt-firewall/main_ipv4.conf - addjust line 'munin_remote_ip' (IPv4)
  lineinfile:
    path: /etc/ipt-firewall/main_ipv4.conf
    regexp: '^munin_remote_ip='
    line: 'munin_remote_ip="{{ munin_remote_ipv4 }}"'
  when:
    - main_ipv4_exists.stat.exists
  notify:
    - Restart IPv4 Firewall


- name: (modify-munin-ip) /etc/ipt-firewall/main_ipv6.conf - addjust line 'munin_remote_ip' (IPv6)
  lineinfile:
    path: /etc/ipt-firewall/main_ipv6.conf
    regexp: '^munin_remote_ip='
    line: 'munin_remote_ip="{{ munin_remote_ipv6 }}"'
  when:
    - main_ipv6_exists.stat.exists
  notify:
    - Restart IPv6 Firewall


- name: (modify-munin-ip)  /etc/munin/munin-node.conf - addjust line 'allow ^' (IPv4)
  lineinfile:
    path: /etc/munin/munin-node.conf
    regexp: '^allow \^{{ fact_munin_node_ipv4_old }}'
    line: 'allow ^{{ fact_munin_node_ipv4 }}$'
  when:
    - munin_node_exists.stat.exists
  notify:
    - Restart munin-node


- name: (modify-munin-ip)  /etc/munin/munin-node.conf - addjust line 'allow ^' (IPv4)
  lineinfile:
    path: /etc/munin/munin-node.conf
    regexp: '^allow \^{{ munin_remote_ipv6_old }}'
    line: 'allow ^{{ munin_remote_ipv6 }}$'
  when:
    - munin_node_exists.stat.exists
  notify:
    - Restart munin-node



