AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible win_file Module: Create Directory on Windows Hosts (Guide) — Video Tutorial
How to create directories on Windows with Ansible win_file module (ansible.windows.win_file). Set paths, manage folders, handle permissions.
What You'll Learn
- How to create a directory in Windows-like systems with Ansible?
- Ansible create a directory
- Main Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
Full Tutorial Content
How to create a directory in Windows-like systems with Ansible?
I'm going to show you a live Playbook and some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot
Ansible create a directory
> `ansible.windows.win_file` Creates, touches, or removes files or directories
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
- `path` path - file path
- `state` string - `file`/`absent`/`directory`/`touch`
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.
Links
- [ansible.windows.win_file](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_file_module.html)
## Playbook
How to create an "example" directory/folder in the Desktop of the user on Windows-like systems with Ansible Playbook.
code
```yaml
---
- 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
```bash
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
```bash
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 executio
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 2 min
- Category: troubleshooting
Read the full written article: Ansible win_file Module: Create Directory on Windows Hosts (Guide)