---
# create the hosts configs and coresponding monit checks

- name: compile list of hosts using perfact-phonehome-checker
  tags: perfact-phonehome-checker
  set_fact:
    phonehome_checker_hosts: "{{
      hosts |  sort_dict_by_keys | dict2items | selectattr('value.perfact_phonehome_checker', 'defined')  }}"
  changed_when: False

#- name: debug list
#  debug:
#    var: phonehome_checker_hosts

# create config directory if does not exist
- name: create phonehome checker config directory
  become: yes
  file:
    path: /etc/perfact/phonehome-checker/conf.d
    state: directory
    mode: '0755'

# create conf for every host which has perfact_phonehome_checker set
- name: create hosts configs phonehome
  loop: "{{ phonehome_checker_hosts }}"
  tags: perfact-phonehome-checker
  become: yes
  ansible.builtin.template:
    src: hosts-template.j2
    dest: /etc/perfact/phonehome-checker/conf.d/{{item.key}}.py
  notify: restart perfact-phonehome-checker

# add monit configuration
- name: monit configuration for perfact-phonehome-checker
  tags: perfact-phonehome-checker
  become: yes
  ansible.builtin.template:
    src: monit-template.j2
    dest: /etc/monit/conf.d/perfact-phonehome-checker
  notify: restart monit
