I will evidence you several operations which an administrator can perform on a remote windows system using ansible-playbook.

Ansible is one of the virtually used DevOps tools in the market today. It provides a agglomeration of windows modules that are used to configure and manage the Windows server. I presume you already take Ansible installed on Windows from where you want to manage the Windows servers.

The following are some of the commonly used tasks performed by Windows administrators on a daily basis. Y'all will be amazed to see how easy it is administered Windows using Ansible.

My Ansible Windows controller motorcar'due south IP address is 192.168.0.106, and my remote Windows arrangement's IP accost is 192.168.0.102. Before you go started, make sure you run a win_ping module to check whether you are able to connect to windows remote server or non.

          [electronic mail protected] ~ $ ansible win -grand win_ping 192.168.0.102 | SUCCESS => {     "changed": faux,     "ping": "pong" }        

My connexion to a remote host is successful.

Then, let's get started with Ansible Playbooks…

Copying Files

win_copy is an ansible module that copies a file from the local server to a remote Windows host. I will use this module to copy a single PDF.

Apply the below YAML code, give the source and destination paths.

          [email protected] ~ $ vi re-create.yml ---  - hosts: win    tasks:    - name: Copy File      win_copy:        src: C:\output.pdf        dest: C:\ansible_examples\             remote_src: yeah        

Run the ansible-playbook for win_copy.

          [electronic mail protected] ~ $ ansible-playbook copy.yml  PLAY [win] ***********************************************************************************************************************************  Task [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102]  TASK [Copy File] ***************************************************************************************************************************** changed: [192.168.0.102]  PLAY Recap *********************************************************************************************************************************** 192.168.0.102 : ok=2 inverse=i unreachable=0 failed=0 skipped=0 rescued=0 ignored=0        

The file has been copied successfully at the destination location on a remote windows arrangement.

ansible windows copy

Install/UnInstall MSI

To install an application using the MSI file, yous need to use win_get_url to mention the path of the MSI file to download then apply the win_package module to install it. The state nowadays means the MSI will exist installed on the machine, and the application is in the present country.

Here, I am installing Apache.

YAML code to exist used:

          [e-mail protected] ~ $ vi msi.yml --- - proper name: Installing Apache MSI    hosts: win      tasks:     - proper noun: Download the Apache installer       win_get_url:         url: https://annal.apache.org/dist/httpd/binaries/win32/httpd-2.two.25-win32-x86-no_ssl.msi         dest: C:\ansible_examples\httpd-ii.two.25-win32-x86-no_ssl.msi      - proper name: Install MSI       win_package:          path: C:\ansible_examples\httpd-two.2.25-win32-x86-no_ssl.msi         land: present        

Run the ansible-playbook to install using MSI.

          [e-mail protected] ~ $ ansible-playbook msi.yml  PLAY [Installing Apache MSI] *****************************************************************************************************************  TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102]  Job [Download the Apache installer] ********************************************************************************************************* changed: [192.168.0.102]  Task [Install MSI] *************************************************************************************************************************** changed: [192.168.0.102]  PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=three changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0        

At present, get to the windows organisation and bank check if the apache awarding got installed successfully.

          C:\Users\geekflare>cd C:\Program Files (x86)\Apache Software Foundation\Apache2.ii\bin C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin>httpd -five Server version: Apache/2.2.25 (Win32) Server built: Jul 10 2013 01:52:12        

You can besides install applications using MSI with arguments. Below is the same case as to a higher place, but instead of a state, we are using an install argument to install apache.

YAML code to be used:

          ---  - name: Installing Apache MSI     hosts: win     tasks:      - name: Download the Apache installer        win_get_url:          url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-ii.2.25-win32-x86-no_ssl.msi          dest: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi       - name: Install MSI        win_package:           path: C:\ansible_examples\httpd-ii.2.25-win32-x86-no_ssl.msi          arguments:            - /install            - /passive            - /norestart        

To uninstall an application using the MSI file, y'all need to use the win_package module. The state absent means the application volition be uninstalled using the MSI file.

Here, I am uninstalling Apache.

          [electronic mail protected] ~ $ vi uninstall_msi.yml  ---  - name: UnInstalling Apache MSI     hosts: win     tasks:      - name: UnInstall MSI        win_package:           path: C:\ansible_examples\httpd-ii.2.25-win32-x86-no_ssl.msi          state: absent        

Run the ansible-playbook to uninstall using MSI.

          [email protected] ~ $ ansible-playbook uninstall_msi.yml  PLAY [UnInstalling Apache MSI] *****************************************************************************************************************  TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102]  Chore [UnInstall MSI] ************************************************************************************************************************* changed: [192.168.0.102]  PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0        

Now, if I bank check the apache version, I will get the below output every bit the application got uninstalled.

          C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin>httpd -v 'httpd' is non recognized as an internal or external command, operable plan or batch file.        

Uninstall Software (.EXE)

