Skip to content

Instantly share code, notes, and snippets.

@larrybolt
Created August 23, 2014 13:55
Show Gist options
  • Save larrybolt/85b59f47615be9fcb643 to your computer and use it in GitHub Desktop.
Save larrybolt/85b59f47615be9fcb643 to your computer and use it in GitHub Desktop.
Bootstrap server to use with ansible
#!/usr/bin/env bash
if [ $# -eq 0 ]; then
echo "please supply a servername: bootstrap-server.sh node.example.com"
exit 1
fi
username=`whoami`
# copy public rsa key to server for root user
read -r -p "Transfer ssh public keys to server for root? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
cat ~/.ssh/id_rsa.pub | ssh root@$1 "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
ssh root@$1 "chmod 0700 ~/.ssh && chmod 0644 ~/.ssh/authorized_keys"
fi
# install sudo, add user and add user to sudo group
read -r -p "This will install sudo, and add $username with sudo rights, continue? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
ssh root@$1 "apt-get update && apt-get install sudo -y && id $username || (adduser $username && usermod -a -G sudo $username)"
fi
# copy public rsa key to server for self
read -r -p "Transfer ssh public keys to server for "$username"? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
cat ~/.ssh/id_rsa.pub | ssh $1 "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
ssh $1 "chmod 0700 ~/.ssh && chmod 0644 ~/.ssh/authorized_keys"
fi
# install python for ansible
read -r -p "Make host ansible ready? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
ssh root@$1 "apt-get install -y python python-apt python-pycurl"
fi
# add hostname to ansible_hosts
read -r -p "Add to ansible_hosts? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "[added]\n$1" >> ansible_hosts
echo "Added to ansible_hosts, please alter category afterwards!"
fi
@mxxcon
Copy link

mxxcon commented Aug 24, 2014

Instead of lines 11/12 and 24/25 consider using ssh-copy-id command. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment