The ansible.builtin.file module with state: touch creates an empty file if it doesn't exist, or updates its modification timestamp if it does — exactly like the Unix touch command. Use it to create marker files, initialize logs, or signal task completion.
Quick Reference
# Create empty file (or update timestamp)
- ansible.builtin.file:
path: /tmp/marker.txt
state: touch
# Create with specific permissions
- ansible.builtin.file:
path: /var/log/myapp/app.log
state: touch
owner: app
group: app
mode: '0644'
# Set specific modification time
- ansible.builtin.file:
path: /tmp/timestamp-file
state: touch
modification_time: "202606020000.00"
Parameters
Parameter
Description
path
File path to touch (required)
state
touch (required for this operation)
owner
File owner
group
File group
mode
File permissions
modification_time
Set mtime (format: YYYYMMDDHHmm.ss or preserve)
access_time
Set atime (same format or preserve)
Common Patterns
Create Log File Before Service Start
- name: Ensure log file exists
ansible.builtin.file:
path: /var/log/myapp/app.log
state: touch
owner: app
group: app
mode: '0644'
changed_when: false # Don't report as change every run
- name: Start application
ansible.builtin.service:
name: myapp
state: started
Add changed_when: false if timestamp update is expected
Parent directory doesn't exist
Create dir first: file: state=directory
Permission denied
Use become: true
Want content in file
Use copy with content instead
Best Practices
Use changed_when: false if touch is just ensuring existence
Create parent directories first — touch doesn't create parents
Use copy force=false if you only want to create, never update
Set owner/group/mode explicitly — don't rely on umask
Use as deployment markers — mtime becomes "last deployed" timestamp
Conclusion
Use file: state=touch to create empty files or update timestamps. Add changed_when: false when the touch is just a precondition. Use copy: force=false when you only want to create without updating existing files. Set permissions explicitly to avoid umask-dependent behavior.