How to configure Windows client setup for Ansible

To Configure Windows client setup for Ansible

Introduction:

Ansible is an open-source automation tool used to manage various configuration and application deployment. It can configure both Unix-like systems as well as Windows systems.

Master Server Requirements:

python3-pip

pywinrm (python package)

Windows Requirements:

powershell 3+

Dot net 4 Step 1: Open the “Power Shell ISE” in windows as Administrator and check the version by using following command

PS C:\WINDOWS\system32> $psversiontable

Name                           Value                                                                                                                                     
----                           -----                                                                                                                                     
PSVersion                      5.1.19041.1237                                                                                                                            
PSEdition                      Desktop                                                                                                                                   
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                   
BuildVersion                   10.0.19041.1237                                                                                                                           
CLRVersion                     4.0.30319.42000                                                                                                                           
WSManStackVersion              3.0                                                                                                                                       
PSRemotingProtocolVersion      2.3                                                                                                                                       
SerializationVersion           1.1.0.1 

Step 2: Search for “setting up windows host ansible”

Step 3: Click on WinRM setup under “setting up windows host ansible”

Step 4: Click on Configure Remoting for ansible.ps1

Step 5: Copy the 453 lines of code from , https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 , Paste 453 lines code in powershell

PS C:\WINDOWS\system32> #Requires -Version 3.0

# Configure a Windows host for remote management with Ansible
# -----------------------------------------------------------
#
# This script checks the current WinRM (PS Remoting) configuration and makes
# Written by Trond Hindenes <trond@hindenes.com>
# Updated by Chris Church <cchurch@ansible.com>
# Updated by Michael Crilly <mike@autologic.cm>
# Updated by Anton Ouzounov <Anton.Ouzounov@careerbuilder.com>
# Updated by Nicolas Simond <contact@nicolas-simond.com>
# Updated by Dag Wieërs <dag@wieers.com>
# Updated by Jordan Borean <jborean93@gmail.com>
# Updated by Erwan Quélin <erwan.quelin@gmail.com>
# Updated by David Norman <david@dkn.email>

     Write-Log "Unable to establish an HTTP or HTTPS remoting session."
    Throw "Unable to establish an HTTP or HTTPS remoting session."
}
Write-VerboseLog "PS Remoting has been successfully configured for Ansible."
Self-signed SSL certificate generated; thumbprint: 6A907B378A75304BC269621019110F9A264462DF


wxf                 : http://schemas.xmlsoap.org/ws/2004/09/transfer
a                   : http://schemas.xmlsoap.org/ws/2004/08/addressing
w                   : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
lang                : en-US
Address             : http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
ReferenceParameters : ReferenceParameters

Ok.

Step 6: Checking the master server OS version

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 7: Adding the ansible repository to apt

root@linuxhelp:~# apt-add-repository ppa:ansible/ansible
Repository: 'deb http://ppa.launchpad.net/ansible/ansible/ubuntu/ hirsute main'
Description:
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.

http://ansible.com/
More info: https://launchpad.net/~ansible/+archive/ubuntu/ansible
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.
Adding deb entry to /etc/apt/sources.list.d/ansible-ubuntu-ansible-hirsute.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/ansible-ubuntu-ansible-hirsute.list
Adding key to /etc/apt/trusted.gpg.d/ansible-ubuntu-ansible.gpg with fingerprint 6125E2A8C77F2818FB7BD15B93C4A3FD7BB9C367
Hit:1 http://in.archive.ubuntu.com/ubuntu hirsute InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu hirsute-updates InRelease [115 kB]                          
Get:3 http://in.archive.ubuntu.com/ubuntu hirsute-backports InRelease [101 kB]                         
Get:4 http://ppa.launchpad.net/ansible/ansible/ubuntu hirsute InRelease [18.1 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main i386 Packages [202 kB]      
Hit:6 http://security.ubuntu.com/ubuntu hirsute-security InRelease                       
Get:7 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 Packages [406 kB]   
Get:8 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 DEP-11 Metadata 
Get:15 http://in.archive.ubuntu.com/ubuntu hirsute-updates/multiverse amd64 DEP-11 Metadata [944 B]    
Get:16 http://in.archive.ubuntu.com/ubuntu hirsute-backports/universe amd64 DEP-11 Metadata [9,348 B]  
Get:17 http://ppa.launchpad.net/ansible/ansible/ubuntu hirsute/main amd64 Packages [880 B]      
Get:18 http://ppa.launchpad.net/ansible/ansible/ubuntu hirsute/main Translation-en [516 B]
Fetched 1,588 kB in 2s (866 kB/s)                    
Reading package lists... Done

