# Ansible Failover of SQL Database

**URL:** <https://forum.ansible.com/t/ansible-failover-of-sql-database/26791>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [February 19, 2018, 8:04pm UTC](https://forum.ansible.com/t/ansible-failover-of-sql-database/26791 "2018-02-19T20:04:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darren\_Rucker](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@Darren\_Rucker](https://forum.ansible.com/u/Darren_Rucker)\
**Post date:** [February 19, 2018, 8:04pm UTC](https://forum.ansible.com/t/ansible-failover-of-sql-database/26791/1 "2018-02-19T20:04:17Z")

</div>

Hi folks. I am pretty new to Ansible though have been assigned a project to failover a Microsoft SQL database from a primary data center to a DR site. What I an uncertain of is how to structure that’s below in Ansible and execute the sql scripts remotely. Would anyone be able to help here? Would anyone have a module/modules to use to accomplish all of this?

//Stop application  
-stop iis on servers a, b and c  
-stop windows services on servers d, e, and f  
-stop scheduled tasks on servers d, e, and f

//No pending transactions replicating  
-execute a SQL script(to check if any replication is in flight) on DB server G  
-if records in queue =0, continue. If query returns anything other than 0, wait 30 seconds and rerun the query. Needs to be 0 before continuing

//Stop log reader agent and drop publications and subscriptions  
-execute SQL script on server G to stop log reader agent  
-execute SQL script on server G to drop replication publications  
-execute SQL script on server H to drop all replication subscriptions  
-execute SQL script on server I to drop all replication subscriptions  
-execute SQL script on server J to drop all replication subscriptions

//Failover Databases  
-execute SQL script to failover databases from primary to dr

Configure replication  
-execute SQL script on server G to create replication publications  
-execute SQL script on server H to create all replication subscriptions  
-execute SQL script on server I to create all replication subscriptions  
-execute SQL script on server J to create all replication subscriptions

Thank you in advance.
