---
- name: Prepare
  hosts: all
  tasks:
    - name: "Installing packages on CentOS family"
      ansible.builtin.package:
        pkg:
          - net-tools
          - which
        state: present
      register: zabbix_agent_prepare_packages_install
      until: zabbix_agent_prepare_packages_install is succeeded
      when:
        - ansible_os_family == 'RedHat'

    - name: "Installing packages on CentOS (Sangoma) family"
      ansible.builtin.package:
        pkg:
          - net-tools
          - which
        state: present
      register: zabbix_agent_prepare_packages_install
      until: zabbix_agent_prepare_packages_install is succeeded
      when:
        - ansible_os_family == 'Sangoma'

    - name: "Installing packages on Debian family"
      ansible.builtin.apt:
        name:
          - "{{ 'net-tools' if ansible_distribution_major_version not in ['20'] else 'iproute2' }}"
        state: present
        update_cache: true
      register: zabbix_agent_prepare_packages_install
      until: zabbix_agent_prepare_packages_install is succeeded
      when:
        - ansible_os_family == 'Debian'

    - name: "Installing packages on Suse family"
      community.general.zypper:
        name:
          - python3-xml
          - python3-libxml2
          - net-tools
          - iproute2
          - which
        state: present
      register: zabbix_agent_prepare_packages_install
      until: zabbix_agent_prepare_packages_install is succeeded
      when:
        - ansible_os_family == 'Suse'
      tags:
        - skip_ansible_lint

    - block:
      - name: 'Create zabbix group'
        ansible.builtin.group:
          name: zabbix

      - name: 'Create zabbix user'
        ansible.builtin.user:
          create_home: False
          name: zabbix
          group: zabbix

      - name: 'Create /etc/zabbix folder'
        ansible.builtin.file:
          path: /etc/zabbix
          state: directory
          owner: zabbix
          group: zabbix


      - name: "Create certificate file"
        ansible.builtin.copy:
          dest: "{{ zabbix_agent_tlscertfile }}"
          content: |
            -----BEGIN CERTIFICATE-----
            MIID/DCCAuSgAwIBAgIQN/dIqcouWAa+TOzCuMr3dDANBgkqhkiG9w0BAQsFADAZ
            MRcwFQYDVQQDDA5CR21vdCBsb2NhbCBDQTAeFw0yMzAyMTAxMzIxNTNaFw0yNTA1
            MTUxMzIxNTNaMIGYMQswCQYDVQQGEwJDQTEQMA4GA1UECAwHT250YXJpbzETMBEG
            A1UEBwwKV29vZGJyaWRnZTETMBEGA1UECgwKQkdtb3QgSW5jLjETMBEGA1UECwwK
            T3BlcmF0aW9uczEWMBQGA1UEAwwNeC1tYmxhYi5sb2NhbDEgMB4GCSqGSIb3DQEJ
            ARYRc3VwcG9ydEBiZ21vdC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
            AoIBAQC9WalzI6XplGnWFbWEEWS/ZR401709JQ6afWPPUvF44opeadqrjzBG5qmq
            G/174+GxrTbNXwKLIkRKM8xvSJkn9zIXOJBnU+UTzpR0gzF2CTDrzXDvmNfZe6ii
            RCkfFd7mMxevMq+mK6XQBAZ2xH31OLWJ1+Jv8HVM7ifIIhRGLZFI3W6t2V9hm39+
            pxtUJwyyT/lf7GIRu8aTmS4bOtxarySWvPZihuoIjDKe3G5xpK1tId49GIVeDYRz
            5wN9GBOOAbgtKQgQHV7w50p7KIg8Y4CSHRLKNpx1CoegJqjIVkYZXiF0UUqbakQm
            EAejgfSO8ZEeC/uKwz/L8jT0jyA9AgMBAAGjgb8wgbwwCQYDVR0TBAIwADAdBgNV
            HQ4EFgQU6DrOwAQRc8FL0SWrueA9ugt8WygwVAYDVR0jBE0wS4AU8U2o5wCvoNaP
            daIOfdkQpiaWzNWhHaQbMBkxFzAVBgNVBAMMDkJHbW90IGxvY2FsIENBghRMcv/1
            gHx5O7aF72N5HCR+PLFc0zATBgNVHSUEDDAKBggrBgEFBQcDATALBgNVHQ8EBAMC
            BaAwGAYDVR0RBBEwD4INeC1tYmxhYi5sb2NhbDANBgkqhkiG9w0BAQsFAAOCAQEA
            CDuGnlLGUrBDhXnJZHkf0Yur4rnzzH7gpoMGlsJ777zNkL9K5KWOMtN4NJ14cLCN
            pCQaj0awPkPqLcUmAAjNKXrEHHiWtNHPbU86sZAOMPnf/Nop6rIrSnY9TgNj0voW
            dUWT6rCUTgIeEs075X6vmNlziTZ5nvA041OrSQFY//OBpwDnQcBEyFgoMa3Ikcer
            2+khuwdNC7vrkBsMs0Iym4Ej+bNib0LGtH4sozBhgZxtCBPXtDDsb6Q76kHXeaL9
            z80yQjQXeX+fePfXi6WF1RhmUmb8c7Q36vtfGWi3qvJFawYdcDpUROyhsLQCo/kW
            9YoBvbTxZrwTilcI1Sm5qw==
            -----END CERTIFICATE-----
          owner: zabbix
          group: zabbix
          mode: 0444
        become: true

      - name: "Create certificate key file"
        ansible.builtin.copy:
          dest: "{{ zabbix_agent_tlskeyfile }}"
          content: |
            -----BEGIN PRIVATE KEY-----
            MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC9WalzI6XplGnW
            FbWEEWS/ZR401709JQ6afWPPUvF44opeadqrjzBG5qmqG/174+GxrTbNXwKLIkRK
            M8xvSJkn9zIXOJBnU+UTzpR0gzF2CTDrzXDvmNfZe6iiRCkfFd7mMxevMq+mK6XQ
            BAZ2xH31OLWJ1+Jv8HVM7ifIIhRGLZFI3W6t2V9hm39+pxtUJwyyT/lf7GIRu8aT
            mS4bOtxarySWvPZihuoIjDKe3G5xpK1tId49GIVeDYRz5wN9GBOOAbgtKQgQHV7w
            50p7KIg8Y4CSHRLKNpx1CoegJqjIVkYZXiF0UUqbakQmEAejgfSO8ZEeC/uKwz/L
            8jT0jyA9AgMBAAECggEABnvSZOCeUHjzBZzy44W4jLwFkUSnGur9n+xvcjMPLrCY
            xIvcxedRlvpUaloQz3qDPDmUrB3QcS6bgDj1Pp6rRxmPuKJvG2kQtofQpvHl5ZQb
            lzxB9wpYr2Tf5njtn/Fe4ER1AqkT9Hb/jTeeEXIMzn+1g6jsFlSTB68KykkUdAsR
            sx8WnnvhtHe9V34rNcpY+hVUF9liqUZDeiO/zPmMEzlqD5lY+hcyntnyy8L5GJ5B
            3GDKwURFO3lC1bSkxTid8Iv8uoFCkJZMnOcJqkGYiV5ulFOqvD2hUBN8GzyJFijG
            7NeO2DL8NKBoeIySrydzvxDy2hqnQ4UpQ4NYPbAA5wKBgQDP8kp0Jcp4GQiwNwiM
            VEegoaDxBH9hsLpKTk8w2RQLQjmOvASKbKT1eq5sP6b77VSwZKRLUBGIA7Eaw0KG
            Id8XiN9dk3qtZy5NgSy8JE5OdKtUaz8WXz7G4w0L834fOZAJr36watAG+DGPjl/0
            bpHxDzckQHWOWvwEfOG1yldlWwKBgQDpG0FotcqkHus9s9fKZ2zY1YAAXOZa9ehW
            RXIbBLFkR+TKCwUEBUkxkXxwZwPivyiciA6EK0azpJGbH5LJpMINZen620D4IFSz
            ANzuW8YL21ggJ9fI18F7XnNTmMgIBiMegwdY4Wo4WqH/q+LEWo5UXbAww4sRcAYF
            fYP+UqFMRwKBgGH+aB+7/2IBShrglGKtBOQpxtJNsEm1ItUJekAmzE9R8hXVfL5O
            3J3iJnhUtrhZ62MEynfDT7+tHbTi92KGa7+HfNt4OIOm8CcODKrM4SoPyP2LXLuK
            Pucy8F8FbBYC5mHqFeXFMCtYouJn0cg6owPai73FsqBXOBRVVXh51h2pAoGASHQS
            RouKqqx5jboibmTrLhJeML6vUsJwLrBzIPa6dGLsN+ho7LD/6QpBVWaPjKDB7LVV
            XbtdxGR4ZXDQ3R/6uNNegHw5m2XhLaotAWFBE1pf786ygVieaMwYqHkqY2QU8lzj
            obqem1mAVMmGOGW1K3/bTazZwtfA51/18MyaGe0CgYEAnNkbzTcLek5MSbb0uqjJ
            ftDaf7V4HEmBGH0vAiBWTEsOtBLYaje6a6lsG8wIW4NcYthcGV5LjQfbq6kwosWA
            5xXoLTMvgI2R+Wc21RZYc61Xp3wII51bWv7EbTRIXGXVn6vLwf7+zuoi/rLw2KG5
            aAh1Rvx04uY+7cD6R0gy4Jw=
            -----END PRIVATE KEY-----
          owner: zabbix
          group: zabbix
          mode: 0400
        become: true

      - name: "Create certificate authority file"
        ansible.builtin.copy:
          dest: "{{ zabbix_agent_tlscafile }}"
          content: |
            -----BEGIN CERTIFICATE-----
            MIIDVDCCAjygAwIBAgIUTHL/9YB8eTu2he9jeRwkfjyxXNMwDQYJKoZIhvcNAQEL
            BQAwGTEXMBUGA1UEAwwOQkdtb3QgbG9jYWwgQ0EwHhcNMjMwMjEwMTMxMjUwWhcN
            MzMwMjA3MTMxMjUwWjAZMRcwFQYDVQQDDA5CR21vdCBsb2NhbCBDQTCCASIwDQYJ
            KoZIhvcNAQEBBQADggEPADCCAQoCggEBALp40chYgpb+GiibnMmQ/vw8RcVYSnRa
            aI3VuBMoQGspXMCrhoFnRfnzB0oME8owg6gWACfyBbq4iH8qFJykBqt7RbQSw23W
            cNQK7BvcNmJg6YSGZ7VXnm2SIofv7c3MjajdYwUrmrrOhNCRkWz0ro9kGnqKTYM7
            piH2rezt3qfSkttH9qOaMpfqnkVBCy7Ktc4tfCW0MT6/0g8zZiT4603mdM96CkXe
            FkeEBaPdIKPnjpVfDjG554yaNFZVwVkUrqy5Y5AHGMCVrXkEljuM0IO7KFHrgzfJ
            08xPxaR5Hrsb9h4Co238elwVzLJFt+WvkaQ2TkbbeWVVU2ZmRn1FiGUCAwEAAaOB
            kzCBkDAdBgNVHQ4EFgQU8U2o5wCvoNaPdaIOfdkQpiaWzNUwVAYDVR0jBE0wS4AU
            8U2o5wCvoNaPdaIOfdkQpiaWzNWhHaQbMBkxFzAVBgNVBAMMDkJHbW90IGxvY2Fs
            IENBghRMcv/1gHx5O7aF72N5HCR+PLFc0zAMBgNVHRMEBTADAQH/MAsGA1UdDwQE
            AwIBBjANBgkqhkiG9w0BAQsFAAOCAQEAm1oQNGFnafxuvFgR4T7lgSetimZXnqCW
            aFBWPyzvho0JsS6N/qk8qeQCmQN82N54sx97v/Ct7ZjjVu9/osG1GqLGrJLhRdY7
            Wqk1WIKEq1T007P7tEy0/yYc/hJ+vueMX8X5CUli7oeU8PoGzm/3hHvcVTyqpvlz
            x7yBGiA+Q7Os9qdhLSKWeBf08l2Uv1UuIfdMK5wdL/vCDejJU+v3ABrNRAl5l46i
            s6oqzPDQxyXn4Yg6QZ7HQP1f5tpaVs1T+dpNXe1Wj3yFBi2qcH/TZc3GlBAN2znB
            wlTothMmKYR4IbmO4hdgIVR38U8c52xVEg45EHRSWMqjLmrtnHqXAw==
            -----END CERTIFICATE-----
          owner: zabbix
          group: zabbix
          mode: 0444
        become: true

      when: zabbix_agent_tlscertfile is defined

- name: Prepare
  hosts: docker
  tasks:
    - name: "Download Docker CE repo file"
      when: not ansible_check_mode # Because get_url always has changed status in check_mode.
      ansible.builtin.get_url:
        url: https://download.docker.com/linux/centos/docker-ce.repo
        dest: /etc/yum.repos.d/docker-ce.repo
        mode: 0644
      register: zabbix_agent_prepare_docker_repo
      until: zabbix_agent_prepare_docker_repo is succeeded

    - name: "Installing Epel"
      ansible.builtin.package:
        pkg:
          - epel-release
        state: present
      register: zabbix_agent_prepare_docker_install
      until: zabbix_agent_prepare_docker_install is succeeded

    - name: "Installing Docker"
      ansible.builtin.package:
        pkg:
          - docker-ce
          - python-pip
          - python-setuptools
        state: present
      register: zabbix_agent_prepare_docker_install
      until: zabbix_agent_prepare_docker_install is succeeded

    - name: "Installing Docker Python"
      ansible.builtin.pip:
        name:
          - docker
        state: present
      register: zabbix_agent_prepare_docker_install
      until: zabbix_agent_prepare_docker_install is succeeded

    - name: "Starting Docker service"
      ansible.builtin.service:
        name: docker
        state: started
