file.write¶
Manage files, directories, links, and permissions
Properties¶
| Property | Type | Required | Description |
|---|---|---|---|
backup |
boolean | No | - |
content |
string | No | - |
force |
boolean | No | - |
group |
string | No | File group (groupname or GID) |
mode |
string | No | File permissions (e.g., '0644', '0755') |
owner |
string | No | File owner (username or UID) |
path |
string | Yes | File, directory, or symlink path (required) |
recurse |
boolean | No | - |
src |
string | No | - |
state |
string | No | Desired file state (file/present: file exists, absent: removed, directory: dir exists, link: symlink, hardlink: hard link, touch: update timestamp, perms: change permissions only) (allowed: file, present, absent, directory, link, hardlink, touch, perms) |
Examples¶
# Write a file with content
- name: Drop a config snippet
file.write:
path: "{{ home }}/.config/myapp/config.yml"
content: |
mode: production
port: 8080
mode: "0644"
# Ensure a directory exists
- name: Create the scratch dir
file.write:
path: "{{ home }}/.mooncake/scratch"
state: directory
mode: "0755"
# Remove a path
- name: Delete the stale lockfile
file.write:
path: /var/run/myapp.lock
state: absent
Platform Support¶
linux, darwin, windows, freebsd
Events Emitted¶
file.createdfile.updatedfile.removeddirectory.createddirectory.removedlink.createdpermissions.changed