Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup windows 10 workstation using Ansible installed on WSL

I have installed Ansible in the WSL (Windows Subsystem for Linux) within my Windows 10 workstation.

My goal is configure both, WSL, and the Windows 10 itself.

I'm able to run playbooks against localhost, which connect and configures via SSH the WSL.

However I am not sure Ansible can run playbooks against the Windows host to be able to setup Windows itself (e.g. install packages using Chocolatey)

Is that even possible? Or Ansible can only setup a windows node when is installed in a different Linux machine?

like image 508
vecin Avatar asked Oct 11 '19 16:10

vecin


2 Answers

Great! I was able to connect to my windows host after following those steps.

However, I had to solve two more issues before I was able to run ansible playbooks against both, WSL and windows host:

1. Define connection for WSL

Windows host uses ansible_connection=winrm, but for WSL needs a different connection, I've set ansible_connection=local.

2. Avoid connection var being overriden

The ansible_connection var is overridden. This is because the var name and the host name is the same. This means that you can either run a playbook for WSL or for Windows host but not against both, as they need different connection.

To fix that you can either set hash-behaviour, or set two different host names for localhost under your WSL, /etc/hosts. I've done the second one:

127.0.0.1   wsl.local
127.0.0.1   windows.local

My /etc/ansible/hosts:

[wsl]
wsl.local 

[wsl:vars]
ansible_connection=local

[windows]
windows.local 
[windows:vars]
ansible_port=5985
ansible_connection=winrm
ansible_winrm_transport=basic
ansible_user=<<ansible_user>>
ansible_password=<<ansible_password>>

Now I can run an ansible_playbook with tasks running against both, my windows host and my WSL. Here for more details on configuration.

like image 124
vecin Avatar answered Oct 10 '22 01:10

vecin


Yes, it's possible.

  1. First you must have WSL (which you do)
  2. Next you need to install Ansible, but you need extra packages for using it with WinRM
    • Install pip $ apt install python-pip
    • Install pip winrm $ pip install pywinrm
    • Install xml parser $ pip install xmltodict
  3. You need your WinRM setuped:
    • You need to make your network private as WinRM by default works only for Private or Domain networks. You can skip that by providing parameter to Enable-PSRemoting -SkipNetworkProfileCheck but I dont suggest doing that.
    • Enable WinRM Enable-PSRemoting running it in the PowerShell in Windows.
    • Enable Basic Auth Set-Item -Path WSMan:\localhost\Service\Auth\Basic -Value $true
    • Enable Unencrypted connectionSet-Item -Path WSMan:\localhost\Service\AllowUnencrypted -Value $true
  4. In WSL, add as vars to your playbook or group_vars
ansible_port: 5985
ansible_connection: winrm
ansible_winrm_transport: basic
  1. When running your playbook provide vars ansible_user=your_win_user and ansible_password=your_win_user_pass or hardcode them in the previous vars.

I use this setup to provision my machine from WSL. You can take a look here. Hope this helps.

like image 42
worldwildwebdev Avatar answered Oct 10 '22 00:10

worldwildwebdev