Skip to content

Instantly share code, notes, and snippets.

@directionless
Last active Dec 14, 2015
Embed
What would you like to do?
chef -- create an ssh key and store it in the attributes
# Create the ssh access key
#
# Something in the node.set lines happens at the *start* of the chef
# run. It might be the node.set, or it might be the File.read, but
# either way, it happens before the resource execution. To compensate
# for this, we need to shift the ssh key creation to the start of the
# run. http://docs.opscode.com/resource_common_compile.html
sshfile = "/home/runtime/id_rsa"
sshcomment = [
node[:fqdn],
"chef-generated",
Time.now.strftime("%Y%m%d"),
].join("-")
make_key = execute "ssh key for runtime" do
user "runtime"
creates sshfile
command "ssh-keygen -t rsa -q -f #{sshfile} -P '' -C #{sshcomment}"
end
make_key.run_action(:run)
node.set[:site][:sshdeploykeys][:runtime] = ::File.read(sshfile)
node.save
# You can retrieve those files for various templates and future use via:
search(:node, 'site_sshdeploykeys:*').each do |n|
n[:site][:sshdeploykeys].each do |user, sshkey|
puts user
puts sshkey
puts '----'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment