Skip to content

Instantly share code, notes, and snippets.

@s2t2
Last active August 29, 2015 14:12
Show Gist options
  • Save s2t2/147ded173c6e6005f6df to your computer and use it in GitHub Desktop.
Save s2t2/147ded173c6e6005f6df to your computer and use it in GitHub Desktop.
Organizing a Chef cookbook, need to access node attributes across recipes, trying to avoid using `Chef::Recipe.send(:include, MyHelper)`

As-Is

# my_cookbook/recipes/default.rb
root_mysql_connection = {
  :host => "localhost",
  :username => "root",
  :password => node["mysql"]["server_root_password"]
}

To-Be?

# my_cookbook/libraries/my_helper.rb
module MyCookbook
  module MyHelper
    def self.root_mysql_connection(node)
      {
        :host => "localhost",
        :username => "root",
        :password => node["mysql"]["server_root_password"]
      }
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment