From 8533b8db862b5b514987e0e25ff2f32519cb5b24 Mon Sep 17 00:00:00 2001 From: dissolution <50195845+SDV109@users.noreply.github.com> Date: Fri, 27 Dec 2024 16:49:42 +0700 Subject: [PATCH] Release 1.0 --- backup.yml | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++---- inventory | 5 ++-- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/backup.yml b/backup.yml index 6196631..c2b6e6e 100644 --- a/backup.yml +++ b/backup.yml @@ -38,10 +38,69 @@ tags: always tasks: - - name: Get ansible_host IP - debug: - msg: "IP Address (ansible_host): {{ ansible_host }}" - name: Get default IPv4 IP address debug: - msg: "Default IPv4 IP Address: {{ ansible_default_ipv4.address }}" \ No newline at end of file + msg: "Default IPv4 IP Address: {{ ansible_default_ipv4.address }}" + + - name: Run patronictl list + command: patronictl -c /etc/patroni/patroni.yml list + register: patronictl_output + ignore_errors: true + + - name: patronictl_output + debug: + msg: "{{ patronictl_output }}" + + - name: Fail if patronictl command failed + fail: + msg: "Failed to get Patroni list using patronictl" + when: patronictl_output.rc != 0 + + - name: Parse patronictl output and set facts + set_fact: + patroni_members: "{{ patronictl_output.stdout_lines | select('search', '\\|.*running.*\\|') | map('regex_replace', '^\\|\\s*(.*?)\\s*\\|\\s*(.*?)\\s*\\|.*$', '\\1:\\2') | list }}" + patroni_leader_name: "{{ patronictl_output.stdout_lines | select('search', '\\|.*Leader.*\\|') | map('regex_replace', '^\\|\\s*(.*?)\\s*\\|.*$', '\\1') | first | default('No leader found') }}" + when: patronictl_output.rc == 0 + + - name: Parse patronictl output and set facts + set_fact: + patroni_leader_ip: "{{ patroni_members | select('search', '^' + patroni_leader_name + ':') | map('regex_replace', '^.*?:(.*)$', '\\1') | first | default('No leader ip found') }}" + when: patronictl_output.rc == 0 and patroni_leader_name != 'No leader found' + + - name: Print the master node + debug: + msg: "The current master node is: {{ patroni_leader_name }} {{ patroni_leader_ip }}" + when: patronictl_output.rc == 0 + + - name: Create directory for dumps on leader + file: + path: /var/lib/postgresql/dump + state: directory + owner: postgres + group: postgres + mode: '0755' + delegate_to: "{{ patroni_leader_ip }}" + run_once: true + become: yes + become_user: postgres + when: patroni_leader_ip != 'No leader ip found' + + - name: Run pg_dump on leader + command: "pg_dump -U postgres -d {{ database_name }} -f //var/lib/postgresql/dump/{{ database_name }}.dump" + register: pg_dump_output + delegate_to: "{{ patroni_leader_ip }}" + run_once: true + become: yes + become_user: postgres + when: patroni_leader_ip != 'No leader ip found' + + - name: Fail if pg_dump failed + fail: + msg: "Failed to run pg_dump" + when: pg_dump_output.rc != 0 and patroni_leader_ip != 'No leader ip found' + + - name: Print backup information + debug: + msg: "Backup was created on host {{ patroni_leader_ip }}, and saved to file /var/lib/postgresq/dump/{{ database_name }}.dump" + when: patroni_leader_ip != 'No leader ip found' \ No newline at end of file diff --git a/inventory b/inventory index 5787945..a89f061 100644 --- a/inventory +++ b/inventory @@ -1,9 +1,8 @@ [hosts_backup] -192.168.62.211 hostname=test1 postgresql_exists=false +10.128.67.200 database_name=dimon [all:vars] ansible_connection='ssh' ansible_ssh_port='22' ansible_user='root' -ansible_ssh_pass='root' # "sshpass" package is required for use "ansible_ssh_pass" -#ansible_ssh_private_key_file= \ No newline at end of file +ansible_ssh_pass='root' \ No newline at end of file