First, generate a set of SSH keys:
ssh-keygen
Setup the ~/.ssh/config
so, by default, we login with a specific user (in this case, root
):
Host hadoop01
User root
Host hadoop02
User root
Host hadoop03
User root
Host deepthought
User root
Install ansible:
pip install ansible
Then create an ansible hosts file:
sudo mkdir /etc/ansible
sudo vi /etc/ansible/hosts
Add your hostnames to /etc/ansible/hosts
:
[basement_cluster]
hadoop[01:03]
[basement]
hadoop[01:03]
deepthought
Create an Ansible playbook:
---
# keyless-entry.yml
- hosts: all
user: root
tasks:
- name: Copy public key to all nodes
copy: src=/Users/alex/.ssh/id_rsa.pub dest=/root/id_rsa.pub owner=root group=root mode=755 backup=yes
- name: Append public key to authorized_keys
shell: "cat /root/id_rsa.pub >> /root/.ssh/authorized_keys"
Then, execute the playbook across the cluster:
ansible-playbook keyless-entry.yml -u root -k
SSH password:
And, if all's well, you should see something like this:
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [hadoop01]
ok: [hadoop02]
ok: [hadoop03]
ok: [deepthought]
TASK: [Copy public key to all nodes] ******************************************
changed: [hadoop01]
changed: [hadoop02]
changed: [hadoop03]
changed: [deepthought]
TASK: [Append public key to authorized_keys] **********************************
changed: [hadoop01]
changed: [hadoop02]
changed: [hadoop03]
changed: [deepthought]
PLAY RECAP ********************************************************************
hadoop01 : ok=3 changed=2 unreachable=0 failed=0
hadoop02 : ok=3 changed=2 unreachable=0 failed=0
hadoop03 : ok=3 changed=2 unreachable=0 failed=0
deepthought : ok=3 changed=2 unreachable=0 failed=0