DEV Community

Roy
Roy

Posted on

ansible create empty file if it does not exist

- name: Touch again the same file, but do not change times this makes the task idempotent
  ansible.builtin.file:
    path: /etc/foo.conf
    state: touch
    mode: u+rw,g-wx,o-rwx
    modification_time: preserve
    access_time: preserve
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
caodawei3 profile image
David Cao

Another way to create a file with Ansible is to use the template module. The template module can be used to create a new file from a template.

Templates are files that contain text that can be dynamically generated using variables and other expressions

The template module has a number of options that can be used to control the behavior of the module.

2 ways to create a file in ansible