---

- hosts: srv1c
  become: yes
  tasks:
  - name: Check zabbix agent installation
    shell: "ZABBIX_COMMAND=$(pgrep -a zabbix_agent) && awk -F= '/^Include=\\/etc\\// {print $2}' 
      $(echo $ZABBIX_COMMAND | sed -nr 's/.*-c ([^ ]+).*/\\1/p') | xargs dirname"
    register: 'zabbix_agent_current'

  - name: 
    set_fact:
      zabbix_configs: '{{ zabbix_agent_current.stdout | default("/etc/zabbix/zabbix_agent2.d") }}'

  - name: Include role tasks by host groups
    include_role:
      name: '{{ item }}'
    when: 'item == "general" or inventory_hostname in groups[item]'
    loop:
      - 'general'
      - 'srv1c_cs'
      - 'srv1c_ls'
      - 'srv1c_ws'

- hosts: hosts_to_restart
  gather_facts: no
  become: yes
  tasks:
    - name: Restart zabbix agent
      service:
        name: 'zabbix-agent{% if zabbix_configs | regex_search("2") %}2{% endif %}'
        state: restarted
