Definitions "undefined local variable or method `params'"
n0ts opened this issue · comments
Naoya Nakazawa commented
Hi, I use Itamae 1.6.3 definitions.
My recipe is below.
recipe.rb
define :test_definition do
template "/path/to/dest/#{params[:name]}" do
source "#{params[:name]}.erb"
action :create
end
end
test_definition 'test'
itamae local --dry-run --log-level=debug recipe.rb got a below error "undefined local variable or method `params' ".
INFO : Starting Itamae...
DEBUG : Executing `mkdir -p /tmp/itamae_tmp`...
DEBUG : exited with 0
DEBUG : Executing `chmod 777 /tmp/itamae_tmp`...
DEBUG : exited with 0
/opt/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/itamae-1.6.3/lib/itamae/resource/base.rb:40:in `method_missing': undefined local variable or method `params' for #<Itamae::Resource::Base::EvalContext:0x007f9ad3ba4968> (NameError)
from itamae/recipe.rb:3:in `block (2 levels) in load'
from /opt/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/itamae-1.6.3/lib/itamae/resource/base.rb:111:in `instance_eval'
And below recipe is worked.
define :test_definition do
name = params[:name]
template "/path/to/dest/#{params[:name]}" do
source "#{name}.erb"
action :create
end
end
test_definition 'test'
Is it possible use params variable in resource block?
Ryota Arai commented
Hi, @n0ts
This is because a block passed to define
and one passed to resource like template
is not executed in the same context. It is expected behavior but should be fixed. For now, you may be able to access params
in resource block like this:
define :test_definition do
define_params = params
template "/path/to/dest/#{define_params[:name]}" do
source "#{define_params[:name]}.erb"
action :create
end
end
Naoya Nakazawa commented
@ryotarai Thanks you for your help.