Skip to content

Instantly share code, notes, and snippets.

@mivok
Created May 30, 2018 20:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mivok/a5a0f8618aa7a7dbcf1d79fcdbffafcd to your computer and use it in GitHub Desktop.
Save mivok/a5a0f8618aa7a7dbcf1d79fcdbffafcd to your computer and use it in GitHub Desktop.
# Save this as ~/.chef/newcreds.rb
# Add require_relative 'newcreds' to your ~/.chef/knife.rb
require 'tomlrb'
require 'pathname'
context_file = "#{Dir.home}/.chef/context"
credentials_file = "#{Dir.home}/.chef/credentials"
context = File.read(context_file).chomp if File.exist?(context_file)
profile_name = ENV["CHEF_PROFILE"] || context || "default"
conf = Tomlrb.load_file(credentials_file) if File.exist?(credentials_file)
conf ||= {}
profile = conf[profile_name] || {}
# Make relative pathnames relative to ~/.chef
unless Pathname.new(profile['client_key']).absolute?
profile['client_key'] = File.expand_path("~/.chef/" + profile['client_key'])
end
%w(client_name client_key validator_key chef_server_url node_name).each do |k|
ChefConfig::Config.send(k, profile[k]) if profile[k]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment