main.yml (3060B)
1 --- 2 # Database role 3 4 - name: Install PostgreSQL on Debian/Ubuntu 5 apt: 6 name: postgresql 7 state: present 8 update_cache: true 9 notify: 10 - Restart postgresql 11 when: ansible_facts["os_family"] == 'Debian' 12 13 - name: Ensure PostgreSQL is started and enabled 14 systemd: 15 name: postgresql 16 state: started 17 enabled: true 18 19 - name: Collect database information 20 become: true 21 become_user: postgres 22 community.postgresql.postgresql_info: 23 filter: 24 - "databases*" 25 register: database_info 26 27 - name: Check if exchange database already exists 28 become: true 29 become_user: postgres 30 ansible.builtin.set_fact: 31 exchange_db_exists: "{{ 'taler-exchange' in database_info.databases.keys() }}" 32 33 - name: Check if versioning schema exists 34 become: true 35 become_user: postgres 36 community.postgresql.postgresql_query: 37 login_user: postgres 38 db: taler-exchange 39 query: 40 SELECT schema_name FROM information_schema.schemata WHERE schema_name = '_v'; 41 register: schema_check 42 when: exchange_db_exists | bool 43 44 - name: Set versioning schema existence fact 45 ansible.builtin.set_fact: 46 versioning_schema_exists: "{{ schema_check.rowcount | default(0) > 0 }}" 47 when: exchange_db_exists | bool 48 49 - name: Check if postgres backup file exists locally 50 ansible.builtin.stat: 51 path: "{{ role_path }}/files/postgres-backup.sql.gz" 52 follow: yes 53 delegate_to: localhost 54 register: backup_file_status 55 56 - name: Set local backup existence fact 57 ansible.builtin.set_fact: 58 local_backup_exists: "{{ backup_file_status.stat.exists | default(false) }}" 59 60 - name: Fail if trying to import backup and versioning schema exists 61 fail: msg="Backup for import provided, but _v schema exists on target host" 62 when: 63 - enable_restore_backup 64 - versioning_schema_exists | default(false) | bool 65 - local_backup_exists | bool 66 67 # Without this the deploy would silently continue onto an empty database. 68 - name: Fail if a restore was requested but no backup is available 69 fail: 70 msg: >- 71 enable_restore_backup is set but 72 {{ role_path }}/files/postgres-backup.sql.gz does not resolve to a 73 file. Fetch the backup with restore.sh first. 74 when: 75 - enable_restore_backup 76 - not (local_backup_exists | bool) 77 - not (exchange_db_exists | bool) 78 79 # Note: the postgres-backup.sql.gz is a symbolic link in Git. 80 # The target of that symbolic link is created via the 'restore.sh' script. 81 - name: Upload database backup file to server if restoring from backup 82 copy: 83 src: postgres-backup.sql.gz 84 dest: /tmp/postgres-backup.sql.gz 85 owner: postgres 86 group: postgres 87 mode: "0400" 88 when: 89 - enable_restore_backup 90 - local_backup_exists | bool 91 92 - name: Restore PostgreSQL database from backup 93 become: true 94 become_user: postgres 95 shell: "gunzip -c /tmp/postgres-backup.sql.gz | psql -X -d postgres" 96 when: 97 - enable_restore_backup 98 - local_backup_exists | bool 99 100 - name: Remove backup from server (delete file) 101 ansible.builtin.file: 102 path: /tmp/postgres-backup.sql.gz 103 state: absent