Skip to content

Instantly share code, notes, and snippets.

@nohupped
Forked from ctgswallow/gist:3409231
Created February 27, 2019 13:39
Show Gist options
  • Save nohupped/caa45c252e5cceab37e32502e2a3d3ff to your computer and use it in GitHub Desktop.
Save nohupped/caa45c252e5cceab37e32502e2a3d3ff to your computer and use it in GitHub Desktop.
Create a template within a ruby block
ruby_block "create ssh key" do
block do
k = SSHKey.generate(:type => 'RSA', :bits => 1024, :comment => "Postgres Master")
node.set[:postgresql][:pubkey] = k.ssh_public_key
node.save
# Much of the DSL disappears in ruby blocks. Here's how to create a template.
rc = Chef::RunContext.new(node, node.cookbook_collection)
t = Chef::Resource::Template.new "/var/lib/postgresql/.ssh/id_rsa"
t.source("id_rsa.erb")
t.owner("postgres")
t.group("postgres")
t.cookbook("postgresql")
t.mode("0600")
t.variables(
:k => k.private_key
)
t.action(:create_if_missing)
t.run_context=(rc)
t.run_action("create")
end
not_if { File.exists?("/var/lib/postgresql/.ssh/id_rsa") }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment