--- - name: Postgres hosts: all become: yes vars_files: - vars.yml pre_tasks: - name: Debug Environnement debug: msg: "ansible_os_family : {{ansible_os_family}} ; ansible_distribution : {{ansible_distribution}} ; ansible_distribution_major_version : {{ansible_distribution_major_version}} ; ansible_distribution_release : {{ansible_distribution_release}}" - name: Debian postgresql server installation block: - name: Update repository ansible.builtin.shell: apt update - name: PostgreSQL installation ansible.builtin.apt: name: "{{item}}" state: present loop: - postgresql - postgresql-contrib - python3-psycopg2 when: (ansible_os_family == "Debian") - name: Redhat postgresql server installation block: - name: Update repository ansible.builtin.shell: dnf update -y - name: PostgreSQL installation ansible.builtin.dnf: name: "{{item}}" state: present loop: - postgresql - postgresql-server - python3-psycopg2 # - python3-pip # - acl # Only for Docker containers when: (ansible_os_family == "RedHat") - name: Suse postgresql server installation zypper: name: "{{item}}" state: present loop: - postgresql - postgresql-server - python3-psycopg2 # - python3-pip # - acl # Only for Docker containers when: (ansible_os_family == "Suse") - name: Start PostgreSQL ansible.builtin.service: name: postgresql state: started enabled: yes - name: Create {{ db_name }} database become_user: postgres community.postgresql.postgresql_db: name: "{{ db_name }}" state: present - name: Create table into {{ db_name }} database become_user: postgres community.postgresql.postgresql_query: db: "{{ db_name }}" query: CREATE TABLE IF NOT EXISTS mytable (index_id INT PRIMARY KEY, field1 VARCHAR(64)) - name: View tables become_user: postgres community.postgresql.postgresql_query: db: "{{ db_name }}" query: SELECT table_name,table_catalog,table_schema,table_type FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema') ORDER BY table_name; - name: Create transfert directory if it does not exist ansible.builtin.file: path: /var/lib/postgresql/FILES state: directory owner: postgres group: postgres mode: '0770' - name: Copy Script.sql ansible.builtin.copy: src: Script.sql dest: "/var/lib/postgresql/FILES/" owner: postgres # Comment if duplicated keys - name: Insert row into table become_user: postgres community.postgresql.postgresql_script: db: "{{ db_name }}" path: "/var/lib/postgresql/FILES/Script.sql" - name: View table data become_user: postgres community.postgresql.postgresql_query: db: "{{ db_name }}" query: SELECT * FROM mytable; post_tasks: - name: Dump database block: - name: Set timestamp set_fact: now: "{{ lookup('pipe', 'date +%Y%m%d-%H:%M:%S') }}" tags: "dump" - name: Back up in progres... become_user: postgres community.postgresql.postgresql_db: state: dump name: "{{ db_name }}" target: "/var/lib/postgresql/FILES/{{ db_name }}_{{ now }}.dump.gz" tags: "dump" - name: Restore the database block: - name: Find files in the target directory become_user: postgres find: paths: "/var/lib/postgresql/FILES" file_type: file age: 0 register: found_files tags: "restore" - name: Sort files by modification time and get the latest set_fact: latest_file: "{{ found_files.files | sort(attribute='mtime') | last }}" tags: "restore" - name: Display the latest file debug: msg: "Latest file: {{ latest_file.path }}" tags: "restore" - name: Restoration in progress... become_user: postgres postgresql_db: state: restore name: "{{db_name}}" target: "{{ latest_file.path }}" # target: "/var/lib/postgresql/FILES/{{ db_name }}_{{ now }}.dump.gz" tags: "restore"