🧱 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:

  1. πŸ—οΈ Initialize β€” Resource created with attributes from the DSL block
  2. πŸ›‘οΈ Evaluate guards β€” only_if/not_if conditions checked (commands run on target)
  3. πŸ” Pre-action β€” Gather desired state, upload temp files for comparison
  4. πŸ“Š Query current state β€” Check existing state on the target via specinfra
  5. πŸ“ˆ Show differences β€” Display what would change (attribute diffs, file content diffs)
  6. βš™οΈ Execute action β€” Apply changes (⏭️ skipped in dry-run mode)
  7. βœ… Verify β€” Run verification commands (⏭️ skipped in dry-run)
  8. πŸ”” 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, template inherits mode, owner, group, and sensitive from file β€” you don’t need to check the file docs separately.