Setting up Windows Host using Ansible

#Add the PPA to the system, and the middle need case Enter to accept the PPA increase

sudo apt-add-repository ppa:ansible/ansible

Image for post
Image for post

#Update and install

sudo apt-get update
sudo apt-get install -y ansible

2. Install pywinrm

sudo apt-get install -y python-pip
pip install pywinrm

3. Configure ansible

Ansible knows all the servers through the hosts file. We need to set up this file before we can start connecting with our other machines

host file path 
/etc/ansible/hosts
Change the command
sudo vi /etc/ansible/hosts
Change content
[random group name]
random name ansible_ssh_host=the ip of the machine to be connected
ex:
[windows]
test ansible_ssh_host=127.0.0.1
Or (in this case, you need to use IP to control the command separately)
[windows]
127.0.0.1
Image for post
Image for post
Image for post
Image for post

4. Add a group folder, the windows connection will need to enter the remote account password, so we create group_vars under ansible and enter the remote settings

PS. The file name must be the same as the group to read the contents of the YMAL file

sudo mkdir /etc/ansible/group_varssudo vi /etc/ansible/group_vars/windows.ymlansible_user: remote account 
ansible_password: remote password
ansible_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore
Image for post
Image for post
Image for post
Image for post

5. Make a powershell script, this article uses windows server 2016 Datacenter

Right-click on the desktop → Add → File Folder

Image for post
Image for post

The upper right arrow of any folder → click on page view → check the file extension, so you can see the file extension

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Name ConfigureRemotingForAnsible.ps1 and paste the content of the URL, the content of the file: https://tinyurl.com/y65bradw

Change the file name after pasting the content.ps1

Image for post
Image for post

6. Open the powershell setting on windows. This article is using windows server 2016 Datacenter. You can skip to step 7 without the following steps

Open powershell, search at the bottom left → type powershell → right click to run as a system administrator

Image for post
Image for post

# Install .NET Framework 4.5

Change the powershell policy to remotesigned and enter y to confirmSet-ExecutionPolicy remotesignedCheck strategyGet-ExecutionPolicy
Image for post
Image for post

View version (must be 3.0 or above)

$PSVersionTable.PSVersion

Image for post
Image for post

Open Winrm service

winrm enumerate winrm/config/listener

If there is no response, it means that the service is not started, and it is not started by default

Image for post
Image for post

7. Basic configuration of winrm service

winrm quickconfig

If the setting has been activated, it will need to be modified with commands

Image for post
Image for post
winrm service configuration auth

winrm set winrm/config/service/auth'@{Basic="true"}'
Image for post
Image for post
Winrm service configures the encryption mode to allow non-encryptionwinrm set winrm/config/service’@{AllowUnencrypted=”true”}’
Image for post
Image for post

Place the ConfigureRemotingForAnsible.ps1 path to execute the configuration winrm and https certificate message

powershell.exe -File ConfigureRemotingForAnsible.ps1

Image for post
Image for post

Check config content

winrm get winrm/config

Image for post
Image for post

8. The test, as shown in the figure below, is successful. Then you can issue other commands to control the machine

ansible all -m win_pingansible test -m win_ping (specify a single host)ansible 127.0.0.1 -m win_ping (specify a single host)ansible windows -m win_ping (specify group)
Image for post
Image for post
Image for post
Image for post

9. Operate the machine by command

ansible name -m win_command -a “command”

ex: ansible windows -m win_command -a “shutdown -r -t 10” (remote operation restart command)

ansible 127.0.0.1 -m win_ping (specify a single host)

ansible windows -m win_ping (specify group)

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store