Why Ansible & "As Code" Concepts
IaC vs. CaC, declarative YAML vs. PowerShell, idempotency, and --check.
00:00I Already Know PowerShell — Why Learn YAML?Chapter 1 of 7
In this lesson, you will learn:
- Why Ansible complements—rather than replaces—your existing PowerShell skills on Windows Server.
- The practical boundary between Infrastructure as Code (IaC) and Configuration as Code (CaC) using your 5-node sandbox lab as a concrete example.
- The difference between Imperative scripting ("how to do it") and Declarative automation ("what the final state must be").
- How Idempotency and Ansible's
--checkmode eliminate configuration drift and simplify Change Advisory Board (CAB) approvals. - Why an Agentless architecture is critical when managing production Windows and SQL Server hosts.
1. The DBA's Question: "Why Not Just Use PowerShell?"
Whenever I talk to another SQL Server DBA or Windows Administrator about Ansible, the first question is almost always the same:
"Luke, I already know PowerShell. Why would I spin up a Linux VM and learn YAML just to manage Windows servers?"
That is a fair question—and I asked the exact same thing when I first encountered Ansible.
PowerShell is not going anywhere. If you work with Windows Server and SQL Server, PowerShell and community modules like dbatools are essential tools in your kit. In fact, under the hood, Ansible's Windows modules are written in PowerShell.
PowerShell handles single-server tasks well, but maintaining consistent settings across 20, 50, or 200 Windows servers over time introduces a different set of problems.
What Happens as Imperative Scripts Scale
When you write a basic PowerShell build script to configure a brand-new server—initializing disks, formatting E:, F:, and T: drives, adding Active Directory groups to Local Administrators, and opening firewall ports—it works great the very first time you run it on a blank machine, assuming nothing goes wrong.
The problems show up the moment you have to re-run that script or keep existing servers aligned over time:
- The "Failed Halfway Through" Problem: Suppose your 10-step build script succeeds on Steps 1 through 4 (disks initialized, drives formatted, local admin group updated), and then fails on Step 5 because a network share was unreachable. Once you fix the network issue, you cannot simply re-run the script—Steps 1 through 4 will immediately blow up with red terminating errors (
Disk is already initialized,The specified account name is already a member of the group) unless you manually highlight and run only the bottom half of the file. - Extra State-Checking and Error-Handling Code: To make that script safe to re-run, you end up writing 150 lines of
if / elsechecks,try / catchblocks, and custom logging. - Six Months Later (Updating 20 Existing Servers): Six months after those servers go live, you need to add a new security group, open a monitoring port, or verify that no one manually changed a service account permissions during a maintenance window. A build-only script can't help you—and when one admin runs
Prep-Server-v2.ps1from their desktop while another runsPrep-Server-Final-FIXED.ps1from a file share, nobody knows which servers actually match your standard. - Multi-Node Orchestration: When you push scripts across 30 servers with
Invoke-Command, you have to build your own logic to track which servers succeeded, which ones timed out, which ones already matched the baseline, and which ones need a reboot before the next step can continue.
Ansible solves this by separating the state you want from the code required to check and enforce it.
2. Infrastructure as Code (IaC) vs. Configuration as Code (CaC)
You will often hear the phrase "Infrastructure as Code" (IaC) used as a catch-all term for any automation file stored in Git. In practice, enterprise automation splits cleanly into two distinct layers—and you are already using both of them in this course.
| Automation Layer | Primary Focus | What It Does in the Real World | What Uses It in Your Lab |
|---|---|---|---|
| Infrastructure as Code (IaC) | The "Metal" & Virtual Hardware | Provisions virtual machines, vCPUs, memory, virtual switches, storage LUNs, and cloud networks (Terraform, Bicep, CloudFormation, Vagrant). | Your vagrantfile (which created DC1, SRV1, SQL1, SQL2, and AC1 and attached 3 raw virtual disks to SQL1 and SQL2). |
| Configuration as Code (CaC) | Inside the Operating System | Configures the OS after it boots: joins Active Directory, formats drives, installs packages, hardens security policies, and manages services. | Ansible running on AC1 to configure SRV1, SQL1, and SQL2. |
3. Imperative vs. Declarative Automation
The biggest mental shift when moving from PowerShell scripts to Ansible playbooks is switching from Imperative thinking to Declarative thinking.
Side-by-Side Code Comparison: Adding a Domain Group to Local Administrators
Suppose you want to ensure the SANDBOX\DBA_Admins Active Directory group is a member of the local Administrators group on SQL1 and SQL2.
The Imperative Approach (Raw PowerShell)
To make a raw PowerShell script safe to run repeatedly without throwing "The specified account name is already a member of the group", you have to write the inspection logic yourself:
$GroupName = "Administrators"
$MemberToAdd = "SANDBOX\DBA_Admins"
$CurrentMembers = Get-LocalGroupMember -Group $GroupName | Select-Object -ExpandProperty Name
if ($CurrentMembers -notcontains $MemberToAdd) {
Write-Output "Adding $MemberToAdd to $GroupName..."
Add-LocalGroupMember -Group $GroupName -Member $MemberToAdd -ErrorAction Stop
} else {
Write-Output "$MemberToAdd is already in $GroupName. No action needed."
}The Declarative Approach (Ansible Playbook Task)
In Ansible, you declare the desired end state using the ansible.windows.win_group_membership module:
- name: Ensure DBA_Admins group is in Local Administrators
ansible.windows.win_group_membership:
name: Administrators
members:
- SANDBOX\DBA_Admins
state: presentNotice what is missing from the YAML: there is no if statement, no Get-LocalGroupMember query, and no try/catch wrapper. The ansible.windows.win_group_membership module still runs PowerShell on the target Windows server, but the module already contains all the defensive state-checking logic for you.
4. Idempotency & Proving What Will Change to the CAB
That comparison brings me to the single most important concept in Ansible: Idempotency.
An operation is idempotent if running it once produces the exact same result as running it ten times, without unintended side effects.
When you run an Ansible playbook against your Windows servers (DC1, SRV1, SQL1, SQL2), Ansible evaluates every task on every server and reports one of three primary states:
| Ansible Status | Terminal Color | What It Means for Your Windows Server |
|---|---|---|
ok |
Green | Ansible inspected the server and confirmed it already matches your playbook. Zero changes were made. |
changed |
Yellow / Amber | Ansible detected configuration drift (for example, a missing firewall rule or unformatted disk) and modified the server to match your playbook. |
failed |
Red | Ansible could not reach the target state (for example, a bad credential, unreachable host, or locked resource) and halted execution for that host. |
How Idempotency Changes Your Daily Workflow
- Safe Re-Runs After Failures: If a playbook fails halfway through because of a temporary network or DNS glitch, you simply re-run the exact same file—completed tasks report
ok(Green) in seconds and execution picks up where it left off. - Continuous Drift Detection: If someone manually stops a required Windows service or alters a permission over RDP, the next playbook run flags that host as
changed(Yellow) and restores your baseline automatically. - Dry-Run (
--check) Mode for CAB Approvals: Runningansible-playbookwith--checkconnects to your Windows servers, inspects their current state, and previews which tasks would reportchangedwithout modifying a single setting:
5. Why Agentless Architecture Matters on Windows & SQL Server
Many legacy configuration management tools (such as Puppet, Chef, or SCCM/MECM) require you to install, patch, and maintain a proprietary background agent service on every single Windows server.
6. Key Takeaways
- Ansible doesn't replace PowerShell—it orchestrates PowerShell across your Windows fleet with built-in state checking, parallel execution, and reporting.
- IaC (Vagrant / Terraform) builds the virtual hardware; CaC (Ansible) configures the Windows OS, Active Directory membership, storage, and services inside the OS.
- Declarative playbooks describe what the server should look like (
state: present), freeing you from writing repetitiveif/elsestate-checking code. - Idempotency ensures you can run your playbooks repeatedly—first to configure a raw server (
changed), and forever after to verify compliance (ok). - Agentless execution keeps your Windows and SQL Server hosts clean, using native WinRM and OpenSSH connections instead of background agents.