---
- name: Create
  hosts: localhost
  connection: local

  gather_facts: false

  pre_tasks:
    - name: Create network
      community.docker.docker_network:
        name: zabbix

  tasks:
    - name: Create OS container(s)
      community.docker.docker_container:
        state: started
        recreate: true
        name: "{{ item.name }}"
        image: "{{ item.image }}"
        privileged: "{{ item.privileged | default(omit) }}"
        command: "{{ item.command | default(omit) }}"
        networks: "{{ item.networks | default(omit) }}"
        volumes: "{{ item.volumes | default(omit) }}"
        cgroupns_mode: "{{ item.cgroupns_mode | default(omit) }}"
      loop: "{{ molecule_yml.platforms }}"

    - name: Create database container(s)
      community.docker.docker_container:
        name: "{{ item.name }}-db"
        image: "{{ _container[_db_type].image }}"
        state: started
        recreate: true
        networks:
          - name: zabbix
        env: "{{ _container[_db_type].env }}"
      loop: "{{ molecule_yml.platforms }}"
      when: item.groups | intersect(_database_groups)
      vars:
        _database_groups: ['mysql', 'pgsql']
        _db_type: "{{ item.groups | intersect(_database_groups) | first }}"
        _container:
          mysql:
            image: "mysql:8.0"
            env: { MYSQL_ROOT_PASSWORD: changeme }
          pgsql:
            image: "timescale/timescaledb:latest-pg13"
            env: { POSTGRES_PASSWORD: changeme }
