Question:
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.
1 2 3 4 |
$ 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:
1 2 3 4 5 6 |
[all_servers:vars] ansible_ssh_private_key_file = ./mykeypair.pem [dashboard] 1.2.3.4 dashboard_domain=my.domain.info |
Here is my playbook:
1 2 3 4 5 6 7 8 9 |
--- - hosts: dashboard gather_facts: False remote_user: ubuntu tasks: - name: ping ping: |
This is the command I am using to run it:
1 2 |
ansible-playbook -i ./hosts test.yml |
It results in the following error:
1 2 3 |
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:
1 2 3 |
$ ssh -i mykeypair.pem ubuntu@1.2.3.4 'whoami' ubuntu |
What am I doing wrong?
Answer:
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:
1 2 3 4 5 6 |
[dashboard:vars] ansible_ssh_private_key_file=./mykeypair.pem [dashboard] 1.2.3.4 dashboard_domain=my.domain.info |