Skip to content

Instantly share code, notes, and snippets.

@directionless
Last active December 14, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save directionless/5134452 to your computer and use it in GitHub Desktop.
Save directionless/5134452 to your computer and use it in GitHub Desktop.
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