Skip to content

Instantly share code, notes, and snippets.

@vastus
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save vastus/89c8fe1def0a459bebc4 to your computer and use it in GitHub Desktop.

Select an option

Save vastus/89c8fe1def0a459bebc4 to your computer and use it in GitHub Desktop.
Provisioning w/ Ansible

Provisioning (Ansible)

A getting started guide.

Prerequisites

Given that you have set up a new instance and the instance is up and running. Python 2 should be already installed on OS X.

If you want to try this out quicky I recommend using Digital Ocean. You can use my referral link to get you some starting money $10 by registering via https://www.digitalocean.com/?refcode=e8eaca937c55.

  • ansible brew install ansible
  • ssh-copy-id brew install ssh-copy-id

Set environment variables

Do the following on your local machine.

The instance's IP address.

export HOST=your.host.ip.here

Let's call the provision user deployer. Feel free to change.

export PROVUSER=deployer

Preppin'

Add user to host machine

Add user with home dir and bash as shell.

ssh root@$HOST useradd -m -s /bin/bash $PROVUSER

Give her a password.

ssh root@$HOST passwd $PROVUSER

Create an SSH key

Defaults are expected. Pick a passphrase, don't leave it empty.

ssh-keygen -t rsa

Start agent.

ssh-agent bash

Add your identity.

ssh-add

Copy key to host

On your local machine.

ssh-copy-id -i ~/.ssh/id_rsa.pub $PROVUSER@$HOST

Ensure that keys match. The command returns 0 on success.

diff -u  <(ssh $PROVUSER@$HOST cat /home/$PROVUSER/.ssh/authorized_keys) ~/.ssh/id_rsa.pub > /dev/null; echo $?

If you get a non-zero value; ssh to host and and compare host's ~/.ssh/authorized_keys with your public key ~/.ssh/id_rsa.pub on your local machine. (Or remove the redirection to /dev/null to see the diff.)

Ansible

Do the following steps in your project dir. You could place playbooks and other related files to a folder called $PROJECT_DIR/provisioning for instance.

Hosts file

This is where your hosts are defined. For example cat $PROJECT_DIR/provisioning/hosts shows:

[ec2]
1.1.1.1
1.1.1.2

[rds]
1.1.2.1

Ping 'em

ansible all -u $PROVUSER -m ping -i hosts

If you got a green light you're good to go. Otherwise run the command again with -vvvv flag and check what's up.

Contributing

Any additions are welcome. Just be short and descriptive. Keep it simple.

Authors

Juho Hautala

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