[chef] Re: template variables arguments and methods


Chronological Thread 
  • From: Brandon Richins < >
  • To: " " < >
  • Subject: [chef] Re: template variables arguments and methods
  • Date: Mon, 9 Mar 2015 22:45:30 +0000
  • Accept-language: en-US

I have to correct my question.  #3 doesn’t work either.  All my @bar sections in my erb are blank when rendered.  

Why won’t methods defined on the recipe evaluate before passing control to the ERB?

Brandon Richins


I have a question about Chef templates, variables arguments, and methods.   When using templates in a recipe, I sometimes want to use a method instead of a local variable as the value to variable key.  ex. def baz { 'baz' } vs baz = 'baz'.  When I try to use baz as a method in the template variables section, using the syntax of #1 and #2, Chef fails to converge the template and raises an undefined method error for baz.  For #1 and #2 if baz is a local variable in the template it works fine.  Today I discovered syntax #4 which worked when baz is a method.

I’m going to show my own ignorance about Ruby now.  I understand { bar: 'b' } to be a Hash in Ruby.  So I think #1 is passing a Hash to a “variables” method on a template object.  For #2 I was guessing it was coercing the list of arguments into a Hash (kind of like varargs in Java).  For #3 I was guessing it’s evaluating the outer {} as a block and the inner {} as a Hash.  Can you confirm these assumptions?

So why do #1 and #2 raise an undefined method error and #3 works.  It seems the Chef/Template DSL is treating methods differently than local variables when it comes to templates.

def baz
  'baz'
end

1) Parentheses then braces
-----------------------------------
template 'foo' do
  variables({
    bar: baz,
    a: 'b',
  })
end

2) Parentheses and no braces
-----------------------------------
template 'foo' do
  variables(
    bar: baz,
    a: 'b',
  )
end

4) Double braces
-----------------------------------
template 'foo' do
  variables{{
    bar: baz,
    a: 'b',
  }}
end

Thanks,

Brandon Richins




Archive powered by MHonArc 2.6.16.

§