AUTOMATESQL
Back to all guides
May 7, 2025•10 min

SQL Server AG Patching with Ansible: Prerequisites

May 07, 2025

prerequisites

Defining the prerequisites before you start patching is extremely important. I'm not talking about making sure your Ansible control node is set up and testing access to your SQL Server AG replicas (very important as well, but a different subject).

Nope, I'm talking about making sure that the exe file you're copying to each host is validated and it's the one you expect. Also, making sure your replicas are compatible with the patch you're about to install. These are the kinds of tasks we're going to perform using the prerequisites.yml file. Now, you may have additional prerequisite tasks you'd like to automate (like making sure there are recent backups, integrity checks have all passed recently, etc), but I've limited the number of tasks to keep the example simple.

Think of this as a template you can use and tweak later for your specific use case.

By the end of this post, you'll have built:

  • defaults/main.yml
  • tasks/main.yml- We'll start this one, but it won't be complete until the end.
  • tasks/prerequisites.yml
  • tasks/identify_primary.yml
  • files/get_sqlversions.ps1

defaults/main.yml

The default.yml file will hold all of the default values for the role variables. But remember, these can be overridden and are very low on Ansible's variable precedence order. I use VS Code to open up the defaults/main.yml file to make things easier. If you've installed Ansible on WSL, then connect to WSL using VS Code, open the sql_ag_patch folder, and then defaults/main.yml. I've left a few example values in place for the defaults.

tasks/main.yml

We'll go ahead and modify the tasks/main.yml file and add the first two tasks. We'll add additional tasks throughout this series.

tasks/prerequisites.yml

This file will contain all of the prerequisite tasks that need to be completed before installing the patch. As you review, think about your unique environment and what you might add here. Checking for backups, a successful integrity check within the past X days, looking for long-running jobs, pending reboots, etc. We're going to leverage the get_sqlversions.ps1 file to retrieve the SQL Server version.

This role will use this file multiple times, here and in the post-validation phase. Spend some time looking over this section and really understand which Ansible modules are being used and why. Especially Ansible's assert module.

We're grouping two tasks by leveraging Ansible's block functionality. The first task in the block will use the win_statmodule to get the checksum of the patch file. Then, the checksum is compared with the checksum we set in the default.yml file. Next, we'll use Ansible's rescue section and fail the play if the checksums do not match. If the checksum does match, then the next task runs.

tasks/identify_primary.yml

I mentioned in the previous post that we're going to avoid hard-coding or even adding a variable to specify the primary replica. Instead, we'll leverage this task file to determine, at run time, which of our replicas is the primary. We're using the win_powershell module again in this file, but notice that the script is used directly instead of using Ansible's lookup plugin. We could have moved this script to a separate .ps1 file, but I wanted to show the flexibility Ansible has when running PowerShell scripts. Plus, this specific PowerShell script is only used once (vs the get_sqlversions.ps1 script you'll build next). You could move the script to its own file or leave it as is. It's up to you.

files/get_sqlversions.ps1

Notice we've switched directories here. Remember that the files directory contains static files to be deployed. However, you'll see that these files don't actually get deployed. We're using Ansible's lookup plugin to read them and then run the script instead of copying them to each replica. Less to clean up in the end.

Conclusion:

Between now and part 4, you have a bit of homework to do. The purpose of this series is not to hand over code to automate a very common task for SQL Server DBAs, but to provide examples you can use as you learn how powerful Ansible is, and why it should be in your toolbox. This example role goes further than using PowerShell or DBATOOLS alone; it'll show you how Ansible can be leveraged to orchestrate the entire process, including failovers, reboots, failbacks, validation, etc, with minimal code.

If you have questions, drop those in the comments below. Next up, we'll build the tasks that will perform validation, patching, and changing failover modes. See you then!

Previous ← SQL Server AG Patching with Ansible: Creating Your Role Structure
Next → SQL Server AG Patching with Ansible: Tasks

LinkedIn YouTube Newsletter

Share this article:
The Database Platform Automation Roadmap

The Database Platform Automation Roadmap

Move from checklists and scattered scripts to desired-state Ansible playbooks—so you can stop wondering whether production still matches what you built. Includes the 60-second self-audit and 23-page field guide (PDF).