How to create an empty file 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 creates an empty file

  • 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](/articles/create-an-empty-file-ansible-module-file)) instead.

Parameters

  • path path - file path
  • state string - file/absent/directory/touch

This module has some parameters to perform any 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 "touch" option.

Links

  • [ansible.windows.win_file](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_file_module.html)

Playbook

Create an empty file in Windows-like systems with Ansible playbook

code

``yaml

---

  • name: win_file module demo

hosts: all

become: false

gather_facts: false

vars:

myfile: 'C:\Users\vagrant\Desktop\example.txt'

tasks:

- name: Creating an empty file

ansible.windows.win_file:

path: "{{ myfile }}"

state: touch

`

execution

`bash

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory create\ file/file-windows.yml

PLAY [win_file module demo] *

TASK [Creating an empty file] *

changed: [WindowsServer]

PLAY RECAP **

WindowsServer : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

ansible-pilot $

`

idempotency not valid

`bash

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory create\ file/file-windows.yml

PLAY [win_file module demo] *

TASK [Creating an empty file] *

changed: [WindowsServer]

PLAY RECAP **

WindowsServer : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

ansible-pilot $

``

before execution

![win_file before execution](/articles/ansible_module_win_file1.jpg)

after execution

![win_file after execution](/articles/ansible_module_win_file2.jpg)

[code with ❤️ in GitHub](https://github.c