Ansible on Windows or almost...

In short
  • Ansible, which is agentless and based on SSH, runs very well from Windows thanks to WSL.
  • Installation under Ubuntu/WSL and configuration via ~/.ansible.cfg.
  • Creating a dedicated user account named "ansible-windows" with its private key.
  • YAML inventories and the community VMware module are enabled for the next steps.
Ansible logo

Click the image to enlarge.

Introduction

In just a few years, Ansible has become the standard for managing the configuration of your server infrastructure, or even your entire infrastructure (especially the network).

Arriving as an alternative to the classic Puppet or Salt, it has gained favor with many system administrators, including myself.

Its main strength (in my opinion) is that it is "agentless" and relies only on a simple SSH connection to manage its targets. Add to this an ever-increasing number of modules that simplify the writing of these famous playbooks, and you get a superb tool for deploying and ensuring compliance for your server configurations or other systems.

The idea of this article is not to go into detail about how Ansible works, but to demonstrate that Ansible can be perfectly used from your Windows workstation

Principle of Ansible (source: https://blog.stephane-robert.info/docs/infra-as-code/gestion-de-configuration/ansible/introduction/).

Click the image to enlarge.

Installing Ansible on Windows

For this exercise, it is necessary to have deployed the WSL layer for Windows. I invite you to follow the following tutorial

Provided that you have chosen Ubuntu as your "Linux subsystem", installing Ansible is very quick.

Launch your Windows terminal, open a tab dedicated to WSL, and run the Ansible installation command.

sudo apt install ansible
Installation of Ansible via APT under WSL

Click the image to enlarge.

Configuring Ansible

You can configure Ansible in two ways:

  • at the system level via /etc/ansible/ansible.cfg
  • at the user session level via ~/.ansible.cfg

  • We will modify the file in the user environment

    In the [defaults] section, you can specify the location of your inventory files. These inventories can be of different types, ranging from simple flat files to Python scripts.

    This is the foundation of Ansible, which uses these inventories to build a list of targets (your servers, switches, etc.) based on what you request.

    Since we are using the Linux subsystem for Windows, there is nothing preventing you from pointing your inventories to a folder within Windows. For reference, your entire Windows file structure is accessible through WSL via the mount point /mnt.

    The same applies to roles. These roles, in the form of playbooks, will gather the instructions to be applied to your target systems.

    Ansible configuration file

    Click the image to enlarge.

    Setting up a dedicated user account

    For greater consistency, you can reference a specific user for connecting to your target systems. Here, I have chosen "ansible-windows". Of course, this user must exist on the resources that Ansible will attempt to connect to. We will see this point later.

    Personally, I recommend using a dedicated user for Ansible in order to clearly differentiate between actions performed via Ansible and actions performed by a regular user.

    Therefore, you must first create this user on WSL.

    sudo useradd ansible-windows
    sudo mkdir /home/ansible-windows
    sudo chown -R ansible-windows /home/ansible-windows/

    Then, connect using the identity of that user

    sudo su - ansible-windows

    Ansible uses SSH to connect to its target systems, so it needs an SSH key generated for it.

    ssh-keygen
    SSH key generation

    Click the image to enlarge.

    In order for this key to be usable without necessarily assuming the identity of the user, we will proceed with modifying the following permissions

    It is not a good security practice to change the default permissions of SSH elements, but this will allow me to use my current user to run my commands and have them executed on the target systems using the ansible-windows account.

    chmod g+r /home/ansible-windows/.ssh/id_rsa
    chmod g+x /home/ansible-windows/.ssh/

    Finally, I will add my account to the group associated with ansible-windows.

    vim /etc/group
    Ansible group

    Click the image to enlarge.

    Configuration summary

    In summary, the configuration of my Ansible installation under Windows can be summarized in my local configuration file

    vim ~/.ansible.cfg
    Kubernetes cluster

    Click the image to enlarge.

    Note the addition of the "private_key_file" parameter, which points to the private key of my "ansible-windows" user that I created earlier.

    In this way, I can simply launch a WSL instance from my Windows terminal using my own identity, but use the "ansible-windows" account (via the use of its private key) for connecting to my remote servers when I call on Ansible.

    You could say that I am using a service account. Of course, this account must be known to my target systems and its key must be authorized. This point will be addressed later.

    A new [inventory] section has been added. It allows you to specify the supported inventory formats. In my case, I am enabling support for YAML files as well as the community vmware module to create an inventory directly from a vCenter. This will be discussed later.

    At this stage, Ansible is fully functional and can be called from your Windows workstation via the WSL layer

    Running an Ansible command

    Click the image to enlarge.