[chef] Re: Re: Re: Defining dynamic global custom variables once for all recipes during chef run


Chronological Thread 
  • From: Daniel DeLeo < >
  • To:
  • Subject: [chef] Re: Re: Re: Defining dynamic global custom variables once for all recipes during chef run
  • Date: Thu, 30 Apr 2015 07:29:31 -0700



On Thursday, April 30, 2015 at 7:08 AM, Mathew Crane wrote:

> Thanks. I made a library instance method to access these attributes 
> dynamically. This solves the code duplication issue.  
>  
> However, I'm still unaware of a good way to 'set' these variables 
> automatically via a library so that they become available to all recipes 
> within the Chef run. Ideally the variables become available to all 
> cookbooks that have 'depend "helper-cookbook"' in metadata.rb, and 
> redundant calls to the Chef API are eliminated. Is this even possible?
>  
> How it exists currently:
>  
> # cookbooks/helper-cookbook/libraries/helper-module.rb
> module helper
> def my_helper_module
> ...code...
> return env, server
> end
> end
>  
> reference: 
> http://stackoverflow.com/questions/22078318/access-node-attributes-in-chef-library
>  
> # cookbooks/dependent-cookbook/metadata.rb:
> depends "helper-cookbook"
>  
> # cookbooks/dependent-cookbook/recipes/default.rb:
> env, server = my_helper_module
>  
> Any tips or suggestions?
The simplest way to go is to define your helper methods as class methods 
(actually module methods since they’d be defined on a module, but the 
difference is pedantic).

This blog post is a bit old and rails-centric but describes how they work 
well enough: 
http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/

With a bit more work, it’s possible to mix your module in to Chef’s DSL, but 
this can be a bit confusing because the recipe DSL and resource DSLs are 
separate things, so you’ll probably stub your toe on that.

--  
Daniel DeLeo







Archive powered by MHonArc 2.6.16.

§