You can as well uninstall software with .exe file using the production id of that software.

          [email protected] ~ $ vi uninstall.yml  ---  - hosts: win     tasks:     - proper name: Uninstall vii-Null from the exe       win_package:         path: C:\Program Files\7-Null\Uninstall.exe         product_id: 7-Zilch         arguments: /S         state: absent        

Run the ansible-playbook to uninstall 7-Zip.

          [email protected] ~ $ ansible-playbook uninstall.yml  PLAY [win] *************************************************************************************************************************************************************************************  Chore [Gathering Facts] ************************************************************************************************************************************************************************* ok: [192.168.0.102]  TASK [Uninstall 7-Zip from the exe] *********************************************************************************************************************************************************** inverse: [192.168.0.102]  PLAY Epitomize ************************************************************************************************************************************************************************************* 192.168.0.102              : ok=ii    changed=i    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0        

End/Start/Restart Windows Services

win_service ansible module is used to start, stop, or restart a service. Here, I volition show you how to cease the tomcat service.

ansible windows tomcat

You lot need to mention the service name in the YAML file and set the state to terminate.

          [email protected] ~ $ half dozen service.yml --- - hosts: win     tasks:      - name: Cease service Tomcat       win_service:         proper noun: Tomcat8         country: stopped        

Run the ansible-playbook to stop the tomcat service.

          [email protected] ~ $ ansible-playbook service.yml  PLAY [win] ***********************************************************************************************************************************  Chore [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102]  TASK [Stop service Tomcat] **************************************************************************************************************** changed: [192.168.0.102]  PLAY Recap *********************************************************************************************************************************** 192.168.0.102 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0        

If y'all check the tomcat service on the windows system, information technology is at present in stopped condition.

ansible windows tomcat stop

You can define state to started or restarted or paused to change the status of the service.

Gathering Facts

Using win_disk_facts ansible module, you lot tin can retrieve all the deejay information of the target host.

          [email protected] ~ $ 6 deejay.yml --- - hosts: win    tasks:    - name: Get deejay facts     win_disk_facts:    - name: Output first disk size     debug:       var: ansible_facts.disks[0].size    - name: Convert beginning system deejay into diverse formats     debug:       msg: '{{ disksize_gib }} vs {{ disksize_gib_human }}'     vars:       # Become first system deejay       disk: '{{ ansible_facts.disks|selectattr("system_disk")|first }}'        # Evidence disk size in Gibibytes       disksize_gib_human: '{{ disk.size|filesizeformat(true) }}'        disksize_gib: '{{ (disk.size/1024|prisoner of war(3))|round|int }} GiB'        

Run the ansible-playbook to get the disk data.

          [electronic mail protected] ~ $ ansible-playbook disk.yml  PLAY [win] ***********************************************************************************************************************************  Task [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102]  TASK [Get deejay facts] ************************************************************************************************************************ ok: [192.168.0.102]  Task [Output showtime disk size] **************************************************************************************************************** ok: [192.168.0.102] => {  "ansible_facts.disks[0].size": "1000204886016" }  TASK [Catechumen get-go system disk into diverse formats] **************************************************************************************** ok: [192.168.0.102] => { "msg": "932 GiB vs 931.5 GiB" }  PLAY Epitomize *********************************************************************************************************************************** 192.168.0.102 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0        

Using win_command ansible module, you can execute commands on the remote host and get CPU information, device details, and much more.

          [email protected] ~ $ 6 cheque.yml --- - hosts: win    tasks:    - proper name: Become deejay facts      win_command: wmic cpu become caption, deviceid, proper name, numberofcores, maxclockspeed, status      register: usage     - debug: msg="{{ usage.stdout }}"        

Run the ansible-playbook to get remote system information.

          [email protected] ~ $ ansible-playbook check.yml  PLAY [win] ***********************************************************************************************************************************  TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102]  TASK [Get facts] ************************************************************************************************************************ changed: [192.168.0.102]  Task [debug] ********************************************************************************************************************************* ok: [192.168.0.102] => { "msg": "Explanation DeviceID MaxClockSpeed Name NumberOfCores Status \r\r\nIntel64 Family six Model 142 Stepping 9 CPU0 2712 Intel(R) Core(TM) i5-7200U CPU @ two.50GHz ii OK \r\r\n\r\r\n" }  PLAY Epitomize *********************************************************************************************************************************** 192.168.0.102 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0        

Running Commands

Whatever commands yous run on a window, they can be run through the ansible win_command module. Yous merely need to specify the command in your YAML file. Hither, I am just creating a directory.

          [e-mail protected] ~ $ vi commands.yml ---  - hosts: win     tasks:     - name: run an executable using win_command       win_command: whoami.exe      - name: run a cmd command        win_command: cmd.exe /c mkdir C:\test        

