main.yml (2585B)
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_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 # Check if the local backup file exists 50 - name: Check if postgres backup file exists locally 51 local_action: 52 module: stat 53 follow: yes 54 path: "{{ role_path }}/files/postgres-backup.sql.gz" 55 register: backup_file_status 56 57 - name: Set local backup existence fact 58 ansible.builtin.set_fact: 59 local_backup_exists: "{{ backup_file_status.stat.exists | default(false) }}" 60 61 - name: Fail if trying to import backup and versioning schema exists 62 fail: msg="Backup for import provided, but _v schema exists on target host" 63 when: 64 - not DISABLE_RESTORE_BACKUP 65 - versioning_schema_exists | default(false) | bool 66 - local_backup_exists | bool 67 68 # Note: the postgres-backup.sql.gz is a symbolic link in Git. 69 # The target of that symbolic link is created via the 'restore.sh' script. 70 - name: Upload database backup file to server if restoring from backup 71 copy: 72 src: postgres-backup.sql.gz 73 dest: /tmp/postgres-backup.sql.gz 74 owner: postgres 75 group: postgres 76 mode: "0400" 77 when: 78 - local_backup_exists | bool 79 80 - name: Restore PostgreSQL database from backup 81 become: true 82 become_user: postgres 83 shell: "gunzip -c /tmp/postgres-backup.sql.gz | psql -X -d postgres" 84 when: 85 - local_backup_exists | bool 86 87 - name: Remove backup from server (delete file) 88 ansible.builtin.file: 89 path: /tmp/postgres-backup.sql.gz 90 state: absent