Getting Started with Ansible and Windows Automation Part 2
Now that we have out basic WinRM setup configured and Ansible it able to communicate with our Windows server, it's time to start automation. In this example we will use Ansible to install the SSMS ( SQL SERVER MANAGEMENT STUDIO ver 18.4) on our windows host
- First thing to do is to download the SSMS.exe from Microsoft on our Ansible server
Now we can start to work on our Ansible playbook, which will do the following:
- Copy the SSMS application to the Windows host to test that Ansible and windows are playing together
- Add the refresh task to the playbook which will call the powershell script to refresh the DR SQL database
- Create the ssms.yaml file and start by coping the SSMS Application to the Windows host
- Run the Ansible playbook
- Check to make sure the file has been copied across
Now Lets configure Ansible to call the powershell script which will automatically refresh a Target SQL Server database from a snapshot running on a Source SQL server database.
In this example I have both the Source and target running on the same Virtual Machine on ESX, the database being refreshed is called testdb
Lets have a look at the Ansible "ssms.yaml" playbook and the powershell script.
ssms.yaml
---
- hosts: winsql
gather_facts: no
tasks:
- name: Copy SSMS Application
win_copy:
src: SSMS-Setup-ENU.exe
dest: c:\\Temp\\
- name: Run Database Refresh Powershell
win_command: powershell.exe -ExecutionPolicy ByPass C:/Users/Administrator/Desktop/snap.ps1
The task called "Run Database Refresh Powershell" uses the win_command Ansible module to call the remote Powershell script called C:/Users/Administrator/Desktop/snap.ps1
The following file called win.yaml is used to setup the Ansible groups variable for WinRm to connect to the windows server
---
ansible_user: Administrator
ansible_password: P@ssw0rd!
ansible_connection: winrm
ansible_winrm_transport: basic
ansible_winrm_port: 5985
The basic powershell script below is used to refresh the DR database called MSSQLDR from the source database MSSQLPROD
- First we add some new data to the MSSQLPROD database, lets create a new table called "cbt10"
- Right Click on the table and select "New Query" type in the SQL quere and click on "Execute"
- create table cbt10 (cbt10 varchar(20))
We can see the new table called dbo.cbt10 created successfully
Lets confirm that the MSQLDR database does not have the new table
Now lets run the Ansible ssms.yaml playbook
[oracle@ansible-server ansible]$ ansible-playbook ssms.yaml
Now we can see the new Table on the MSQLDR database after running the ansible refresh playbook
You can also run the playbook in debug mode to get more verbosity from the output
[oracle@ansible-server ansible]$ ansible-playbook -vvv ssms.yaml












Comments