Mastering Ansible: Configure Local Windows 11 Machine via WSL
Image by Pancho - hkhazo.biz.id

Mastering Ansible: Configure Local Windows 11 Machine via WSL

Posted on

Are you tired of manually configuring your local Windows 11 machine? Do you want to automate the process and make it more efficient? Look no further! In this article, we’ll show you how to configure your local Windows 11 machine using Ansible in WSL (Windows Subsystem for Linux). Yes, you read that right – Ansible on Windows!

Prerequisites

Before we dive into the tutorial, make sure you have the following prerequisites in place:

  • Windows 11 machine with WSL 2 enabled
  • Ansible installed on your WSL distribution (we’ll use Ubuntu in this example)
  • A basic understanding of Ansible and its terminology

Setting up Ansible on WSL

If you haven’t already, let’s set up Ansible on your WSL distribution. Open your WSL terminal and run the following command:

sudo apt update && sudo apt install ansible

This will install Ansible and its dependencies on your WSL distribution. Once the installation is complete, you can verify Ansible’s version by running:

ansible --version

Creating an Inventory File

In Ansible, an inventory file is used to define the hosts that you want to configure. In our case, we’ll create an inventory file that points to our local Windows 11 machine. Create a new file called `hosts` in your WSL home directory with the following contents:

[windows]
localhost ansible_connection=winrm ansible_winrm_transport=basic

This inventory file defines a single host, `localhost`, with the `ansible_connection` set to `winrm` and `ansible_winrm_transport` set to `basic`. This tells Ansible to use the WinRM connection plugin to connect to our Windows 11 machine.

Configuring WinRM on Windows 11

Before we can use Ansible to configure our Windows 11 machine, we need to enable and configure WinRM. Open the Command Prompt as an administrator and run the following commands:

winrm quickconfig
winrm set winrm/config/service/auth @{Basic="true"}
winrm set winrm/config/service @{AllowUnencrypted="true"}

This will enable WinRM and configure it to allow basic authentication and unencrypted connections.

Creating a Playbook

A playbook is a YAML file that defines the tasks that Ansible should execute on the target host. Let’s create a playbook that configures our Windows 11 machine. Create a new file called `configure_windows.yml` in your WSL home directory with the following contents:

---
- name: Configure Windows 11 machine
  hosts: windows
  gather_facts: no

  tasks:
  - name: Install Chocolatey
    win_chocolatey:
      name: chocolatey
      state: present

  - name: Install Git
    win_chocolatey:
      name: git
      state: present

  - name: Set wallpaper
    win_regedit:
      path: HKCU:\Control Panel\Desktop
      name: WallPaper
      data: C:\Windows@Web\Wallpaper\img0.jpg
      type: string

  - name: Disable Windows Defender
    win_regedit:
      path: HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender
      name: DisableAntiSpyware
      data: 1
      type: dword

This playbook defines four tasks that will be executed on our Windows 11 machine:

  • Install Chocolatey
  • Install Git
  • Set the wallpaper to a specific image
  • Disable Windows Defender

Running the Playbook

Now that we have our playbook in place, let’s run it! Open your WSL terminal and navigate to the directory where you created the playbook:

ansible-playbook -i hosts configure_windows.yml

This command tells Ansible to run the `configure_windows.yml` playbook using the `hosts` inventory file. Ansible will connect to our Windows 11 machine using WinRM and execute the tasks defined in the playbook.

Verifying the Configuration

Once the playbook has completed, let’s verify that the configuration changes have been applied. You should see that Chocolatey and Git have been installed, the wallpaper has been set, and Windows Defender has been disabled.

Task Verification Steps
Install Chocolatey Open a new Command Prompt and run choco --version. You should see the Chocolatey version number.
Install Git Open a new Command Prompt and run git --version. You should see the Git version number.
Set wallpaper Check that the wallpaper has been set to the specified image.
Disable Windows Defender Open Windows Defender and check that it has been disabled.

Conclusion

In this article, we’ve shown you how to configure your local Windows 11 machine using Ansible in WSL. We’ve covered setting up Ansible on WSL, creating an inventory file, configuring WinRM on Windows 11, creating a playbook, and running the playbook to apply the configuration changes. With Ansible, you can automate the configuration of your Windows 11 machine and make it more efficient and consistent.

Additional Resources

For more information on Ansible and its modules, check out the following resources:

Happy automating!

Frequently Asked Questions

The world of Windows 11, Ansible, and WSL (Windows Subsystem for Linux) can be a fascinating yet daunting place. Fear not, dear adventurer, for we’ve got the answers to your most pressing questions!

How do I install Ansible on my Windows 11 machine using WSL?

Easy peasy! You can install Ansible on your Windows 11 machine using WSL by following these steps: Open your WSL terminal, update your package list with `sudo apt update`, and then install Ansible with `sudo apt install ansible`. Voilà! You’re ready to roll!

What is the best way to configure my local Windows 11 machine using Ansible in WSL?

To configure your local Windows 11 machine using Ansible in WSL, create an inventory file (`hosts`) that includes your local machine’s IP address or hostname. Then, write a playbook that defines the desired state of your machine, and finally, run the playbook using `ansible-playbook -i hosts playbook.yml`. Boom! Your machine is now configured according to your playbook.

Can I use Ansible modules specifically designed for Windows in WSL?

You bet! Ansible provides a range of modules specifically designed for Windows, such as the `win_*` modules. These modules can be used in your playbook to manage your Windows 11 machine. Simply install the required modules using `ansible-galaxy collection install ansible.windows`, and then use them in your playbook as needed.

How do I ensure that my Ansible playbook is idempotent when configuring my local Windows 11 machine?

To ensure idempotence, design your playbook to use Ansible’s built-in modules and parameters that support idempotent operations. For example, use the `win_package` module with the `state: present` parameter to install software only if it’s not already installed. Additionally, use conditional statements and variables to make your playbook more dynamic and flexible.

What are some best practices for debugging Ansible playbooks in WSL?

When debugging Ansible playbooks in WSL, use the `-v` flag to increase verbosity, which provides more detailed output. You can also use the `–step` flag to step through the playbook one task at a time. Additionally, enable debugging on specific tasks by adding the `debug` module with the `var` parameter to inspect variable values.