--- - name: Backup PostgreSQL and create cron hosts: postgres_servers become: true vars: backup_dir: "{{ backup_dir }}" tasks: - name: Create backup directory file: path: "{{ backup_dir }}" state: directory owner: postgres group: postgres mode: '0755' - name: PostgreSQL database backup become_user: postgres command: > pg_dump -U {{ postgres_user }} -F c -f "{{ backup_dir }}/db_backup_{{ postgres_db }}_{{ ansible_date_time.iso8601 }}.sql" {{ postgres_db }} environment: PGPASSWORD: "{{ postgres_password }}" - name: Create cron backup script cron: name: "PostgreSQL hourly backup" user: postgres minute: "0" hour: "*" job: 'pg_dump -U {{ postgres_user }} -F c {{ postgres_db }} > {{ backup_dir }}/db_backup_{{ postgres_db }}_$(date +\%F-\%H-\%M).sql' environment: PGPASSWORD: "{{ postgres_password }}"