It is possible to backup server using Ansible. I'm new user of Ansible so I'm not sure if it is a good practice or not.
If you want to backup databases you can use mysql_db module.
tasks:
- name: backup db server
mysql_db: name={{item}} state=dump target=/{{item}}-{{ansible_date_time.iso8601}}.sql
with_items: db_name
then you can use fetch or synchronize module for transferring those files to local machine. With fetch
module it is not possible to download multiple files as it doesn't support wildcard characters and folder download. Creating one tar file is an option to overcome this problem.
You can also use scp
to download backup files from remote instance.
- name: Downloading backup files from remote machine
command: scp -i {{ansible_ssh_private_key_file}} {{remote_user}}@{{host}}:/backup/* {{backup_folder}}
delegate_to: 127.0.0.1