Look at Windows Module of Ansible

Reading Time: 2 minutes

Hello Everyone! Today we will learn how we can create Windows Module in Ansible.

When using Ansible to manage Windows, many of the syntax and rules that apply for Unix/Linux hosts also apply to Windows, but there are still some differences when it comes to components like path separators and OS-specific tasks. This document covers details specific to using Ansible for Windows.

Some of the Windows Modules used in Ansible: –

  • win_copy
  • win_file
  • win_command
  • win_stat
  • win_unzip

Script on given Ansible Windows Module

  1. win_copy
---
- hosts: web
  tasks:
    - name: Copy file with owner and permissions
      win_copy:
        src: ./commands.txt
        dest: C:\Users\Administrator\Downloads\
      

Here, “src” contains the current working directory which stores “commands.txt” file that is going to be copied on the Destination path

2. win_unzip

---
- name: Unarchive a file
  hosts: web
  become: yes
  tasks:
  - name: Unarchive a file that is already on the remote machine
    win_unzip:
      src: /Downloads/mysql-server_8.0.22-1debian10_amd64.deb-bundle.tar
      dest: /Downloads/
      remote_src: yes

Here, “src” contains the current working directory which stores “sql package” that needs to unzip/unarchive on the Destination path.

3. win_command

---
- hosts: web
  tasks:
   - name: Run command to create a folder
     win_command: cmd.exe /c mkdir C:\test
  

Here, the command consists of creating new directory in the give path.

4.win_stat

- name: Obtain information about a file
  win_stat:
    path: D:\foo.ini
  register: file_info

Here, it gives the information the file.

5. win_file

---
- hosts: web
  tasks:
  - name: Touch a file (creates if not present, updates modification time if present)
    win_file:
      path: D:\file.txt
      state: touch

Here, it will create a file when it is not present on the given path.

Apart, from the given modules above there are more modules which are given in the official document.

Modules in Ansible can also be created by the developer also. For more information click.

References :

https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html

https://docs.ansible.com/ansible/latest/user_guide/windows_usage.html

Written by 

VIkas Vashisth is a Software Consultant and having experience in DevOps. He loves to know about new technology.

Leave a Reply