title: Add an ssh key using Chef
body: <p>I would like to add a private SSH key to a machine using Chef.</p>
  
  <p><em>I'm new to Chef, and not a ruby programmer so the code below might be less-than optimum</em></p>
  
  <p>My recipe includes the following:</p>
  
  <pre class="lang-rb prettyprint-override"><code>execute "add private ssh key" do
    command 'ssh-add ' + ::File.join('/home', node['user'], '.ssh/keys/id_rsa')
    user node['user']
  end
  </code></pre>
  
  <p>and upon running <code>sudo chef-client</code> on the target machine, then we get "Could not open a connection to your authentication agent" when attempting to add the SSH key.</p>
  
  <p>This indicates the ssh client is not running, so I change the recipe to start with <code>eval $(ssh-agent) &amp;&amp;</code> in order to start the client. After this upon running <code>sudo chef-client</code>  in the shell we see the command in green, indicating the command was executed successfully.</p>
  
  <p>However, the <code>id_rsa</code> key has not been added for the logged in user, and I see the SSH client is not running.</p>
  
  <p>I probably am barking up the wrong tee, but this suggests to me that the key was added for a different user - maybe side affect of Chef being ran with sudo (Chef does not work when sudo is not used).</p>
  
  <p>Has anyone experience with working around this problem, or adding SSH keys to machines using Chef?</p>