Bootstrap FreeKB - Ansible - Create or remove a file using the file module
Ansible - Create or remove a file using the file module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The file module with state: touch is used to create a file. In this example, the /tmp/foo.txt file will be created if it does not exist.

---
- hosts: localhost
  tasks:
  - name: create /tmp/foo.txt
    ansible.builtin.file:
      path: /tmp/foo.txt
      state: touch
...

 

The task should always indicate changed, regardless if the file was or was not created. If the file does not exist, it will be created. If the file exists, it will not be overwritten. However, the date and time of the file will be updated to reflect the current date time, and the file permissions and ownership will be updated. This is similar to the behavior of the Linux touch command.

TASK [create /tmp/foo.txt]
changed: [localhost]

PLAY RECAP
localhost   : ok=1  changed=1  unreachable=0  failed=0

 

state: absent is used to remove files. In this example, the /tmp/foo.txt file will be removed if it exists.

---
- hosts: localhost
  tasks:
  - name: remove /tmp/foo.txt
    ansible.builtin.file:
      path: /tmp/foo.txt
      state: absent
...

 

The task should be changed if the file existed and was removed.

TASK [remove /tmp/foo.txt]
changed: [localhost]

PLAY RECAP
localhost   : ok=1  changed=1  unreachable=0  failed=0

 

The task should be ok if the file did not exist, thus there was no file to remove.

TASK [remove /tmp/foo.txt]
ok: [localhost]

PLAY RECAP
localhost   : ok=1  changed=0  unreachable=0  failed=0

 

If you want to remove all of the files in a directory without deleting the directory, the find module can be used to get the list of files in the directory  and then the file module with the with_items parameter can be used to remove the files in the directory.

---
- hosts: localhost
  tasks:
  - name: find the files in the /tmp directory
    find:
      paths: /tmp
    register: tmp_files

  - name: absent /tmp/foo/
    ansible.builtin.file:
      path: "{{ item.path }}"
      state: absent
    with_items: "{{ tmp_files.files }}"
...   

 


Loop over multiple files/directories

Create multipe files

---
- hosts: localhost
  tasks:
  - name: create multiple files
    ansible.builtin.file:
      path: "{{ item }}"
      state: touch
    with_items:
    - /tmp/foo.txt
    - /tmp/bar.txt
...   

 

Create multipe directories

---
- hosts: localhost
  tasks:
  - name: create multiple directories
    ansible.builtin.file:
      path: "{{ item }}"
      state: directory
    with_items:
    - /tmp/foo
    - /tmp/bar
...   

 

Better yet, create multiple files and directories

---
- hosts: localhost
  tasks:
  - name: create multiple files and directories
    ansible.builtin.file:
      path: "{{ item.path }}"
      state: "{{ item.state }}"
    with_items:
    - { path: "/tmp/foo",             state: "directory" }
    - { path: "/tmp/bar",             state: "directory" }
    - { path: "/tmp/foo/example.txt", state: "touch" }
    - { path: "/tmp/bar/example.txt", state: "touch" }
...   

 


Skip if file exists

To prevent the target file from being modified in any way when the file exists, you can first use the stat module to determine if the file exists. Then, use when to only perform the touch if the file does not exist.

- name: "determine if /tmp/foo.txt exists"
  stat:
    path: "/tmp/foo.txt"
  register: "foo"

- name: "create /tmp/foo.txt"
  ansible.builtin.file:
    path: "/tmp/foo.txt"
    state: "touch"
  when: "foo.stat.exists == false"

 

With stat and when, if the file was not created or modified, the play to determine if the file exists should return ok and the play to touch the file should return skipping.

TASK [determine if /tmp/foo.txt exists]
ok: [server1.example.com]

TASK [touch /tmp/foo.txt]
skipping: [server1.example.com]

PLAY RECAP
server1.example.com   : ok=2  changed=0  unreachable=0  failed=0

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 4875d0 in the box below so that we can be sure you are a human.