itamae-kitchen / itamae

Configuration management tool inspired by Chef, but simpler and lightweight. Formerly known as Lightchef.

Home Page:https://itamae.kitchen/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Definitions "undefined local variable or method `params'"

n0ts opened this issue · comments

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?

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

@ryotarai Thanks you for your help.