- From: Mathias Lafeldt <
>
- To:
- Subject: [chef] Re: Re: Templates don't fail when values are missing
- Date: Tue, 16 Apr 2013 23:12:41 +0200
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
<
>
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
>
<
>
>
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:
>
>
>
> <foobar value="<%= @property['foobar'] %>" />
>
>
>
> … and the 'foobar' key/value pair is not present, I wind up with this in
>
> the generated file:
>
>
>
> <foobar value="" />
>
>
>
>
>
>
>
> Is there any way to get Chef to throw a warning or error in this case?
>
>
>
> Thanks.
>
>
>
> -Pete
Archive powered by MHonArc 2.6.16.