Skip to content

Instantly share code, notes, and snippets.

@DmZ
Last active December 15, 2015 06:59
Show Gist options
  • Save DmZ/5220159 to your computer and use it in GitHub Desktop.
Save DmZ/5220159 to your computer and use it in GitHub Desktop.
Allow override normal attributes passed by command line to chef-solo. Put to libraries/ folder of your cookbook.
#
# Load attributes from local file
# overriding/merging with command line attributes
#
require 'chef/run_context'
class Chef::RunContext
alias_method :original_load_attributes, :load_attributes
def load_attributes
filename = '/etc/chef/attrs.json'
begin
json_file = open(filename)
json = Chef::JSONCompat.from_json(json_file.read)
json_file.close unless json_file.closed?
Chef::Log.debug("Node #{@node.name} loading attributes from json #{filename}")
@node.consume_attributes(json)
rescue Exception => error
Chef::Log.debug("Skipping #{filename} due to error: " + error.message)
end
original_load_attributes
end
end
@DmZ
Copy link
Author

DmZ commented Mar 22, 2013

Chef 10.x only for now.
(For Chef 11.x need to override Chef::RunContext::CookbookCompiler.compile_attributes instead)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment