---
- name: "Set default ip address for zabbix_agent_ip"
  ansible.builtin.set_fact:
    zabbix_agent_ip: "{{ hostvars[inventory_hostname]['ansible_default_ipv4'].address }}"
  when:
    - zabbix_agent_ip is not defined
    - "'ansible_default_ipv4' in hostvars[inventory_hostname]"
  tags:
    - config

- name: "Get Total Private IP Addresses"
  ansible.builtin.set_fact:
    total_private_ip_addresses: "{{ ansible_all_ipv4_addresses | ansible.utils.ipaddr('private') | length }}"
  when:
    - ansible_all_ipv4_addresses is defined
    - not (zabbix_agent_dont_detect_ip)
  tags:
    - config

- name: "Set first public ip address for zabbix_agent_ip"
  ansible.builtin.set_fact:
    zabbix_agent_ip: "{{ ansible_all_ipv4_addresses | ansible.netcommon.ipaddr('public') | first }}"
  when:
    - zabbix_agent_ip is not defined
    - total_private_ip_addresses is defined
    - total_private_ip_addresses == '0'
  tags:
    - config

- name: "Set first private ip address for zabbix_agent_ip"
  ansible.builtin.set_fact:
    zabbix_agent_ip: "{{ ansible_all_ipv4_addresses | ansible.netcommon.ipaddr('private') | first }}"
  when:
    - zabbix_agent_ip is not defined
    - total_private_ip_addresses is defined
    - total_private_ip_addresses != '0'
  tags:
    - config

- name: "Fail invalid specified agent_listeninterface"
  ansible.builtin.fail:
    msg: "The specified network interface does not exist"
  when:
    - (zabbix_agent_listeninterface)
    - (zabbix_agent_listeninterface not in ansible_interfaces)
  tags:
    - config

- name: "Set network interface"
  ansible.builtin.set_fact:
    network_interface: ansible_{{ zabbix_agent_listeninterface }}
  when:
    - (zabbix_agent_listeninterface)
    - zabbix_agent_listenip is undefined
  tags:
    - config

- name: "Get IP of agent_listeninterface when no agent_listenip specified"
  ansible.builtin.set_fact:
    zabbix_agent_listenip: "{{ hostvars[inventory_hostname][network_interface]['ipv4'].address | default('0.0.0.0') }}"
  when:
    - (zabbix_agent_listeninterface)
    - zabbix_agent_listenip is undefined
  tags:
    - config
    - api

- name: "Default agent_listenip to all when not specified"
  ansible.builtin.set_fact:
    zabbix_agent_listenip: "0.0.0.0"
  when:
    - zabbix_agent_listenip is undefined
  tags:
    - config

- name: "Fail invalid specified agent_listenip"
  ansible.builtin.fail:
    msg: "The {{ item }} (agent_listenip) does not exist"
  when:
    - item != '0.0.0.0'
    - item != '127.0.0.1'
    - item not in ansible_all_ipv4_addresses
  with_items:
    "{{ zabbix_agent_listenip | split(',') }}"
  tags:
    - config

- name: "Configure SELinux when enabled"
  ansible.builtin.include_tasks: selinux.yml
  when: ansible_facts.selinux.status | default('disabled') == 'enabled'

- name: "Adding zabbix group"
  ansible.builtin.group:
    name: zabbix
    state: present
    gid: "{{ zabbix_agent_docker_user_gid | default(omit) }}"
  become: true
  when:
    - zabbix_agent_docker | bool
  tags:
    - config

- name: "Adding zabbix user"
  ansible.builtin.user:
    name: zabbix
    group: zabbix
    state: present
    create_home: false
    home: /etc/zabbix
    uid: "{{ zabbix_agent_docker_user_uid | default(omit) }}"
    system: true
  become: true
  when:
    - zabbix_agent_docker | bool
  tags:
    - config

- name: "Configure zabbix-agent"
  ansible.builtin.template:
    src: agent.conf.j2
    dest: "/etc/zabbix/zabbix_agent{{ (zabbix_agent2 | bool) | ternary('2', 'd') }}.conf"
    owner: root
    group: root
    mode: "{{ zabbix_agent_conf_mode }}"
  notify:
    - restart zabbix-agent
  become: true
  when:
    - not (zabbix_agent_docker | bool)
  tags:
    - config

- name: "Create include dirs zabbix-agent"
  ansible.builtin.file:
    path: "{{ include_dir }}"
    owner: root
    group: root
    mode: "{{ zabbix_agent_include_mode }}"
    state: directory
  loop: "{{ [zabbix_agent_include_dir] if zabbix_agent_include_dir is string else zabbix_agent_include_dir }}"
  loop_control:
    loop_var: 'include_dir'
  become: true
  tags:
    - config

- name: "Install the Docker container"
  ansible.builtin.include_tasks: Docker.yml
  when:
    - zabbix_agent_docker | bool

- name: "Remove zabbix-agent installation when zabbix-agent2 is used."
  ansible.builtin.include_tasks: remove.yml
  when:
    - zabbix_agent2 | bool
    - zabbix_agent_package_remove

- name: "Make sure the zabbix-agent service is running"
  ansible.builtin.service:
    name: "{{ zabbix_agent_service }}"
    state: started
    enabled: true
  become: true
  when:
    - not (zabbix_agent_docker | bool)
  tags:
    - service

- name: "Give zabbix-agent access to system.hw.chassis info"
  ansible.builtin.file:
    path: /sys/firmware/dmi/tables/DMI
    owner: root
    group: zabbix
  become: true
  when:
    - zabbix_agent_chassis is defined
    - zabbix_agent_chassis | bool
  tags:
    - config
