In DevOps workflows, infrastructure as code has become a very important approach to managing and automating any infrastructure. Ansible, a powerful infrastructure as code tool, simplifies configuration management, build deployments and environment setup across multiple systems. 

One common challenge in the DevOps world is ensuring that build environments are set up consistently across different machines to make sure they behave the same way. In this post, I will demonstrate how Ansible can automate the setup of a build environment for any type of build. I will include Windows as well as the Linux environment setup. 

  • Windows-based .NET builds 

Needs Visual Studio Build Tools, .NET 6, Git and Python for my example 

  • Linux-based C++/Python builds 

Needs CMake, Git and Python for my example 

By the end of this tutorial, you’ll be able to use Ansible to automate infrastructure setup on any Windows or Linux build machine. 

Why Use Ansible for IaC? 

  • Agentless: No need to install an agent on target machines 
  • Unchanged: Ensures it only changes if there is any new addition Multi-Platform: Supports Windows, Linux and cloud environments 
  • Scalability: Can automate configuration across multiple machines at the same time 
  • Simple: Easy to understand and write as it uses YAML (human readable format) 

Instead of manually installing dependencies on every build agent or cloud, we can use Ansible playbooks to define and automate the setup process across all at one time. 

Let’s jump on to a simple example: 

Setting Up Ansible for Windows and Linux 

Before writing a playbook, ensure that you follow this document

  • Ansible is installed on your control machine 
  • Python and WinRM are configured for Windows remote execution 

Install Ansible on the Control Machine 

Run the following command for both build agents: 

Configure Inventory File 

Create an inventory file (inventory.ini) to list the Windows and Linux build machines: 

Screenshot 2025 07 11 14.46.26

Replace ansible_host with your actual machine IPs that you are trying to setup. 

Replace your ansible_user and your ansible_password with the credentials of your administrative user. 

Ansible Playbook for Windows (.NET Build) and Linux (CMake Build) 

We’ll now create an Ansible playbook to set up both Windows and Linux build environments. 

Playbook File (playbook.yaml) 

Screenshot 2025 07 11 14.49.02

This playbook: 

Runs the windows_build role for Windows machines. Runs the linux_build role for Linux machines. 

Role for .NET Build Environment (Windows) 

Create a directory structure under your project: 

Screenshot 2025 07 11 14.51.17

This Windows role installs: 

Visual Studio Build Tools .NET 6 SDK 

Git 

Python 

Windows Role (roles/windows_build/tasks/main.yaml) 

Screenshot 2025 07 11 15.22.15

Role for Linux Build Environment 

Create a directory structure under your project: 

Screenshot 2025 07 11 14.56.37

This Linux role installs: 

CMake Git 

Python3 

Linux Role (roles/linux_build/tasks/main.yaml) 

Screenshot 2025 07 11 14.58.59

Screenshot 2025 07 11 15.00.39

Running the Playbook 

Execute the playbook to configure both Windows and Linux machines: 

Screenshot 2025 07 11 15.03.02

Verifying the Installation 

Windows Machine 

Run the following commands in PowerShell: 

 

Screenshot 2025 07 11 15.04.46

Linux Machine 

Run the following commands in the terminal: 

Screenshot 2025 07 11 15.07.26

Using This Playbook for More Machines 

To configure additional machines, simply update the inventory configuration file with new hosts and re-run the playbook:

Screenshot 2025 07 11 15.10.01

Ansible ensures it is unchanged, meaning: 

  • If a package is already installed, it wonʼt reinstall.  
  • If a package is missing, Ansible will install it. 

Common Errors or Issues Seen as Follows: 

Authentication Issues 

Ensure WinRM is enabled on Windows. 

Use SSH keys for Linux authentication for Git repositories. 

Firewall & Connectivity Problems 

Open necessary ports for WinRM. 

Configuration Updates 

Run the playbook regularly to maintain consistency and include new changes. 

Conclusion 

By using Ansible for Infrastructure as Code (IaC), you can: 

  • Automate build environment setup. 
  • Ensure consistency across Windows and Linux machines.  
  • Scale effortlessly by updating the inventory file. 

This playbook allows DevOps teams to quickly provision and configure any number of machines, making their lives easier!! 


Share.
Leave A Reply