Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible with multiple SSH key pair

Tags:

ansible

I am new to Ansible. I am able to test it and its working fine with my test requirment. For making connection between management node and the client node I am using already created ssh key pair. How can I use another node with different SSH key pair? For reference I am considering 3 ec2-instance with different key pairs.

like image 853
sumit kumar Avatar asked Oct 08 '14 11:10

sumit kumar


2 Answers

Good news- in a basic use case, this is fairly easy. Simply use the ansible_ssh_private_key_file parameter in your Ansible inventory.

Here are some examples purloined from my personal file:

$ cat hosts.ini  [server1] 54.1.2.3 ansible_ssh_private_key_file=~/.ssh/server1.pem  [testservers] ec2-54-2-3-4.compute-1.amazonaws.com ansible_ssh_private_key_file=~/.ssh/aws-testserver.pem ansible_ssh_user=ubuntu ec2-54-2-3-5.compute-1.amazonaws.com ansible_ssh_private_key_file=~/.ssh/aws-testserver.pem ansible_ssh_user=ubuntu  [piwall] 10.0.0.88 ansible_ssh_private_key_file=~/.ssh/raspberrypi.pem ansible_ssh_user=pi 
like image 79
tedder42 Avatar answered Oct 06 '22 13:10

tedder42


tedder42 is correct, however, there is a better way of doing it.

See ansible_ssh_private_key_file here.

I have in my host files the following

# SSH Keys configuration  [all_servers:vars] ansible_ssh_private_key_file = <YOUR PRIVATE KEY LOCATION>   # Server configuration [all_servers:children] elastic_servers nginx_servers  [elastic_servers] 44.22.11.22 44.55.66.77 22.11.22.33  [nginx_servers] 22.24.123.123 233.111.222.11 

If you have multiple keys configuration, you can do something like the following

[nginx:vars] ansible_ssh_private_key_file = <YOUR PRIVATE KEY LOCATION> [app:vars] ansible_ssh_private_key_file = <YOUR 2nd PRIVATE KEY LOCATION>  [nginx:children] nginx_servers  [app:children] app_servers  [nginx_servers] 1.2.3.4 [app_servers] 5.5.5.5 6.6.6.6 

That's way cleaner than tedder42 answer. This is useful if you have multiple keys for multiple servers.

Otherwise, you can include your key in ansible.cfg file instead.

like image 33
Ahmed Avatar answered Oct 06 '22 13:10

Ahmed