Templates don't fail when values are missing


#1

I have a large (hundreds of properties, dozens of templates) set of templates I’m porting from Nant to Chef. The properties in the old system are scattered across multiple files and while aggregating the properties into data bags and adjusting the templates, I have missed some values.

The call in the recipe to merge each template with the properties from the data bags works, but values that are not present in the hash are simply empty in the generated template. The substitution fails silently.

For example, if I have this in the template:

… and the ‘foobar’ key/value pair is not present, I wind up with this in the generated file:

Is there any way to get Chef to throw a warning or error in this case?

Thanks.

-Pete


#2

In ruby, Hash[nil] => nil; so, explicitly bind your hash keys to
’variables’ in the template and then check for presence of the ivar.

An alternate workaround: <%= if @property.key?(‘foobar’) %>

Cheers,

AJ

On 16 April 2013 06:48, Peter Loron peterl@standingwave.org wrote:

I have a large (hundreds of properties, dozens of templates) set of templates I’m porting from Nant to Chef. The properties in the old system are scattered across multiple files and while aggregating the properties into data bags and adjusting the templates, I have missed some values.

The call in the recipe to merge each template with the properties from the data bags works, but values that are not present in the hash are simply empty in the generated template. The substitution fails silently.

For example, if I have this in the template:

… and the ‘foobar’ key/value pair is not present, I wind up with this in the generated file:

Is there any way to get Chef to throw a warning or error in this case?

Thanks.

-Pete


#3

Peter,

You might want to use Hash#fetch in your template, which raises a
KeyError if a key can’t be found (thereby aborting the chef run).

See http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-fetch

-Mathias

On Mon, Apr 15, 2013 at 9:45 PM, AJ Christensen aj@junglist.gen.nz wrote:

In ruby, Hash[nil] => nil; so, explicitly bind your hash keys to
’variables’ in the template and then check for presence of the ivar.

An alternate workaround: <%= if @property.key?(‘foobar’) %>

Cheers,

AJ

On 16 April 2013 06:48, Peter Loron peterl@standingwave.org wrote:

I have a large (hundreds of properties, dozens of templates) set of templates I’m porting from Nant to Chef. The properties in the old system are scattered across multiple files and while aggregating the properties into data bags and adjusting the templates, I have missed some values.

The call in the recipe to merge each template with the properties from the data bags works, but values that are not present in the hash are simply empty in the generated template. The substitution fails silently.

For example, if I have this in the template:

… and the ‘foobar’ key/value pair is not present, I wind up with this in the generated file:

Is there any way to get Chef to throw a warning or error in this case?

Thanks.

-Pete


#4

Thanks! I will check that out!

-pete

On Apr 16, 2013, at 2:12 PM, Mathias Lafeldt mathias.lafeldt@gmail.com wrote:

Peter,

You might want to use Hash#fetch in your template, which raises a
KeyError if a key can’t be found (thereby aborting the chef run).

See http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-fetch

-Mathias

On Mon, Apr 15, 2013 at 9:45 PM, AJ Christensen aj@junglist.gen.nz wrote:

In ruby, Hash[nil] => nil; so, explicitly bind your hash keys to
’variables’ in the template and then check for presence of the ivar.

An alternate workaround: <%= if @property.key?(‘foobar’) %>

Cheers,

AJ

On 16 April 2013 06:48, Peter Loron peterl@standingwave.org wrote:

I have a large (hundreds of properties, dozens of templates) set of templates I’m porting from Nant to Chef. The properties in the old system are scattered across multiple files and while aggregating the properties into data bags and adjusting the templates, I have missed some values.

The call in the recipe to merge each template with the properties from the data bags works, but values that are not present in the hash are simply empty in the generated template. The substitution fails silently.

For example, if I have this in the template:

… and the ‘foobar’ key/value pair is not present, I wind up with this in the generated file:

Is there any way to get Chef to throw a warning or error in this case?

Thanks.

-Pete