---
- hosts: localhost
  gather_facts: no
  connection: local
  become: no
  vars:
    g_server: http://localhost:8080/zabbix/api_jsonrpc.php
    g_user: ''
    g_password: ''

  roles:
  - lib_zabbix

  post_tasks:
  - name: CLEAN List template for heartbeat
    zbx_template:
      zbx_server: "{{ g_server }}"
      zbx_user: "{{ g_user }}"
      zbx_password: "{{ g_password }}"
      state: list
      name: 'Template Heartbeat'
    register: templ_heartbeat

  - name: CLEAN List template app zabbix server
    zbx_template:
      zbx_server: "{{ g_server }}"
      zbx_user: "{{ g_user }}"
      zbx_password: "{{ g_password }}"
      state: list
      name: 'Template App Zabbix Server'
    register: templ_zabbix_server

  - name: CLEAN List template app zabbix server
    zbx_template:
      zbx_server: "{{ g_server }}"
      zbx_user: "{{ g_user }}"
      zbx_password: "{{ g_password }}"
      state: list
      name: 'Template App Zabbix Agent'
    register: templ_zabbix_agent

  - name: CLEAN List all templates
    zbx_template:
      zbx_server: "{{ g_server }}"
      zbx_user: "{{ g_user }}"
      zbx_password: "{{ g_password }}"
      state: list
    register: templates

  - debug: var=templ_heartbeat.results

  - name: Remove templates if heartbeat template is missing
    zbx_template:
      zbx_server: "{{ g_server }}"
      zbx_user: "{{ g_user }}"
      zbx_password: "{{ g_password }}"
      name: "{{ item }}"
      state: absent
    with_items: "{{ templates.results | difference(templ_zabbix_agent.results) | difference(templ_zabbix_server.results) | oo_collect('host') }}"
    when:  templ_heartbeat.results | length == 0
