Skip to content

Instantly share code, notes, and snippets.

@alexwoolford
Last active August 29, 2015 14:11
Show Gist options
  • Save alexwoolford/c3ea79e3d7cf0ebaf7ac to your computer and use it in GitHub Desktop.
Save alexwoolford/c3ea79e3d7cf0ebaf7ac to your computer and use it in GitHub Desktop.

Keyless SSH with Ansible

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 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment