💻 local_ruby_block
💻 local_ruby_block
Execute arbitrary Ruby code on the local machine (where Itamae runs), not on the target. Useful for local logic, logging, or dynamic recipe behavior.
⚡ Actions
| Action | Description |
|---|---|
:run |
Execute the block (default) |
:nothing |
Do nothing (use with notifications) |
📋 Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
block |
Proc | — | Ruby code to execute locally |
💡 This resource has no
default_nameattribute — the resource name is just a label for logging purposes.
🔍 How It Works
- Execute — Calls the Ruby block on the local machine
- CWD support — If the common
cwdattribute is set, changes directory viaDir.chdir(cwd)before executing the block - Local context — The block runs in the Itamae process, with access to
run_commandfor executing shell commands on the target
🔬 Dry-Run Behavior
⚠️ Important: The Ruby block is not executed in dry-run mode. The block is inside the
action_runmethod, which is skipped entirely. There is no way to preview what alocal_ruby_blockwould do.
📖 Examples
Run local logic
local_ruby_block 'print info' do
block do
puts 'Recipe is running!'
end
end
Dynamic decisions 🔀
local_ruby_block 'check environment' do
block do
result = run_command('uname -r')
Itamae.logger.info "Kernel: #{result.stdout.strip}"
end
end
Conditional resource execution
local_ruby_block 'setup feature flag' do
block do
result = run_command('cat /etc/feature-flags.json')
$enable_monitoring = result.stdout.include?('"monitoring": true')
end
end
package 'prometheus-node-exporter' do
only_if { $enable_monitoring }
end
Logging and debugging 🐛
local_ruby_block 'debug info' do
block do
Itamae.logger.info "Node attributes: #{node.to_hash}"
Itamae.logger.info "Running on: #{run_command('hostname').stdout.strip}"
end
end