ReferenceResources
directory
Manage directories with ownership and permissions.
The directory resource ensures directories exist (or are absent) with the correct ownership and permissions. It is declarative and idempotent.
Input
| Field | Type | Default | Required | Description |
|---|---|---|---|---|
path | string | — | Yes | Absolute path to the directory |
mode | string | — | No | Directory mode (e.g. "0755") |
owner | string | — | No | Owner user |
group | string | — | No | Owner group |
state | "present" | "absent" | "present" | No | Whether the directory should exist |
recursive | boolean | true | No | Use mkdir -p for recursive creation |
Output
| Field | Type | Description |
|---|---|---|
path | string | The directory path |
changed | boolean | Whether the directory was modified |
Behavior
Check phase
- Tests directory existence with
test -d. - For
state: "absent"— in desired state if the directory doesn't exist. - For
state: "present"— checks mode, owner, and group viastatif specified.
Apply phase
state: "present": creates the directory withmkdir -p(ormkdirifrecursive: false). Sets mode, owner, and group if specified.state: "absent": removes the directory withrm -rf.
Annotations
| Property | Value |
|---|---|
| Nature | Declarative |
| Idempotent | Yes |
| Destructive | Yes (when state: "absent") |
| Read-only | No |
| Required capabilities | exec |
Examples
Create a directory with ownership
await directory({
path: "/var/www/app",
owner: "www-data",
group: "www-data",
mode: "0755",
})Create nested directories
await directory({ path: "/opt/app/data/uploads" })With recursive: true (the default), all parent directories are created as needed.
Non-recursive creation
await directory({ path: "/opt/app/logs", recursive: false })Fails if /opt/app doesn't exist.
Remove a directory
await directory({ path: "/tmp/old-deploy", state: "absent" })Warning: state: "absent" runs rm -rf — this is destructive and recursive.
Gotchas
recursivedefaults totrue, meaningmkdir -pis used by default.- Set
recursive: falseto fail if parent directories don't exist. state: "absent"runsrm -rfon the path. Use with care.modeshould be an octal string like"0755", not a number.ownerandgroupare set independently — you can set one without the other.