Ansible By Example - How I Use Ansible Roles

ansible dba challenges newsletter sql server windows server Jul 06, 2024

Edition: Saturday, July 6th, 2024

As you begin creating playbooks to automate your work, you'll soon discover they can become many 100s of lines of YAML. 

Ansible Roles, allow you to group tasks, variables, handlers, and other artifacts.  Today, I'll focus on an example role I've built and used to install and configure SQL Server on Windows. 

Let's jump in. 

What are Ansible Roles?


Ansible roles are a way to organize playbooks and reusable configurations.  Roles allow you to break down your configuration into smaller, reusable components, making your Ansible projects more modular and easier to manage.

Structure

They typically have the following directory structure:

  1. tasks: Contains the main list of tasks to be executed by the role.
  2. handlers: Defines handlers that can be used by this role or other roles.  Handlers are tasks that run only when notified.
  3. templates: Contains templates that can be deployed by the role.  In our case, we'll place the config2022.j2 file here (which becomes the Configuration.ini file when installing SQL Server).
  4. files: Holds static files that need to be deployed by the role.  We won't use this folder in today's example.
  5. vars: Defines variables that are available to the role.  Today's example will use a main.yml var file which has been encrypted using ansible-vault.  It contains the SA password along with the domain user we're adding to the sysadmin role.
  6. defaults: Sets default variables for the role.  I'm storing non-sensitive defaults here.
  7. meta: Defines metadata about the role, including dependencies.
  8. README.md: Optional, provides information about the role.

Benefits

  1. Modularity: Roles encapsulate configurations, making them modular and reusable.  You can develop roles that perform specific functions and reuse them across multiple playbooks.
  2. Reusability: Roles can be shared and reused across different projects.  This makes it easier to maintain consistency and reduces duplication effort.
  3. Maintainability: By organizing configurations into roles, you make your playbooks more maintainable.  Changes can be made in one place and will be reflected wherever the role is used.
  4. Readability: Roles improve the readability of playbooks by abstracting complex configurations into simplified, organized, components.
  5. Collaboration: Roles enable better collaboration among team members.  Different team members can work on different roles simultaneously without interfering with each other's work.
  6. Scalability: Roles help scaling the configuration management as they can be used to manage configurations across a large number of nodes efficiently.

 

Example - MSSQL role


Here's a quick example of a role I created to perform the following tasks:

  1. Initialize, partition, and format 3 disks.
  2. Set the power plan to high performance.
  3. Copy the ComputerManagementDsc and SqlServerDsc (PowerShell desired state configuration) resources from the Ansible control node to the target hosts.
  4. Copy the SQL Server 2022 Dev ISO and update files to the target hosts.
  5. Generate the Configuration.ini file using variables found in the defaults/main.yml file.  
  6. Mount the ISO.
  7. Install SQL Server and the update.
  8. Configure max memory, maxdop, enable backup compression, configure cost threshold for parallelism, and configure instance port.
  9. Create a firewall rule to allow traffic using the port SQL Server is now listening on.
  10. Remove the DSC resources from the target hosts.
  11. Delete the ISO and update files from the target hosts.

This takes about 11 minutes to complete on two host machines.

Environment:


Here's how I have my lab configured.  If you build one to follow along in the examples, be sure to create snapshots first.  This way you can easily reset your environment for the next walkthrough.

  • Hardware - Geekom A7 Mini PC Ryzen - 64 GB RAM / 2 TB drive
  • Hypervisor - VMware Workstation Pro 17.5 (get this for free if you're using it for personal use - VMware Workstation Pro: Now Available Free for Personal Use)
  • Virtual Machines - All built using a single Windows Server 2022 Standard (eval) Hashicorp Packer image and managed with Vagrant.
    • DC1 - Domain Controller (HOMELAB.LOCAL)
    • CA1 - Certificate Authority
    • SRV1 - SQL Server (eventually) - Contains the OS drive and 3 uninitialized NVMe disks.
    • SRV2 - SQL Server (eventually) - Contains the OS drive and 3 uninitialized NVMe disks.
    • SRV3 - Client machine - Windows Subsystem for Linux (Ubuntu) - Ansible Control Node.  Visual Studio Code is installed and used to create the playbooks throughout this series.

 

Conclusion:


Ansible Roles are great for breaking down complex configurations into manageable tasks.  Combining the flexibility of Ansible and PowerShell Desired State Configuration allows you to have a lot of flexibility in managing your environment as code.  From single instance configurations to complex failover clustered instances and Availability Groups.  

You can grab the example code at Ansible-for-SQL-Server-DBAs/Ansible-Roles.

Ansible For SQL Server DBAs (join the wait list) - https://www.automatesql.com/ansible

Thanks for reading!  Have a great week!