---
- tags:
    - config
  block:
    - when:
        - ansible_os_family == "Windows"
      notify:
        - restart win zabbix agent
      block:
        - name: Windows | Install user-defined userparameters
          ansible.windows.win_template:
            src: "{{ zabbix_agent_userparameters_templates_src }}/{{ item.name }}.j2"
            dest: '{{ zabbix_agent_win_include }}\{{ item.name }}.conf'
          loop: "{{ zabbix_agent_userparameters | rejectattr('scripts_dir', 'defined') }}"

        - name: Windows | Install user-defined scripts
          ansible.windows.win_copy:
            src: "{{ zabbix_agent_userparameters_scripts_src }}/{{ item.scripts_dir }}"
            dest: '{{ zabbix_win_install_dir }}\scripts\'
          loop: "{{ zabbix_agent_userparameters | selectattr('scripts_dir', 'defined') }}"

    - when:
        - ansible_os_family != 'Windows'
      notify:
        - restart zabbix-agent
        - restart mac zabbix agent
      become: true
      block:
        - name: Install user-defined userparameters
          ansible.builtin.template:
            src: "{{ zabbix_agent_userparameters_templates_src }}/{{ item.name }}.j2"
            dest: "{{ zabbix_agent_include_dir if zabbix_agent_include_dir is string else zabbix_agent_include_dir | first }}/userparameter_{{ item.name }}.conf"
            owner: zabbix
            group: zabbix
            mode: "0644"
          loop: "{{ zabbix_agent_userparameters }}"

        - name: Install user-defined scripts
          ansible.builtin.copy:
            src: "{{ zabbix_agent_userparameters_scripts_src }}/{{ item.scripts_dir }}"
            dest: /etc/zabbix/scripts/
            owner: zabbix
            group: zabbix
            mode: "0755"
          loop: "{{ zabbix_agent_userparameters | selectattr('scripts_dir', 'defined') }}"
