Ansible Pilot

Create an empty file - Ansible module file

How to create an empty ~/example.txt file or update the time of an already create file.

Create an empty file - Ansible module file

How to create an empty ~/example.txt file or update the time of an already create file.

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

Today we’re talking about the Ansible module file. The full name is ansible.builtin.file, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years. It works in a different variety of operating systems. It manages files and file properties. For Windows targets, use the ansible.windows.win_file module instead.

Main Parameters

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.

demo

Let’s jump into a real-life playbook on how to create an empty file with Ansible.

---
- name: file module demo
  hosts: all
  vars:
    myfile: "~/example.txt"
  tasks:
    - name: Creating an empty file
      ansible.builtin.file:
        path: "{{ myfile }}"
        state: touch

code with ❤️ in GitHub

Recap

Now you know how to create an empty file or update access time 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

September 17, 2021

FREE Top 10 Best Practices

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