Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a public keypair .pem file for ansible playbooks?

Tags:

I want to use a public aws keypair .pem file for running ansible playbooks. I want to do this without changing my ~/.ssh/id_rsa.pub and I can't create a new keypair from my current ~/.ssh/id_rsa.pub and apply it to the ec2 instances I am trying to change.

$ ansible --version ansible 1.9.6   configured module search path = None 

Here is my hosts file (note that my actual ip is replaced with 1.2.3.4). This is probably the issue since I need a way to set a public key variable and use that:

[all_servers:vars] ansible_ssh_private_key_file = ./mykeypair.pem  [dashboard] 1.2.3.4 dashboard_domain=my.domain.info  

Here is my playbook:

--- - hosts: dashboard   gather_facts: False   remote_user: ubuntu    tasks:     - name: ping       ping: 

This is the command I am using to run it:

ansible-playbook -i ./hosts test.yml 

It results in the following error:

fatal: [1.2.3.4] => SSH Error: Permission denied (publickey).     while connecting to 1.2.3.4:22 

There is no problem with my keypair:

$ ssh -i mykeypair.pem [email protected] 'whoami' ubuntu 

What am I doing wrong?

like image 301
Alex Cohen Avatar asked Feb 08 '17 20:02

Alex Cohen


People also ask

How do I use ssh keys in Ansible?

Setting up SSH keys By default, Ansible assumes you are using SSH keys to connect to remote machines. SSH keys are encouraged, but you can use password authentication if needed with the --ask-pass option. If you need to provide a password for privilege escalation (sudo, pbrun, and so on), use --ask-become-pass .


2 Answers

Ok little mistakes I guess you can't have spaces in host file variables and need to define the group you are applying the vars to. This hosts file works with it all:

[dashboard:vars] ansible_ssh_private_key_file=./mykeypair.pem  [dashboard] 1.2.3.4 dashboard_domain=my.domain.info  
like image 76
Alex Cohen Avatar answered Sep 25 '22 16:09

Alex Cohen


I have come across this and all what I had to do was to run the below

#ssh-agent bash #ssh-add ~/.ssh/keypair.pem 
like image 36
Victor Biga Avatar answered Sep 22 '22 16:09

Victor Biga