Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Create a template within a ruby block

View gist:3409231
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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

Does not seem to work with chef v11.10.0 due to Chef::RunContext requiring an events argument.

@chr0n1x, here's one that works in latest Chef:

ruby_block 'run_my_template_resource' do
  action :create
  block do
    r = Chef::Resource::Template.new('template_name', run_context)
    r.path       '/path/to/write.to'
    r.source     'source.erb'
    r.cookbook   'cookbook-name'
    r.owner      'root'
    r.group      'root'
    r.mode       00600
    r.variables  my: 'variables'
    r.run_action :create
  end
end

@JeanMertz I found this while i was looking for a solution to one of my own problems with RemoteFile. By your example it feels like i should be able to do this:

ruby_block "parse-json" do
    block do
      f =  Chef::Provider::File::RemoteFile.new("/tmp/googlebackup", run_context)
      f.source "http://google.com"
      f.run_action :create
    end
    action :create
  end

but all i get is this error:

[2014-03-28T14:26:08+00:00] ERROR: ruby_block[parse-json] (/tmp/kitchen/cookbooks/jenkins_build_artefact/providers/default.rb line 16) had an error: NoMethodError: No resource or method named `source' for `Chef::Provider::RemoteFile ""'    

Any suggestions?

@mansona in my example, I instantiate the Resource instead of the Provider:

Chef::Resource::RemoteFile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.