Updated April 14, 2023
Definition of Ansible Windows Modules
Ansible windows modules are those Ansible modules that are used to do changes on Microsoft Windows machines. The syntax and behavior of these modules are the same as other Linux based modules with different OS specific tasks and path separators. Ansible windows modules are used to orchestrate tasks on Microsoft Windows machines. These modules are developed on PowerShell mostly rather than Python. Also, the connection method like WinRM or similar Microsoft Windows supported ones are used in this case rather than SSH.
Syntax and Parameters
Ansible Windows Modules have a set of parameters and acceptable values. Also for some of the parameters have default values. So it becomes more important to learn and understand the various possibilities. These modules have similar syntax as any other module. Only we need to take care of parameters to pass in the playbook to achieve our task requirements.
Below is a list of some of the important Ansible Windows Modules with a short description. Please note this list is only for Microsoft Windows environment and not exhaustive as there are many more modules which Ansible community is keep on developing:
- win_chocolatey: This is used to manage packages.
- win_chocolatey_facts: To create the collection of facts for use in chocolatey.
- win_chocolatey_source: To do the management of chocolatey sources.
- win_chocolatey_feature: To do the management of chocolatey features.
- win_acl: To set the permission of files, directory, or registry for a system group or user.
- win_command: To run a command on Windows PowerShel
- win_copy: To copy files to windows hosts.
- win_disk_facts: To display the attached disks and related information on windows targets.
- win_dns_record: To perform DNS record management.
- win_domain: To ensure the Windows domain existence.
- win_domain_controller: To manage a domain controller.
- win_domain_computer: To manage the Active directory Computers.
- win_domain_user: To manage Active Directory Users.
- win_environment: To modify the environment variables.
- win_eventlog: To Manage Windows Event.
- win_feature: To uninstalling and installing any Windows Feature on Windows.
- win_file: To manage files.
- win_find: To find files.
- win_firewall: To manage Windows.
- win_format: To format.
- win_get_url: To download files over HTTP/HTTPS/FTP.
- win_hostname: To manage the computer name.
- win_mapped_drive: To mapping the network drives.
- win_package: To do package installation or uninstallation.
- win_ping: To test the working setup with windows
- win_reboot: To reboot a machine.
- win_regedit: To do regist management.
- win_service: To manage the services
- win_updates: To do a windows update.
- win_wait_for: To wait for a condition to fulfill before continuing in the playbook
- win_user: To manage local windows user.
- win_wait_for_process: To wait for a process to not exist or exist before continuing in the playbook.
- win_webpicmd: Install packages on target machines using Web platform installer CLI.
- win_whoami: To get the current user details.
- win_iis_virtualdirectory: To configure a virtual directory on IIS.
- win_iis_website: To configure a website on IIS.
- win_lininfile: To put a line in file.
Each module has a separate set of parameters and options. you can refer to the below link for the latest Ansible Windows modules, on this page you can click on a module link to get a detailed picture of that module.
Working of Ansible Windows Modules
In Ansible, playbooks are written on YAML format and every module is written inside a playbook must follow indentation and spacing. Ansible Windows modules work similarly. But there are a few points which one must remember before starting to use or trying to start the use of Ansible Windows modules.
- PowerShell 3.0 and .NET 4.0 at least must be installed on target Windows
- A WinRM listener should be created if not existing and
- In the latest release of Ansible, Win32-openSSH support was provided, but that is experimental currently. It might work or not in your case. So try it first in Lab
- Besides WinRM, there are other connection methods like Basic,
- As Windows is not a POSIX operating system, so there are many limitations, thus better you must try every setup in Labs
- For custom new modules, you can create module code in PowerShell and then documentation in Python with the same file name
Examples of Ansible Windows Modules
In this section, we will do some practical examples. We will create playbooks on the Ansible controller node named Ansible1. Also, we have target Windows machines as windows and WinServer1.lablad1.local. Using the playbooks, we try to make changes or fetch data from a remote target Windows machine.
Example #1
In this example, we will simply use Ansible win_ping module to check the connection and authentication success from the Ansible controller node to target windows machine. We use the below command for the same.
ansible window -m win_ping
In the output, you can see that connection was successfully checked.
Example #2
In this example, we have a remote host named as WinServer1.lablad1.local. On which we will run the command using Ansible windows module named win_command. Then register the output in a variable named netstat. To print the output contained in variable netstat, we will use the Ansible module debug. For this task, we have a playbook like below:
---
-name: test cmd from win_command module hosts: lablad1.local
tasks:
-name: run an executable command on remote Windows system win_command: netstat -e
register: netstat
-debug: var=netstat
Now run this playbook like below:
ansible-playbook ansible_win_command.yaml
The Output will look like below, where you can see above, in stdout_lines, all output by running netstat
command on target machine:
Conclusion
As we saw in this article, Ansible Windows modules are very useful and a bit tricky to use in the IT infrastructure environment. But when we have a good knowledge of these kinds of modules, we can easily do Microsoft Windows administration and operational changes. So first learn them and then use them.
Recommended Articles
This is a guide to Ansible Windows Modules. Here we also discuss the definition and working of ansible windows modules along with different examples and its code implementation. You may also have a look at the following articles to learn more –