0

Ansible for SQL Server DBAs. Available Now!

Header Logo
Blog DBA Challenges
Courses
Automated Sandbox Framework Ansible for SQL Server DBAs: Level 1
Log In
← Back to all posts
Connect
Share to…
Share

#016 Leveraging Ansible Variables to Configure Lock Pages in Memory

by Luke Campbell
Dec 28, 2024

You've probably seen it happen.  During peak load on a SQL Server instance, the operating system occasionally pages out parts of SQL Server's memory to disk, causing performance slowdowns.  And you've probably read how Lock Pages in Memory can help address this issue.  

Granting this user right can be time-consuming and error-prone when done manually especially if you need to grant the permission on multiple instances.

This is where Ansible comes in.  It allows you to automate changes and maintain configurations across development, staging, and production environments.  

Today, I want to introduce you to how to set this permission using Ansible and cover Ansible Variables.

Ansible variables make it easy to parameterize this configuration, while variable precedence ensures the right value always wins if defined in multiple places.

What are Ansible Variables?

Ansible variables allow you to store and reuse values—like the name of the SQL Server service account or the necessary Windows permission—so you can write flexible, maintainable playbooks. For example, you can manage LPIM (the SeLockMemoryPrivilege right) across different environments by defining variables in the appropriate location, like your role defaults, inventory files, or host_vars.

Below is a minimal playbook snippet that configures LPIM for a SQL Server service account:

- name: Grant Lock Pages in Memory to SQL Service Account

  hosts: sqlservers

  gather_facts: false

  vars:

    service_account_name: "SANDBOX\\SQLService"

    lpim_permission: "SeLockMemoryPrivilege"

  tasks:

    - name: Assign LPIM to the SQL Server service account

      ansible.windows.win_user_right:

        user: "{{ service_account_name }}"

        right: "{{ lpim_permission }}"

        action: add

Here, service_account_name and lpim_permission are standard Ansible variables defined at the play level.

How Variable Precedence Works

You can define the same variable (e.g., service_account_name) in multiple places. Ansible resolves which value to use based on a precedence hierarchy. From lowest to highest:

1. Role defaults (defaults/main.yml in a role)

2. Inventory group vars (group_vars)

3. Inventory host vars (host_vars)

4. Playbook/group/host vars or vars_files

5. Block vars, task vars, and handler vars

6. Role vars (vars/main.yml in a role)

7. Extra vars (-e on the command line)

If a variable is defined in multiple places, the definition with the highest precedence wins. For example, if service_account_name is set to "SANDBOX\\SQLService" in role defaults but you specify it as "SANDBOX\\ProdSQLService" via extra vars, then "SANDBOX\\ProdSQLService" will ultimately be used.

Example: Using LPIM with Different Accounts

Imagine you have two servers—one for development and one for production. You might keep a default service account in a role default file, then override it for production in your inventory’s host_vars or through extra vars:

• Role default (roles/mssql/defaults/main.yml):

service_account_name: "SANDBOX\\SQLServiceDev"

lpim_permission: "SeLockMemoryPrivilege"

• Inventory host_vars (host_vars/sql-prod.yml):

service_account_name: "SANDBOX\\SQLServiceProd"

When running the playbook against sql-prod host, Ansible automatically overrides the default account (SANDBOX\SQLServiceDev) with SANDBOX\SQLServiceProd from the host_vars file.

If you further decide to specify it at runtime:

ansible-playbook -i inventory site.yml -e "service_account_name=SANDBOX\\SQLServiceSpecial"

that extra var will override both the role default and the host_vars value. This level of control and flexibility is exactly what makes Ansible’s variable system so powerful.

Wrapping Up

Understanding variable precedence is crucial for any robust Ansible setup, especially when managing Windows-specific tasks like granting LPIM permissions to your SQL Server service account.

Whether you define your variables in role defaults, host_vars, or pass them via extra vars, the highest priority always wins—helping you keep configurations consistent and your SQL Servers properly configured.

- Luke

Responses

Join the conversation
t("newsletters.loading")
Loading...
#015 Introduction to the Automated Sandbox Framework
2025 is just around the corner, and it's shaping up to be an exciting year for DBAs.  With Windows Server 2025 already here and SQL Server 2025 on the horizon, there's no better time to sharpen your skills and prepare for what's ahead. This year, I've focused on solving a common challenge for DBAs: how to quickly and consistently build test environments for learning and experimentation. In mid-...
#014 Introduction to the Enterprise Policy-Based Management Framework
It's been a few weeks since the last edition.  Life happens--between releasing Ansible for SQL Server DBAs, celebrating Thanksgiving, preparing for Christmas, and juggling the day-to-day demands of being a full-time consultant, it's easy to feel stretched thin.  Have you ever felt that way? But here's a question:  Are we just staying busy or moving toward our goals (we like to think so)?  Are o...
#013 Creating and Implementing Your First Policy
Usually, right after building a SQL Server instance, it's in pretty good shape.  You've configured settings like maxdop, cost threshold for parallelism, max memory, etc. You've likely set up your standard maintenance jobs and set up users and roles appropriately.   But let's say it's a year or two later, and you've been disengaged from that instance.  Are the settings you applied still intact? ...

DBA Challenges

Helping you become a more efficient SQL Server Database Administrator through real-world challenges and automation.
Footer Logo
Policies Contact
© 2025 AUTOMATESQL LLC

Five Steps to Spot Automation Opportunities

Wondering if that task you're working on should be automated?  With your free Time Tracker Excel workbook, you're able to easily track and analyze how you spend your time throughout the day.

 

When you signup, we'll send you periodic emails with additional free content. Unsubscribe anytime.