From 9b8839f0ab8470471cf36d6d56cfa4c17bda6422 Mon Sep 17 00:00:00 2001 From: AndrewGodGivens <107873163+AndrewGodGivens@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:12:32 +0300 Subject: [PATCH] Update run-iptables.yml (#71) * Update run-iptables.yml * add playbook for backup maria_db * add playbook for creation postgres replica * add description --------- Co-authored-by: Andew Bogdanov --- playbooks/backups/run-mariadb-backup.yml | 25 ++++++ playbooks/configuration/run-iptables.yml | 4 +- playbooks/databases/run-postgres-replica.yml | 95 ++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100755 playbooks/backups/run-mariadb-backup.yml create mode 100755 playbooks/databases/run-postgres-replica.yml diff --git a/playbooks/backups/run-mariadb-backup.yml b/playbooks/backups/run-mariadb-backup.yml new file mode 100755 index 00000000..3145c3be --- /dev/null +++ b/playbooks/backups/run-mariadb-backup.yml @@ -0,0 +1,25 @@ +#!/usr/bin/env -S ansible-playbook -e @vars/extra.yaml +# +# Playbook to make cron job to create bakcup for mariadb +# executable script: +# +# > ./run-mariadb-backup.yml.yml +# +--- +- name: Configure target servers + hosts: mariadb_servers + become: yes + become_user: root + + tasks: + - name: Ensure backup db + cron: + name: "Backup mariadb" + user: "root" + minute: "0" + hour: "2" + job: "/usr/bin/docker exec -i {{ mariadb_backup_container_name }} oom-mariabackup create -i {{ number_of_inc_backups }} -r {{ number_of_retain_backups }} >> {{ mariadb_backup_logfile }} 2>&1" + state: present + tags: + - backup + - crontab diff --git a/playbooks/configuration/run-iptables.yml b/playbooks/configuration/run-iptables.yml index e6ac5535..637d72d8 100755 --- a/playbooks/configuration/run-iptables.yml +++ b/playbooks/configuration/run-iptables.yml @@ -13,6 +13,7 @@ tasks: - name: Restart docker + become: yes systemd: name: docker state: restarted @@ -20,4 +21,5 @@ - (iptables.flush_all is defined and iptables.flush_all) or (not iptables.flush_all is defined and iptables_defaults.flush_all) tags: - iptables - failed_when: false + register: result_restart_docker + failed_when: "result_restart_docker is failed and 'Could not find the requested service' not in result_restart_docker.msg" diff --git a/playbooks/databases/run-postgres-replica.yml b/playbooks/databases/run-postgres-replica.yml new file mode 100755 index 00000000..70af6d89 --- /dev/null +++ b/playbooks/databases/run-postgres-replica.yml @@ -0,0 +1,95 @@ +#!/usr/bin/env -S ansible-playbook -e @vars/extra.yaml +# +# +# Playbook for creation postgres replica. Warning!!! Playbook is not idempotent! Use only for first creation! +# executable script: +# +# > ./run-postgres-replica.yml +# +--- +- name: Install postgres-replica container + hosts: postgresql_servers + become: yes + become_user: root + vars: + replica_backup_command: "docker exec -i {{ replica_container_name }} pg_basebackup --host={{ replica_origin_ip }} --port={{ replica_origin_port }} --username={{ vault_replica_username }} --pgdata=/tmp/ --wal-method=stream --write-recovery-conf" + tasks: + - name: Run postgres-replica container + docker_compose: + state: present + project_name: "{{ replica_project_name }}" + definition: + version: '2' + services: + cadvisor: + image: "{{ replica_image }}" + container_name: "{{ replica_container_name }}" + volumes: + - "{{ replica_host_pgdata }}:/tmp" + restart: always + environment: + POSTGRES_USER: "{{ replica_postgres_user }}" + POSTGRES_PASSWORD: "{{ REPLICA_POSTGRES_PASSWORD }}" + POSTGRES_DB: "{{ replica_postgres_db }}" + PGDATA: "/patroni_db/postgresql/data/" + ARCHIVE_MODE: 'OFF' + PGUSER: "{{ replica_pguser }}" + PGPASSWORD: "{{ vault_replica_password }}" + PGHOST: '/var/run/postgresql' + PGDATABASE: "{{ replica_pgdatabase }}" + RECOVERY_WALG: 'false' + + - name: Create backup for replication + ansible.builtin.shell: + cmd: "{{ replica_backup_command }}" + + - name: Remove container + community.docker.docker_container: + name: "{{ replica_container_name }}" + state: absent + + - name: Creating a file for adding to conf + copy: + dest: "{{ replica_host_pgdata }}/00server.conf" + content: | + primary_slot_name = '' + archive_mode = always + archive_command = 'archive.sh wal-push %p' + archive_timeout = 30 + + - name: Create backup for replication + ansible.builtin.shell: + cmd: echo "include '00server.conf'" >> {{ replica_host_pgdata }}/postgresql.conf + + - name: Run postgres-replica container + docker_compose: + state: present + project_name: "{{ replica_project_name }}" + definition: + version: '2' + services: + cadvisor: + image: "registry.nalitek.com/library/postgres_with_walg_replica:14.1" + container_name: "{{ replica_container_name }}" + volumes: + - "{{ replica_host_pgdata }}:/patroni_db/postgresql/data/" + restart: always + environment: + POSTGRES_USER: "{{ replica_postgres_user }}" + POSTGRES_PASSWORD: "{{ REPLICA_POSTGRES_PASSWORD }}" + POSTGRES_DB: "{{ replica_postgres_db }}" + PGDATA: "/patroni_db/postgresql/data/" + ARCHIVE_MODE: 'ON' + ARCHIVE_TIMEOUT: 30 + AWS_ACCESS_KEY_ID: "{{ REPLICA_AWS_ACCESS_KEY_ID }}" + AWS_REGION: "{{ replica_aws_region }}" + AWS_SECRET_ACCESS_KEY: "{{ REPLICA_AWS_SECRET_ACCESS_KEY }}" + PGUSER: "{{ replica_pguser }}" + PGPASSWORD: "{{ REPLICA_POSTGRES_PASSWORD }}" + WALG_S3_PREFIX: "{{ replica_walg_s3_prefix }}" + PGHOST: "{{ replica_pg_host }}" + PGDATABASE: "{{ replica_pgdatabase }}" + AWS_ENDPOINT: "{{ replica_aws_endpoint }}" + TRIGGER_FILE: '/trigger' + RECOVERY_WALG: 'false' + APPRISE_TARGET: "{{ REPLICA_APPRISE_TARGET }}"