- name: Create Docker network for PostgreSQL docker_network: name: "{{ postgres_network_name }}" state: present ipam_config: - subnet: "{{ postgres_subnet }}" when: run_once_docker_postgres is not defined - name: Install PostgreSQL docker_container: name: "{{ postgres_name }}" image: "{{ postgres_image }}:{{ postgres_version }}" detach: yes env: POSTGRES_PASSWORD: "{{ postgres_password }}" POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=C" # Necessary for web-app-matrix networks: - name: "{{ postgres_network_name }}" published_ports: - "127.0.0.1:{{ postgres_port }}:5432" volumes: - "{{ postgres_volume }}:/var/lib/postgresql/data" restart_policy: "{{ docker_restart_policy }}" healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 10s timeout: 5s retries: 5 start_period: 30s register: setup_postgres_container_result when: run_once_docker_postgres is not defined - name: Wait for Postgres inside the container shell: "docker exec {{ postgres_name }} pg_isready -U postgres" register: pg_ready until: pg_ready.rc == 0 retries: 30 delay: 5 when: - setup_postgres_container_result is defined - setup_postgres_container_result.changed - run_once_docker_postgres is not defined - name: install python-psycopg2 pacman: name: python-psycopg2 state: present when: run_once_docker_postgres is not defined - name: "Initialize database for '{{ database_name }}'" include_tasks: init.yml when: "{{ postgres_init }}" - name: Run the docker_postgres tasks once set_fact: run_once_docker_postgres: true when: run_once_docker_postgres is not defined