Ansible Pilot

Create a directory on Windows-like systems - Ansible module win_file

How to automate the creation of "example" folder in the user Desktop on Windows-like hosts with Ansible Playbook.

How to create a directory in Windows-like systems with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot

Ansible create a directory

Today we’re talking about the Ansible module win_file. The full name is ansible.windows.win_file, which means that is part of the collection of modules to interact with windows machines. It’s a module pretty stable and out for years. It creates, touches, or removes files or directories. For Linux targets, use the ansible.builtin.file module instead

Main Parameters

This module has some parameters to perform different tasks. The only required is “path”, where you specify the filesystem path of the file you’re going to edit. The state defines the type of object we are modifying, the default is “file” but for our use case, we need the “directory” option.

demo

How to create an “example” directory/folder in the Desktop of the user on Windows-like systems with Ansible Playbook.

code

---
- name: win_file module demo
  hosts: all
  vars:
    mydir: 'C:\Users\vagrant\Desktop\example'
  tasks:
    - name: Create a directory
      ansible.windows.win_file:
        path: "{{ mydir }}"
        state: directory

execution

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory create\ directory/directory_create_windows.yml
PLAY [win_file module demo] ***********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [Create a directory] *************************************************************************
changed: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

idempotency

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory create\ directory/directory_create_windows.yml
PLAY [win_file module demo] ***********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [Create a directory] *************************************************************************
ok: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

before execution

win_file before execution

after execution

win_file after execution

code with ❤️ in GitHub

Recap

Now you know how to create a directory in Windows-like systems with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Want to keep this project going? Please donate

Trustpilot
Follow me

Subscribe not to miss any new releases

March 14, 2022

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress