Hello Stefan,
I am trying to create a playbook to perform the three below mentioned tasks which is to accomplish the database refresh. First two tasks (Database backup and Backup copy to destination server) are running fine but for the third one which is to restore the Database I need to change the host server as windows server 2. So I am not sure how I can achieve it. The playbook I am using is below:
1) Taking backup on remote windows server 1.
2) Copying that backup to another remote windows server 2.
3) Restoring that backup to remote windows server 2.
---
- hosts: SQLSERVER
vars:
DbName_Source:
ansible_become_user: Ansible
ansible_become_pass: xyz@123
DbName_Destination:
vars_prompt:
- name: DbName_Source
private: no
prompt: Source Database
default:
- name: DbName_Destination
private: no
prompt: destination Database
default:
tasks:
- name: DB Backup
win_shell: |
$Backup = "BACKUP DATABASE {{ DbName_Source }} TO DISK = 'E:\Backup\{{ DbName_Source }}-Databasebackup.bak'"
Invoke-SqlCmd -Query $Backup
- name: Sync SQLSERVER to SQLSERVER2
become: yes
become_method: runas
become_flags: logon_type=new_credentials logon_flags=netcredentials_only
win_copy:
src: E:\BACKUP\
dest: \\xx.xxx.xxx.xx\E$\BACKUP\
remote_src: yes
- name: DB Restore
win_shell: |
$Restore = "RESTORE DATABASE {{ DbName_Destination }} FROM
DISK = N'E:\BACKUP\{{ DbName_Source }}-Databasebackup.bak'
WITH FILE = 1,
MOVE N'{{ DbName_Source }}' TO N'D:\SQLDATA\MSSQL15.MSSQLSERVER\MSSQL\DATA\{{ DbName_Destination }}.mdf',
MOVE N'{{ DbName_Source }}_log' TO N'L:\SQLLOG\{{ DbName_Destination }}_log.ldf',
NOUNLOAD, REPLACE, STATS = 5"
Invoke-SqlCmd -Query $Restore