title: How to use a public keypair .pem file for ansible playbooks?
body: <p>I want to use a public aws keypair .pem file for running ansible playbooks.  I want to do this without changing my <code>~/.ssh/id_rsa.pub</code> and I can't create a new keypair from my current <code>~/.ssh/id_rsa.pub</code> and apply it to the ec2 instances I am trying to change.</p>
  
  <pre><code>$ ansible --version
  ansible 1.9.6
    configured module search path = None
  </code></pre>
  
  <p>Here is my hosts file (note that my actual ip is replaced with <code>1.2.3.4</code>).  This is probably the issue since I need a way to set a public key variable and use that:</p>
  
  <pre><code>[all_servers:vars]
  ansible_ssh_private_key_file = ./mykeypair.pem
  
  [dashboard]
  1.2.3.4 dashboard_domain=my.domain.info 
  </code></pre>
  
  <p>Here is my playbook:</p>
  
  <pre><code>---
  - hosts: dashboard
    gather_facts: False
    remote_user: ubuntu
  
    tasks:
      - name: ping
        ping:
  </code></pre>
  
  <p>This is the command I am using to run it:</p>
  
  <pre><code>ansible-playbook -i ./hosts test.yml
  </code></pre>
  
  <p>It results in the following error:</p>
  
  <pre><code>fatal: [1.2.3.4] =&gt; SSH Error: Permission denied (publickey).
      while connecting to 1.2.3.4:22
  </code></pre>
  
  <p>There is no problem with my keypair:</p>
  
  <pre><code>$ ssh -i mykeypair.pem ubuntu@1.2.3.4 'whoami'
  ubuntu
  </code></pre>
  
  <p>What am I doing wrong?</p>