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:
- tasks: Contains the main list of tasks to be executed by the role.
- handlers: Defines handlers that can be used by this role or other roles. Handlers are tasks that run only when notified.
- 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).
- files: Holds static files that need to be deployed by the role. We won't use this folder in today's example.
- 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.
- defaults: Sets default variables for the role. I'm storing non-sensitive defaults here.
- meta: Defines metadata about the role, including dependencies.
- README.md: Optional, provides information about the role.
Benefits
- Modularity: Roles encapsulate configurations, making them modular and reusable. You can develop roles that perform specific functions and reuse them across multiple playbooks.
- Reusability: Roles can be shared and reused across different projects. This makes it easier to maintain consistency and reduces duplication effort.
- 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.
- Readability: Roles improve the readability of playbooks by abstracting complex configurations into simplified, organized, components.
- Collaboration: Roles enable better collaboration among team members. Different team members can work on different roles simultaneously without interfering with each other's work.
- 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:
- Initialize, partition, and format 3 disks.
- Set the power plan to high performance.
- Copy the ComputerManagementDsc and SqlServerDsc (PowerShell desired state configuration) resources from the Ansible control node to the target hosts.
- Copy the SQL Server 2022 Dev ISO and update files to the target hosts.
- Generate the Configuration.ini file using variables found in the defaults/main.yml file.
- Mount the ISO.
- Install SQL Server and the update.
- Configure max memory, maxdop, enable backup compression, configure cost threshold for parallelism, and configure instance port.
- Create a firewall rule to allow traffic using the port SQL Server is now listening on.
- Remove the DSC resources from the target hosts.
- 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!
Whenever you're ready, there is one way I can help you gain hands-on experience:Ā
Automated Sandbox Fundamentals: I teach how to build a virtual lab using automation in this course. Learn how toĀ create golden images, using both Windows and Linux, to easily spin up and add additional machines to your sandbox.Ā It's packed with 8 modules and the scripts you'll need to build your environment.Ā Start small, andĀ scale as needed by easily changing the configuration file included with the course.