--- # Attach this app's network to the shared Postgres unit. # # Postgres knows nothing about its clients, exactly like Caddy: a service joins # by dropping 50-nextcloud.conf and the shared 10-network-aliases.conf next to # postgres.container. The canonical way to do that is the shared include below — # it writes the same bytes every service writes, asserts that nextcloud.network # already exists, and notifies the "Restart Postgres" handler defined in this # role. # # This runs in the roles phase, which is after the register include in # playbook.yml's pre_tasks has created nextcloud.network. - name: Attach Nextcloud to Postgres ansible.builtin.include_tasks: "{{ playbook_dir }}/../postgres/roles/attach/tasks/main.yaml" # On a first attach both drop-ins are new, so Postgres has to be recreated to # join nextcloud_net — and that restart is a *handler*, which by default runs at # the end of the play, after the nextcloud role has already started the app. The # app would then come up before `db` resolves, fail its own readiness check, and # fail the play on a machine where nothing is actually wrong. # # Flushing applies the new drop-in before the app starts. It also flushes the # "Restart Caddy" that the register pre_task queued, which is what attaches Caddy # to nextcloud_net — also wanted before the app is expected to serve. On later # runs nothing is pending and this is a no-op. - name: Apply the Postgres network change before starting Nextcloud ansible.builtin.meta: flush_handlers # The database itself. Postgres is a shared unit that ships one default database, # and nothing else in this repo knows what this app's database is called — so # without this, a first run on a fresh host reaches the setup wizard (or the # unattended install) only to fail on "database {{ pg_database }} does not exist". # Later runs find it and do nothing. # # psql runs inside the Postgres container rather than over the app network, so the # only thing this needs is a cluster that answers. The retry loop is there because # the flush above may have just restarted it. - name: Check whether the {{ pg_database }} database exists ansible.builtin.command: >- podman exec -e PGPASSWORD={{ pg_password }} {{ postgres_container }} psql -U {{ pg_user }} -d postgres -tAc "select datname from pg_database where datname = '{{ pg_database }}'" register: nextcloud_db until: nextcloud_db.rc == 0 retries: 12 delay: 5 changed_when: false failed_when: false - name: Fail if the shared Postgres is not reachable ansible.builtin.assert: that: nextcloud_db.rc == 0 fail_msg: >- The shared Postgres container ({{ postgres_container }}) did not answer. Run postgres/playbook.yml on this host first: this playbook attaches to that cluster and creates its own database in it. Postgres said: {{ nextcloud_db.stderr | default('') | trim }} - name: Create the {{ pg_database }} database ansible.builtin.command: >- podman exec -e PGPASSWORD={{ pg_password }} {{ postgres_container }} createdb -U {{ pg_user }} -O {{ pg_user }} {{ pg_database }} when: nextcloud_db.stdout | trim | length == 0