Skip to content

Instantly share code, notes, and snippets.

@ctgswallow
Created August 20, 2012 23:27
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ctgswallow/3409231 to your computer and use it in GitHub Desktop.
Save ctgswallow/3409231 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
@coderanger
Copy link

This should not be used, it is not safe and breaks the Chef API.

@krzkrzkrz
Copy link

krzkrzkrz commented Jul 9, 2018

@coderanger what should not be used exactly? Chef::Resource::RemoteFile? Or Chef::Resource::Template.new inside block? Pls be specific.

At the same time. If you are hinting not to use this approach. Please suggest an alternative

Simply stating This should not be used, it is not safe and breaks the Chef API. is good information. But it doesn't really help the OP and future observers

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