Step 8: Update the apt after adding the ansible repository

root@linuxhelp:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu hirsute InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu hirsute-updates InRelease                                   
Hit:3 http://in.archive.ubuntu.com/ubuntu hirsute-backports InRelease                                  
Hit:4 http://ppa.launchpad.net/ansible/ansible/ubuntu hirsute InRelease                                
Hit:5 http://security.ubuntu.com/ubuntu hirsute-security InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
263 packages can be upgraded. Run 'apt list --upgradable' to see them.

Step 9: Now install Ansible by using following command

root@linuxhelp:~# apt install ansible
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  ansible-core python3-jinja2 python3-packaging python3-pyparsing python3-resolvelib sshpass
Suggested packages:
  python-jinja2-doc python-pyparsing-doc
The following NEW packages will be installed:
  ansible ansible-core python3-jinja2 python3-packaging python3-pyparsing python3-resolvelib sshpass
0 upgraded, 7 newly installed, 0 to remove and 263 not upgraded.
Need to get 21.1 MB of archives.
After this operation, 277 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 python3-jinja2 all 2.11.2-1 [99.8 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 python3-pyparsing all 2.4.7-1 [61.4 kB]   
Get:3 http://ppa.launchpad.net/ansible/ansible/ubuntu hirsute/main amd64 ansible-core all 2.11.6-1ppa~hirsute [927 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 python3-packaging all 20.9-2 [29.9 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu hirsute/universe amd64 python3-resolvelib all 0.5.4-1 [21.6 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu hirsute/universe amd64 sshpass amd64 1.09-1 [11.7 kB]

Step 10:Installing the python package manger

root@linuxhelp:~# apt install python3-pip
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu build-essential dpkg-dev fakeroot g++ g++-10 gcc
  gcc-10 gcc-11-base javascript-common libalgorithm-diff-perl libalgorithm-diff-xs-perl
  libalgorithm-merge-perl libasan6 libatomic1 libbinutils libc-dev-bin libc-devtools libc6-dev
  libcc1-0 libcrypt-dev libctf-nobfd0 libctf0 libexpat1-dev libfakeroot libgcc-10-dev libgcc-s1
  libgomp1 libitm1 libjs-jquery libjs-sphinxdoc libjs-underscore liblsan0 libnsl-dev libpython3-dev
  libpython3.9 libpython3.9-dev libpython3.9-minimal libpython3.9-stdlib libquadmath0 libstdc++-10-dev
  libstdc++6 libtirpc-dev libtsan0 libubsan1 linux-libc-dev lto-disabled-list make manpages-dev
  python-pip-whl python3-dev python3-distutils python3-lib2to3 python3-setuptools python3-wheel
  python3.9 python3.9-dev python3.9-minimal rpcsvc-proto zlib1g-dev
Suggested packages:
  binutils-doc debian-keyring g++-multilib g++-10-multilib gcc-10-doc gcc-multilib autoconf automake
  libtool flex bison gcc-doc gcc-10-multilib gcc-10-locales apache2 | lighttpd | httpd glibc-doc
  libstdc++-10-doc make-doc python-setuptools-doc python3.9-venv python3.9-doc binfmt-support
The following NEW packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu build-essential dpkg-dev fakeroot g++ g++-10 gcc
  gcc-10 javascript-common libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl
  libasan6 libatomic1 libbinutils libc-dev-bin libc-devtools libc6-dev libcc1-0 libcrypt-dev
  libctf-nobfd0 libctf0 libexpat1-dev libfakeroot libgcc-10-dev libitm1 libjs-jquery libjs-sphinxdoc
  libjs-underscore liblsan0 libnsl-dev libpython3-dev libpython3.9-dev libquadmath0 libstdc++-10-dev
  libtirpc-dev libtsan0 libubsan1 linux-libc-dev lto-disabled-list make manpages-dev python-pip-whl
  python3-dev python3-distutils python3-pip python3-setuptools python3-wheel python3.9-dev
  rpcsvc-proto zlib1g-dev
The following packages will be upgraded:
  gcc-11-base libgcc-s1 libgomp1 libpython3.9 libpython3.9-minimal libpython3.9-stdlib libstdc++6
  python3-lib2to3 python3.9 python3.9-minimal
10 upgraded, 53 newly installed, 0 to remove and 253 not upgraded.
Need to get 56.0 MB/63.3 MB of archives.
After this operation, 216 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 binutils-common amd64 2.36.1-6ubuntu1 [217 kB]

Step 11:Installing the python package pywinrm by using following command

root@linuxhelp:~# pip install pywinrm
Collecting pywinrm
  Downloading pywinrm-0.4.2-py2.py3-none-any.whl (44 kB)
Requirement already satisfied: requests>=2.9.1 in /usr/lib/python3/dist-packages (from pywinrm) (2.25.1)
Collecting requests-ntlm>=0.3.0
  Downloading requests_ntlm-1.1.0-py2.py3-none-any.whl (5.7 kB)
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from pywinrm) (1.15.0)
Collecting xmltodict
  Downloading xmltodict-0.12.0-py2.py3-none-any.whl (9.2 kB)
Collecting ntlm-auth>=1.0.2
  Downloading ntlm_auth-1.5.0-py2.py3-none-any.whl (29 kB)
Requirement already satisfied: cryptography>=1.3 in /usr/lib/python3/dist-packages (from requests-ntlm>=0.3.0->pywinrm) (3.3.2)
Installing collected packages: ntlm-auth, xmltodict, requests-ntlm, pywinrm
Successfully installed ntlm-auth-1.5.0 pywinrm-0.4.

Step 12:Adding the windows client in ansible inventory

root@linuxhelp:~# vi /etc/ansible/hosts


[windows]
192.168.6.104

[windows:vars]
ansible_user=Admin
ansible_password=Admin@123
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

Step 13: Checking the connection with the windows client system

root@linuxhelp:~# ansible windows -m win_ping
192.168.6.104 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Step 14: Creating an ansible playbook for creating a directory

root@linuxhelp:~# vi test.yml 

- hosts: windows
  gather_facts: true
  tasks:
  - name: create directory
    win_file:
             path: c:\Ansible
             state: directory

Step 15: Checking the syntax of the playbook by using following command

root@linuxhelp:~# ansible-playbook test.yml --syntax-check

playbook: test.yml

Step 16: Run the ansible playbook by using following command

root@linuxhelp:~# ansible-playbook test.yml 

PLAY [windows] *****************************************************************************************

TASK [Gathering Facts] *********************************************************************************
ok: [192.168.6.104]

TASK [create directory] ********************************************************************************
changed: [192.168.6.104]

PLAY RECAP *********************************************************************************************
192.168.6.104              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Step 17: Hence a folder named “Ansible” is created in windows client system

with this configuration of windows client setup for ansible have been completed.

FAQ
Q
Which port does winRM uses ?
A
WinRM uses 5986 port in windows.
Q
How Ansible connect to windows servers ?
A
Ansible connects to windows servers by use of WinRM..
Q
What is WinRM ?
A
WinRM is a service in windows which is used to communicate remotly.
Q
What is python3-pip
A
Pip is a package-management system written in Python used to install and manage python packages.
Q
What is a playbook in Ansible ?
A
Collection of tasks is known as play and Collection of plays is known as Playbook.