LOGBOOK

HELP

Quiz Entry - updated: 2026.11.12

How do you create an empty file with a given owner, group and mode, and why does that task report changed on every run?

ansible.builtin.file with state: touch plus owner, group and mode; like the touch command it also updates the timestamps each time, so it is never "already done". Add access_time: preserve and modification_time: preserve to make it idempotent.

- name: Touch a file and set permissions
  ansible.builtin.file:
    path: /path/to/file
    owner: user1
    group: group1
    mode: "0640"
    state: touch

Result: an empty file -rw-r-----. user1 group1 0 ... file, owned by user1, readable by group1, invisible to everyone else.

state: touch copies the command's behaviour: create the file if it is missing, otherwise update its access and modification times. Since the times change every run, Ansible reports changed every run. If you only need the file to exist with the right attributes:

    state: touch
    access_time: preserve
    modification_time: preserve

Now an existing file is left alone and the task reports ok.

About mode: unquoted 0640 works, because YAML reads a leading 0 as octal. Writing 640 without the zero gives the decimal number 640, which is a different, wrong mode. Quoting ("0640") removes all doubt.

Go deeper:

From Quiz: ITIA / Ansible: Managing Files and Jinja2 Templates | Updated: Nov 12, 2026