I am going to present you many operations that an administrator can carry out on a distant Home windows system utilizing ansible-playbook.
Ansible is likely one of the most generally used DevOps instruments available on the market at present. It gives quite a lot of Home windows modules which might be used to configure and handle the Home windows server. I assume you’ve already put in Ansible on Home windows from the place you wish to handle the Home windows servers.
The next are some widespread duties carried out by Home windows directors each day. You may be amazed at how straightforward it’s to handle Home windows utilizing Ansible.
The IP tackle of my Ansible Home windows controller machine is 192.168.0.106 and the IP tackle of my distant Home windows system is 192.168.0.102. Earlier than you get began, ensure you have a win_ping
module to verify in the event you can hook up with the Home windows Distant Server or not.
Geekflare@MSEDGEWIN10 ~
$ ansible win -m win_ping
192.168.0.102 | SUCCESS => {
"modified": false,
"ping": "pong"
}
My connection to a distant host is profitable.
So let’s get began with Ansible Playbooks…
Copy recordsdata
win_copy is an anible module that copies a file from the native server to a distant Home windows host. I’ll use this module to repeat one PDF.
Use the YAML code under and specify the supply and vacation spot paths.
Geekflare@MSEDGEWIN10 ~
$ vi copy.yml
---
- hosts: win
duties:
- title: Copy File
win_copy:
src: C:output.pdf
dest: C:ansible_examples
remote_src: sure
Run the ansible playbook for win_copy.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook copy.yml
PLAY [win] ***********************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
okay: [192.168.0.102]
TASK [Copy File] *****************************************************************************************************************************
modified: [192.168.0.102]
PLAY RECAP ***********************************************************************************************************************************
192.168.0.102
: okay=2 modified=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
The file has been efficiently copied to the vacation spot location on a distant Home windows system.
Set up/Uninstall MSI
To put in an utility utilizing the MSI file, you will need to use win_get_url to state the trail of the MSI file to obtain after which the win_package module to put in it. The present state implies that the MSI is being put in on the machine and the applying is in its present state.
Right here I set up Apache.
YAML code to make use of:
Geekflare@MSEDGEWIN10 ~
$ vi msi.yml
---
- title: Putting in Apache MSI
hosts: win
duties:
- title: Obtain the Apache installer
win_get_url:
url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi
dest: C:ansible_exampleshttpd-2.2.25-win32-x86-no_ssl.msi
- title: Set up MSI
win_package:
path: C:ansible_exampleshttpd-2.2.25-win32-x86-no_ssl.msi
state: current
Run the ansible playbook to put in with MSI.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook msi.yml
PLAY [Installing Apache MSI] *****************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
okay: [192.168.0.102]
TASK [Download the Apache installer] *********************************************************************************************************
modified: [192.168.0.102]
TASK [Install MSI] ***************************************************************************************************************************
modified: [192.168.0.102]
PLAY RECAP ***********************************************************************************************************************************
192.168.0.102
: okay=3 modified=2 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
Now go to the Home windows system and verify if the Apache utility is put in efficiently.
C:Usersgeekflare>cd C:Program Recordsdata (x86)Apache Software program FoundationApache2.2bin
C:Program Recordsdata (x86)Apache Software program FoundationApache2.2bin>httpd -v
Server model: Apache/2.2.25 (Win32)
Server constructed: Jul 10 2013 01:52:12
You too can set up purposes utilizing MSI with arguments. Beneath is similar instance as above, however as a substitute of a standing we use an set up argument to put in apache.
YAML code to make use of:
---
- title: Putting in Apache MSI
hosts: win
duties:
- title: Obtain the Apache installer
win_get_url:
url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi
dest: C:ansible_exampleshttpd-2.2.25-win32-x86-no_ssl.msi
- title: Set up MSI
win_package:
path: C:ansible_exampleshttpd-2.2.25-win32-x86-no_ssl.msi
arguments:
- /set up
- /passive
- /norestart
To uninstall an utility utilizing the MSI file, you will need to delete the win_package module. The absent standing implies that the applying is uninstalled utilizing the MSI file.
Right here I uninstall Apache.
Geekflare@MSEDGEWIN10 ~
$ vi uninstall_msi.yml
---
- title: UnInstalling Apache MSI
hosts: win
duties:
- title: UnInstall MSI
win_package:
path: C:ansible_exampleshttpd-2.2.25-win32-x86-no_ssl.msi
state: absent
Run the ansible playbook to uninstall with MSI.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook uninstall_msi.yml
PLAY [UnInstalling Apache MSI] *****************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
okay: [192.168.0.102]
TASK [UnInstall MSI] *************************************************************************************************************************
modified: [192.168.0.102]
PLAY RECAP ***********************************************************************************************************************************
192.168.0.102
: okay=2 modified=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
Now once I verify the Apache model I get the under output as soon as the applying is uninstalled.
C:Program Recordsdata (x86)Apache Software program FoundationApache2.2bin>httpd -v 'httpd' is just not acknowledged as an inside or exterior command,
operable program or batch file.
Uninstall Software program (.EXE)
You too can uninstall software program with an .exe file utilizing that software program’s product ID.
Geekflare@MSEDGEWIN10 ~
$ vi uninstall.yml
---
- hosts: win
duties:
- title: Uninstall 7-Zip from the exe
win_package:
path: C:Program Files7-ZipUninstall.exe
product_id: 7-Zip
arguments: /S
state: absent
Run the ansible playbook to uninstall 7-Zip.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook uninstall.yml
PLAY [win] *************************************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************************
okay: [192.168.0.102]
TASK [Uninstall 7-Zip from the exe] ***********************************************************************************************************************************************************
modified: [192.168.0.102]
PLAY RECAP *************************************************************************************************************************************************************************************
192.168.0.102 : okay=2 modified=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Cease/begin/restart Home windows Providers
win_service ansible module is used to begin, cease or restart a service. Right here I present you how you can cease the tomcat service.
You could embody the service title within the YAML file and set the standing to cease.
Geekflare@MSEDGEWIN10 ~
$ vi service.yml
---
- hosts: win
duties:
- title: Cease service Tomcat
win_service:
title: Tomcat8
state: stopped
Run the ansible playbook to cease the tomcat service.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook service.yml
PLAY [win] ***********************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
okay: [192.168.0.102]
TASK [Stop service Tomcat] ****************************************************************************************************************
modified: [192.168.0.102]
PLAY RECAP ***********************************************************************************************************************************
192.168.0.102
: okay=2 modified=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
When you verify the tomcat service on the Home windows system, it’s now within the stopped state.
You may outline the standing as began, restarted or paused to vary the standing of the service.
Gather info
Utilizing win_disk_facts ansible module, you’ll be able to retrieve all disk data from the goal host.
Geekflare@MSEDGEWIN10 ~
$ vi disk.yml
---
- hosts: win
duties:
- title: Get disk info
win_disk_facts:
- title: Output first disk dimension
debug:
var: ansible_facts.disks[0].dimension
- title: Convert first system disk into numerous codecs
debug:
msg: '{{ disksize_gib }} vs {{ disksize_gib_human }}'
vars:
# Get first system disk
disk: '{selectattr("system_disk")}'
# Present disk dimension in Gibibytes
disksize_gib_human: '{filesizeformat(true) }'
disksize_gib: '{spherical} GiB'
Run the ansible playbook to get the disk data.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook disk.yml
PLAY [win] ***********************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
okay: [192.168.0.102]
TASK [Get disk facts] ************************************************************************************************************************
okay: [192.168.0.102]
TASK [Output first disk size] ****************************************************************************************************************
okay: [192.168.0.102] => {
"ansible_facts.disks[0].dimension": "1000204886016"
}
TASK [Convert first system disk into various formats] ****************************************************************************************
okay: [192.168.0.102] => {
"msg": "932 GiB vs 931.5 GiB"
}
PLAY RECAP ***********************************************************************************************************************************
192.168.0.102
: okay=4 modified=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
Utilizing win_command ansible module, you’ll be able to run instructions on the distant host and get CPU data, machine data, and way more.
Geekflare@MSEDGEWIN10 ~
$ vi verify.yml
---
- hosts: win
duties:
- title: Get disk info
win_command: wmic cpu get caption, deviceid, title, numberofcores, maxclockspeed, standing
register: utilization
- debug: msg="{{ utilization.stdout }}"
Run the ansible playbook to get distant system data.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook verify.yml
PLAY [win] ***********************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
okay: [192.168.0.102]
TASK [Get facts] ************************************************************************************************************************
modified: [192.168.0.102]
TASK [debug] *********************************************************************************************************************************
okay: [192.168.0.102] => {
"msg": "Caption DeviceID MaxClockSpeed
Identify
NumberOfCores Standing rrnIntel64 Household 6 Mannequin 142 Stepping 9 CPU0 2712 Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz 2 OK rrnrrn"
}
PLAY RECAP ***********************************************************************************************************************************
192.168.0.102
: okay=3 modified=1 unreachable=0 failed=0
skipped=0 rescued=0
ignored=0
Execute instructions
No matter instructions you run in a window, they are often executed via the weatherwort win_command module. You simply must specify the command in your YAML file. Right here I simply create a folder.
Geekflare@MSEDGEWIN10 ~
$ vi instructions.yml
---
- hosts: win
duties:
- title: run an executable utilizing win_command
win_command: whoami.exe
- title: run a cmd command
win_command: cmd.exe /c mkdir C:check
Run the ansible playbook to carry out the win_command operation.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook instructions.yml
PLAY [win] ***********************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
okay: [192.168.0.102]
TASK [run an executable using win_command] ***************************************************************************************************
modified: [192.168.0.102]
TASK [run a cmd command] *********************************************************************************************************************
modified: [192.168.0.102]
PLAY RECAP ***********************************************************************************************************************************
192.168.0.102
: okay=3 modified=2 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
Setting variables
A Home windows system has a number of setting variables, for instance JAVA_HOME. The habits win_environment ansible module means that you can add or change setting variables on a Home windows system. On this instance, I’m including a brand new variable to the record of Home windows setting variables.
Geekflare@MSEDGEWIN10 ~
$ vi env.yml
---
- hosts: win
duties:
- title: Set an setting variable for all customers
win_environment:
state: current
title: NewVariable
worth: New Worth
stage: machine
Run the ansible playbook so as to add the setting variable to a distant Home windows machine.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook env.yml
PLAY [win] ***********************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
okay: [192.168.0.102]
TASK [Set an environment variable for all users] *********************************************************************************************
modified: [192.168.0.102]
PLAY RECAP ***********************************************************************************************************************************
192.168.0.102
: okay=2 modified=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
Go to the setting variables window; you will notice the brand new variable you simply added is right here.
Add/edit registry
win_regedit The ansible module is used so as to add or edit registry information on a distant Home windows machine. You could specify the trail of the registry and the contents to be added/up to date. Right here I create a brand new GeekFlare registry entry inside HKLM:SOFTWARE path after which add title and information to this registry.
Geekflare@MSEDGEWIN10 ~
$ vi registry.yml
---
- hosts: win
duties:
- title: Making a registry
win_regedit:
path: HKLM:SOFTWAREGeekFlare
- title: Modifying a registry, including title and information
win_regedit:
path: HKLM:SOFTWAREGeekFlare
title: Geek
information: Flare
Run the ansible playbook so as to add the registry.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook registry.yml
PLAY [win] ***********************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
okay: [192.168.0.102]
TASK [Creating a registry] *******************************************************************************************************************
modified: [192.168.0.102]
TASK [Modifying a registry, adding name and data] ********************************************************************************************
modified: [192.168.0.102]
PLAY RECAP ***********************************************************************************************************************************
192.168.0.102
: okay=3 modified=2 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
When you go to the Registry Editor on the distant system, you will notice that this registry has been efficiently added with the Identify and Knowledge parameters.
Delete log
win_eventlog The ansible module is used so as to add, clear or delete Home windows occasion logs from the Home windows system.
Go to Home windows Powershell and record the EventLogs current on the distant Home windows machine.
PS C:UsersGeekflare> Get-EventLog -Checklist
Max(Okay) Retain OverflowAction Entries Log
------ ------ -------------- ------- ---
20,480 0 OverwriteAsNeeded 33,549 Utility
20,480 0 OverwriteAsNeeded 0 HardwareEvents
512 7 OverwriteOlder 20 Web Explorer
20,480 0 OverwriteAsNeeded 0 Key Administration Service
128 0 OverwriteAsNeeded 190 OAlerts
Safety
20,480 0 OverwriteAsNeeded 44,828 System
15,360 0 OverwriteAsNeeded 3,662 Home windows PowerShell
Now I’ll present you how you can delete logs from all sources for Web Explorer.
Geekflare@MSEDGEWIN10 ~
$ vi log.yml
---
- hosts: win
duties:
- title: Take away Web Explorer Logs
win_eventlog:
title: Web Explorer
state: absent
Run the ansible playbook to take away Web Explorer from the distant Home windows machine.
Geekflare@MSEDGEWIN10 ~
$ ansible-playbook log.yml
PLAY [win] *************************************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************************
okay: [192.168.0.102]
TASK [Remove Internet Explorer Logs] **********************************************************************************************************************************************
modified: [192.168.0.102]
PLAY RECAP *************************************************************************************************************************************************************************************
192.168.0.102 : okay=2 modified=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
When you now re-list the EventLogs, you will notice that the Web Explorer logs have been eliminated.
PS C:UsersGeekflare> Get-EventLog -Checklist
Max(Okay) Retain OverflowAction Entries Log
------ ------ -------------- ------- ---
20,480 0 OverwriteAsNeeded 33,549 Utility
20,480 0 OverwriteAsNeeded 0 HardwareEvents
20,480 0 OverwriteAsNeeded 0 Key Administration Service
128 0 OverwriteAsNeeded 190 OAlerts
Safety
20,480 0 OverwriteAsNeeded 44,835 System
15,360 0 OverwriteAsNeeded 56 Home windows PowerShell
In order that was all about Ansible playbooks, which can be utilized for distant Home windows administration. Go forward and check out these playbooks. You too can attempt different accessible Ansible Home windows modules.