Run the ansible-playbook to perform win_command functioning.

          [email protected] ~ $ ansible-playbook commands.yml  PLAY [win] ***********************************************************************************************************************************  Job [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102]  Task [run an executable using win_command] *************************************************************************************************** changed: [192.168.0.102]  Chore [run a cmd command] ********************************************************************************************************************* changed: [192.168.0.102]  PLAY Epitomize *********************************************************************************************************************************** 192.168.0.102 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0        

Environment Variables

A windows arrangement has multiple surroundings variables, for case, JAVA_HOME. Using the win_environment ansible module, you lot can add or modify surroundings variables on a windows organization. In this case, I am adding a new variable to the windows environment variables listing.

          [electronic mail protected] ~ $ half dozen env.yml --- - hosts: win    tasks:    - name: Set an environment variable for all users      win_environment:        country: nowadays        proper noun: NewVariable        value: New Value        level: machine        

Run the ansible-playbook to add the environment variable on a remote windows motorcar.

          [email protected] ~ $ ansible-playbook env.yml  PLAY [win] ***********************************************************************************************************************************  TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102]  Task [Set an environment variable for all users] ********************************************************************************************* changed: [192.168.0.102]  PLAY Recap *********************************************************************************************************************************** 192.168.0.102 : ok=2 changed=ane unreachable=0 failed=0 skipped=0 rescued=0 ignored=0        

Get to the environs variables window; you will see the new variable yous just added is present hither.

ansible windows variables

Add/Edit Registry

win_regedit ansible module is used to add or edit registry details on a remote windows machine. You need to give the path of the registry and content to be added/updated. Here I am creating a new registry entry GeekFlare inside HKLM:\SOFTWARE path and and then calculation name and data to this registry.

          [e-mail protected] ~ $ vi registry.yml ---  - hosts: win     tasks:     - name: Creating a registry       win_regedit:        path: HKLM:\SOFTWARE\GeekFlare     - name: Modifying a registry, adding name and data       win_regedit:        path: HKLM:\SOFTWARE\GeekFlare        name: Geek        information: Flare        

Run the ansible-playbook to add the registry.

          [email protected] ~ $ ansible-playbook registry.yml  PLAY [win] ***********************************************************************************************************************************  Task [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102]  Task [Creating a registry] ******************************************************************************************************************* changed: [192.168.0.102]  TASK [Modifying a registry, adding proper noun and information] ******************************************************************************************** changed: [192.168.0.102]  PLAY Epitomize *********************************************************************************************************************************** 192.168.0.102 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0        

If you become to Registry Editor on the remote system, you lot can run across this registry has been added successfully with Name and Information parameters.

ansible windows registry

Delete Log

win_eventlog ansible module is used to add together, clear, or remove windows event logs from the windows system.

Go to Windows Powershell, and list the EventLogs nowadays on the remote windows machine.

          PS C:\Users\Geekflare> Get-EventLog -List                                                                                    Max(K) Retain OverflowAction        Entries Log   ------ ------ --------------        ------- ---   20,480      0 OverwriteAsNeeded      33,549 Application   xx,480      0 OverwriteAsNeeded           0 HardwareEvents      512      7 OverwriteOlder             20 Net Explorer   20,480      0 OverwriteAsNeeded           0 Key Management Service      128      0 OverwriteAsNeeded         190 OAlerts                                               Security   20,480      0 OverwriteAsNeeded      44,828 Arrangement   15,360      0 OverwriteAsNeeded       3,662 Windows PowerShell        

Now, I volition bear witness how to remove logs from all sources for Net Explorer.

          [e-mail protected] ~ $ vi log.yml --- - hosts: win    tasks:    - name: Remove Internet Explorer Logs      win_eventlog:       name: Internet Explorer       country: absent        

Run the ansible-playbook to remove the Internet Explorer form the remote windows machine.

          [email protected] ~ $ ansible-playbook log.yml  PLAY [win] *************************************************************************************************************************************************************************************  TASK [Gathering Facts] ************************************************************************************************************************************************************************* ok: [192.168.0.102]  TASK [Remove Internet Explorer Logs] ********************************************************************************************************************************************** changed: [192.168.0.102]  PLAY RECAP ************************************************************************************************************************************************************************************* 192.168.0.102              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0        

Now, if y'all list the EventLogs again, you volition see Internet Explorer logs take been removed.

          PS C:\Users\Geekflare> Get-EventLog -List    Max(Thousand) Retain OverflowAction        Entries Log   ------ ------ --------------        ------- ---   twenty,480      0 OverwriteAsNeeded      33,549 Awarding   xx,480      0 OverwriteAsNeeded           0 HardwareEvents   xx,480      0 OverwriteAsNeeded           0 Key Direction Service      128      0 OverwriteAsNeeded         190 OAlerts                                               Security   20,480      0 OverwriteAsNeeded      44,835 System   15,360      0 OverwriteAsNeeded          56 Windows PowerShell        

Then, that was all nearly Ansible playbooks, which tin can exist used for remote windows administration. Go ahead and attempt out these playbooks. You can likewise endeavour other Ansible Windows modules bachelor.