Question

What I'm trying to accomplish is to generate an ssh-key and set the public key to a node attribute so I can later search on that node attribute and add it to the other servers/users authorized_keys.

My first attempt is this:

key = '/home/vagrant/.ssh/id_rsa'

bash "generate_key" do
  user "vagrant"
  code <<-EOH
    ssh-keygen -f #{key} -t rsa -N ''
  EOH
  not_if { ::File.exists?(key) }
end

ruby_block "reload_config" do
  block do
    node.default['test'] = IO.read("#{key}.pub")
    node.save
  end
  action :create
end

node.save #because i donno?

log "#{node['test']}"

This fails because (i think) the ruby block is evaluated before the bash block, so there is no file to read and or the node['test'] is evaluated at compile time so it is blank (though it doesn't work on the second converge either, when the file does exist).

I feel lost in something that seems trivial on the surface.

Was it helpful?

Solution

I've implemented sth. very similar here

ruby_block "save keys to attributes" do
  only_if { public_key.to_s == "" }
  block do
    private_key = File.read(ssh_key)
    public_key = File.read(ssh_key + ".pub")
    node.set['gerrit']['peer_keys']['private'] = private_key
    node.set['gerrit']['peer_keys']['public'] = public_key
  end
end

I think the precedence level "default" that you use is causing the problems, but I'm unsure, what the exact problem is in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top