title: Ansible with Github: Permission denied (Publickey)
body: <p>I'm trying to understand the GitHub ssh configuration with Ansible (I'm working on the Ansible: Up &amp; Running book). I'm running into two issues.</p>
  
  <p><strong>Permission denied (publickey)</strong> -
  When I first ran the <code>ansible-playbook mezzanine.yml</code> playbook, I got a permission denied:</p>
  
  <pre><code>failed: [web] =&gt; {"cmd": "/usr/bin/git ls-remote '' -h refs/heads/HEAD", "failed": true, "rc": 128}
  stderr: Permission denied (publickey).
  fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights
  and the repository exists.
  
  msg: Permission denied (publickey).
  fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights
  and the repository exists.
  
  FATAL: all hosts have already failed -- aborting
  </code></pre>
  
  <p>Ok, fair enough, I see several people have had this problem. So I jumped to appendix A on running Git with SSH and it said to run the ssh-agent and add the id_rsa public key:</p>
  
  <pre><code>eval `ssh-agent -s`
  ssh-add ~/.ssh/id_rsa
  </code></pre>
  
  <p>Output: <code>Identity Added</code>I ran <code>ssh-agent -l</code> to check and got the long string: <code>2048 e3:fb:...</code> But I got the same output. So I checked the Github docs on ssh key generations and troubleshooting which recommended updating the ssh config file on my host machine:</p>
  
  <pre><code>Host github.com
      User git
      Port 22
      Hostname github.com
      IdentityFile ~/.ssh/id_rsa
      TCPKeepAlive yes
      IdentitiesOnly yes
  </code></pre>
  
  <p>But this still provides the same error. So at this point, I start thinking it's my rsa file, which leads me to my second problem.</p>
  
  <p><strong>Key Generation Issues</strong> - I tried to generate an additional cert to use, because the Github test threw another "Permission denied (publickey)" error.</p>
  
  <pre><code>Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
  Permission denied (publickey).
  </code></pre>
  
  <p>I followed the Github instructions from scratch and generated a new key with a different name.</p>
  
  <pre><code>ssh-keygen -t rsa -b 4096 -C "me@example.com"
  </code></pre>
  
  <p>I didn't enter a passphrase and saved it to the .ssh folder with the name git_rsa.pub. I ran the same test and got the following:</p>
  
  <pre><code>$ ssh -i ~/.ssh/git_rsa.pub -T git@github.com
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  Permissions 0644 for '/Users/antonioalaniz1/.ssh/git_rsa.pub' are too open.
  It is required that your private key files are NOT accessible by others.
  This private key will be ignored.
  bad permissions: ignore key: ~/.ssh/github_rsa.pub
  Permission denied (publickey).
  </code></pre>
  
  <p>I checked on the permissions and did a <code>chmod 700</code> on the file and I still get <code>Permission denied (publickey)</code>. I even attempted to enter the key into my Github account, but first got a message that the key file needs to start with <code>ssh-rsa</code>. So I started researching and hacking. Started with just entering the long string in the file (it started with --BEGIN PRIVATE KEY--, but I omitted that part after it failed); however, Github's not accepting it, saying it's invalid. </p>
  
  <p>This is my Ansible command in the YAML file:</p>
  
  <pre><code>- name: check out the repository on the host
    git: repo={{ repo_url }} dest={{ proj_path }} accept_hostkey=yes
  
    vars:
      repo_url: git@github.com:lorin/mezzanine-example.git
  </code></pre>
  
  <p>This is my ansible.cfg file with ForwardAgent configured:</p>
  
  <pre><code>[defaults]
  hostfile = hosts
  remote_user = vagrant
  private_key_file = .vagrant/machines/default/virtualbox/private_key
  host_key_checking = False
  
  [ssh_connection]
  ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o ForwardAgent=yes
  </code></pre>
  
  <p>The box is an Ubuntu Trusty64 using Mac OS. If anyone could clue me into the file permissions and/or Github key generation, I would appreciate it. </p>