π§± Resources
π§± Resources
Resources are the building blocks of Itamae recipes. Each resource describes a piece of infrastructure and its desired state. Itamae ships with 15 built-in resource types.
π§ Common Attributes
All resources share these attributes:
| Attribute | Type | Description |
|---|---|---|
action |
Symbol or Array | Action(s) to perform. Each resource has its own set of valid actions. |
user |
String | Execute resource commands as this user. |
cwd |
String | Working directory for commands. |
only_if |
String | Guard β execute resource only if this shell command succeeds (exit 0). |
not_if |
String | Guard β skip resource if this shell command succeeds (exit 0). |
π‘ Tip: Guards run real shell commands on the target β even in dry-run mode.
π Notifications
Resources can notify other resources when they change:
template '/etc/nginx/nginx.conf' do
source 'nginx.conf.erb'
notifies :restart, 'service[nginx]'
end
notifies
Trigger an action on another resource:
notifies :action, 'resource_type[name]' # delayed (default)
notifies :action, 'resource_type[name]', :delayed
notifies :action, 'resource_type[name]', :immediately
subscribes
Listen for changes on another resource:
service 'nginx' do
subscribes :restart, 'template[/etc/nginx/nginx.conf]'
action :nothing
end
β±οΈ Timing
:delayed(default) β Run the notification after the entire recipe completes. Duplicate delayed notifications are coalesced.:immediatelyβ Run the notification right after the notifying resource executes.
π Built-in Resources
| Resource | Description |
|---|---|
| π directory | Manage directories |
| β‘ execute | Run shell commands |
| π file | Manage file content and attributes |
| π gem_package | Install Ruby gems |
| π git | Clone and sync git repositories |
| π₯ group | Manage system groups |
| π http_request | Make HTTP requests and save responses |
| π link | Create symbolic links |
| π» local_ruby_block | Execute local Ruby code |
| π¦ package | Install system packages |
| π remote_directory | Upload directories to targets |
| π€ remote_file | Upload files to targets |
| π service | Manage system services |
| π template | Render ERB templates |
| π€ user | Manage system users |
π Resource Lifecycle
Each resource goes through these steps when executed:
- ποΈ Initialize β Resource created with attributes from the DSL block
- π‘οΈ Evaluate guards β
only_if/not_ifconditions checked (commands run on target) - π Pre-action β Gather desired state, upload temp files for comparison
- π Query current state β Check existing state on the target via specinfra
- π Show differences β Display what would change (attribute diffs, file content diffs)
- βοΈ Execute action β Apply changes (βοΈ skipped in dry-run mode)
- β Verify β Run verification commands (βοΈ skipped in dry-run)
- π Notify β Trigger any notifications/subscriptions
𧬠Inheritance Hierarchy
Resources form an inheritance tree. Child resources inherit all parent attributes:
Base (action, user, cwd)
βββ π¦ Package
βββ π Service
βββ π File (path, content, mode, owner, group, sensitive, block)
β βββ π€ RemoteFile (+source)
β β βββ π Template (+variables)
β βββ π HttpRequest (+url, headers, message, redirect_limit)
βββ π Directory
βββ β‘ Execute
βββ π Link
βββ π€ User
βββ π₯ Group
βββ π Git
βββ π GemPackage
βββ π RemoteDirectory
βββ π» LocalRubyBlock
π‘ For example,
templateinheritsmode,owner,group, andsensitivefromfileβ you donβt need to check thefiledocs separately.