summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranck Cuny <franck.cuny@gmail.com>2014-02-01 16:40:09 -0800
committerFranck Cuny <franck.cuny@gmail.com>2014-02-01 16:40:09 -0800
commit235a885bad73132ffb5f558b0e265a940e586277 (patch)
tree9cc9b03d91035a86a32e440a60f2a7f98aa0a065
parentChange the font for the "code" block. (diff)
downloadlumberjaph-235a885bad73132ffb5f558b0e265a940e586277.tar.gz
Prepare the article for vagrant / ansible / ec2.
-rw-r--r--_drafts/provision-vagrant-with-ansible-to-aws.md39
-rw-r--r--_posts/2014-02-01-provision-an-ec2-instance-with-vagrant-and-ansible.md127
2 files changed, 127 insertions, 39 deletions
diff --git a/_drafts/provision-vagrant-with-ansible-to-aws.md b/_drafts/provision-vagrant-with-ansible-to-aws.md
deleted file mode 100644
index c0c42ae..0000000
--- a/_drafts/provision-vagrant-with-ansible-to-aws.md
+++ /dev/null
@@ -1,39 +0,0 @@
----
-title: Provision a AWS server with Vagrant and Ansible
-summary: In which I provision a AWS server using Vagrant and Ansible
-layout: post
----
-
-I have a private repository with all my rules for ansible. You'll need a simple playbook with the following content:
-
-```yaml
-miranda ~/code/git/shiro λ more vagrant.yml
--
- hosts: all
- sudo: yes
- vars_files:
- - vars/defaults.yml
- roles:
- - common
-```
-
-First let's try our setup with a local box: `vagrant box add precise64 http://files.vagrantup.com/precise64.box`.
-
-Now we can create a Vagrantfile:
-
-```ruby
-Vagrant.configure("2") do |config|
- config.vm.box = "precise64"
- config.vm.box_url = "http://files.vagrantup.com/precise64.box"
-
- config.ssh.forward_agent = true
-
- config.vm.provision "ansible" do |ansible|
- ansible.playbook = ENV['VAGRANT_ANSIBLE_PLAYBOOK']
- end
-end
-```
-
-If we run `vagrant up`, a box will be started and provisioned. Once it's done, you can run `vagrant ssh` and once logged inside the VM, run `htop`.
-
-Now let's move to the configuration for AWS. `vagrant plugin install vagrant-aws`. Then `vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box`.
diff --git a/_posts/2014-02-01-provision-an-ec2-instance-with-vagrant-and-ansible.md b/_posts/2014-02-01-provision-an-ec2-instance-with-vagrant-and-ansible.md
new file mode 100644
index 0000000..a64cbac
--- /dev/null
+++ b/_posts/2014-02-01-provision-an-ec2-instance-with-vagrant-and-ansible.md
@@ -0,0 +1,127 @@
+---
+title: Provisioning an EC2 instance with Vagrant and Ansible
+summary: In which I provision an EC2 instance with Vagrant and Ansible
+layout: post
+---
+
+I like using [Ansible](http://www.ansible.com/) to manage my personal servers. It forces me to make the environment reproducible so I don't have to care about a specific box: I can throw them away easily, knowing I can get a new one when I need.
+
+Ansible is also easier to reason about than Chef or Puppet, at least to me. The fact that I have to manage and maintain only a few machines is probably why.
+
+When I develop on my personal projects, I use a lot of VMs, and that's where [Vagrant](http://www.vagrantup.com/) enters the picture. Being able to start a local VM and get a clean environment quickly is invaluable to me. It makes it easy to test an application or library in different situation.
+
+But sometimes a local VM is not enough, and you need a more powerful server, so now you need an EC2 instance.
+
+My goal with this article is to show how easy you can combine Vagrant with Ansible to provision an EC2 instance.
+
+## The basic
+
+I have a private repository with all my rules for Ansible. But for this post, all we need is a simple playbook. So let's start by creating a directory named *vagrant*, and put inside a configuration file named *playbook.yml*, with the following content:
+
+```yaml
+-
+ hosts: all
+ sudo: yes
+ tasks:
+ - name: Install htop
+ apt: pkg=htop state=installed
+```
+
+What we're saying, is that for all the hosts in our inventory we will use `sudo` to install the program `htop` using `apt-get` (yes, I assume you're using a debian-based system, but you get the idea).
+
+First, we will try the setup on a local box. If you don't already have a Vagrant box installed, you can grab a new one by running `vagrant box add precise64 http://files.vagrantup.com/precise64.box`.
+
+Now we can add the configuration file named *Vagrantfile* with this content.
+
+```ruby
+Vagrant.configure("2") do |config|
+ config.vm.box = "precise64"
+ config.vm.box_url = "http://files.vagrantup.com/precise64.box"
+
+ config.ssh.forward_agent = true
+ config.vm.provision :ansible, :playbook => "playbook.yml"
+end
+```
+
+This file says that we will use the box named *precise64*, located at the given URL, and we want to provision it using Ansible, and the path to the playbook.
+
+By running `vagrant up`, a box gets started and provisioned. The output should be similar to this:
+
+```
+Bringing machine 'default' up with 'virtualbox' provider...
+[default] Importing base box 'precise64'...
+[default] Matching MAC address for NAT networking...
+[default] Setting the name of the VM...
+[default] Clearing any previously set forwarded ports...
+[default] Clearing any previously set network interfaces...
+[default] Preparing network interfaces based on configuration...
+[default] Forwarding ports...
+[default] -- 22 => 2222 (adapter 1)
+[default] Booting VM...
+[default] Waiting for machine to boot. This may take a few minutes...
+[default] Machine booted and ready!
+[default] Mounting shared folders...
+[default] -- /vagrant
+[default] Running provisioner: ansible...
+
+PLAY [all] ********************************************************************
+
+GATHERING FACTS ***************************************************************
+The authenticity of host '[127.0.0.1]:2222 ([127.0.0.1]:2222)' can't be established.
+RSA key fingerprint is 50:db:75:ba:11:2f:43:c9:ab:14:40:6d:7f:a1:ee:e3.
+Are you sure you want to continue connecting (yes/no)? yes
+ok: [default]
+
+TASK: [Install htop] **********************************************************
+changed: [default]
+
+PLAY RECAP ********************************************************************
+default : ok=2 changed=1 unreachable=0 failed=0
+```
+
+As we can see, everything went well, and the application `htop` was successfully installed. We can now run `vagrant ssh` and once logged inside the VM, run `htop`.
+
+## AWS
+
+I've created a key pair for Vagrant in the AWS console. Note the access and secret access keys, and download the SSH private key too. For this article, we will put the key into the same directory as our playbook and Vagrant's configuration.
+
+We need to install a plugin for that: `vagrant plugin install vagrant-aws`. We also need to modify our *Vagrantfile* to use a different box, and also add the configuration for AWS.
+
+```ruby
+Vagrant.configure("2") do |config|
+ config.vm.provision :ansible, :playbook => 'playbook.yml'
+
+ # This configuration is for our local box, when we use virtualbox as the provider
+ config.vm.provider :virtualbox do |vb, override|
+ override.vm.box = "precise64"
+ override.vm.box_url = "http://files.vagrantup.com/precise64.box"
+ end
+
+ # This configuration is for our EC2 instance
+ config.vm.provider :aws do |aws, override|
+ aws.access_key_id = "access key"
+ aws.secret_access_key = "secret access key"
+ # ubuntu AMI
+ aws.ami = "ami-e7582d8e"
+ aws.keypair_name = "vagrant"
+ aws.security_groups = ["default", "quicklaunch-1"]
+
+ override.vm.box = "dummy"
+ override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
+ override.ssh.username = "ubuntu"
+ override.ssh.private_key_path = "vagrant.pem"
+ end
+end
+```
+
+We need to override the user name to *ubuntu* and specify the path to the private key (the one we got from the AWS console when we created our new key pair) to log into the instance. The box also needs to be overridden.
+
+Running `vagrant up --provider=aws` will provision the box. It will also generates an inventory file for you inside the directory, so ansible will know what to do.
+
+It will takes a few minutes to start the instance and run the provisioning part. Wait a few minutes, but if it looks like the system is stuck, you can re-run the previous command by exporting `VAGRANT_LOG=debug` in order to get more detailed information.
+
+> If the provisioning blocks while trying to connect to ssh, it's probably because your security group doesn't allow SSH connections.
+
+Now `vagrant ssh` will dump you into the VM and you should be able to run `htop`.
+
+Don't forget to run `vagrant halt` and `vagrant destroy` once you're done!