- name: Deploy Zabbix Stack hosts: zabbix become: yes tasks: - name: Check if .env already exists ansible.builtin.stat: path: /opt/stacks/zabbix/.env register: env_file - name: Generate passwords (only if .env doesn't exist) ansible.builtin.set_fact: postgres_password: "{{ lookup('ansible.builtin.password', '/dev/null', length=32, chars=['ascii_letters', 'digits']) }}" grafana_secret: "{{ lookup('ansible.builtin.password', '/dev/null', length=32, chars=['ascii_letters', 'digits']) }}" when: not env_file.stat.exists - name: Create stack directory ansible.builtin.file: path: /opt/stacks/zabbix state: directory mode: '0755' recurse: yes - name: Write .env file (only if it didn't exist) ansible.builtin.copy: dest: /opt/stacks/zabbix/.env mode: '0600' content: | POSTGRES_USER={{ postgres_user }} POSTGRES_PASSWORD={{ postgres_password }} POSTGRES_DB={{ postgres_db }} GRAFANA_USER={{ grafana_user }} GRAFANA_SECRET={{ grafana_secret }} TZ={{ tz }} when: not env_file.stat.exists - name: Read existing .env file ansible.builtin.slurp: src: /opt/stacks/zabbix/.env register: env_contents when: env_file.stat.exists - name: Deploy docker-compose.yml ansible.builtin.copy: dest: /opt/stacks/zabbix/compose.yml content: | services: zabbix-server: image: zabbix/zabbix-server-pgsql:alpine-7.4-latest container_name: zabbix-server restart: unless-stopped env_file: .env environment: DB_SERVER_HOST: zabbix-postgres ZBX_CACHESIZE: 512M ZBX_STARTPOLLERS: 20 ZBX_STARTPINGERS: 10 depends_on: - zabbix-postgres volumes: - zabbix-server:/var/lib/zabbix networks: - zabbix_internal zabbix-frontend: image: zabbix/zabbix-web-nginx-pgsql:alpine-7.4-latest container_name: zabbix-frontend restart: unless-stopped env_file: .env environment: DB_SERVER_HOST: zabbix-postgres ZBX_SERVER_HOST: zabbix-server depends_on: - zabbix-server networks: - zabbix_internal - pangolin zabbix-postgres: image: postgres:17-alpine container_name: zabbix-postgres restart: unless-stopped env_file: .env volumes: - postgres:/var/lib/postgresql/data networks: - zabbix_internal grafana: image: grafana/grafana:latest container_name: grafana restart: unless-stopped env_file: .env environment: GF_INSTALL_PLUGINS: alexanderzobnin-zabbix-app user: "472" volumes: - grafana:/var/lib/grafana networks: - zabbix_internal - pangolin volumes: postgres: zabbix-server: grafana: networks: zabbix_internal: driver: bridge pangolin: external: true - name: Pull images community.docker.docker_compose_v2_pull: project_src: /opt/stacks/zabbix - name: Deploy stack community.docker.docker_compose_v2: project_src: /opt/stacks/zabbix state: present