Tuesday, July 16, 2013

Ansible - how to launch EC2 instances and setup the php environment

In this post, we will create a script that will launch an instance in the EC2 cloud and install php and nginx (Installing httpd is going to be very similar) on it.

First you will need to set be Ansible.

If you are using ubuntu, read Install Ansible on ubuntu EC2.

If you are using a Mac, read Installing and Running Ansible on Mac OSX and pinging ec2 machines.

You must:
  • have python boto installed
  • set up the AWS access keys in the environment settings
Adding a host

We will use the ec2 module. It runs against localhost, so we will add a host entry.

vi /etc/ansible/hosts

Append the following:

localhost ansible_connection=local

Launching a micro instance

- name: create instances
hosts: localhost
vars:
keypair: myKeyPair
security_group: mySecurityGroup
instance_type: t1.micro
image: ami-05355a6c
count: 1
region: us-east-1
user: ec2-user
sudo: no
tasks:
- name: launch instances
local_action: ec2 keypair={{keypair}} group={{security_group}} instance_type={{instance_type}} image={{image}} wait=true count={{count}} region={{region}} instance_tags='{"Name":"ansible"}'
register: ec2
- name: add all instance public IPs to host group
local_action: add_host hostname={{item.public_ip}} groupname=ec2-servers ansible_ssh_user=ec2-user ansible_ssh_private_key_file=/etc/ansible/wundr_rest.pem
with_items: ec2.instances
- name: Create volumes and attach
local_action: ec2_vol instance={{item.id}} volume_size=8 region={{region}} device_name=xvdf
with_items: ec2.instances
register: ec2_vol
- name: wait for ssh to come up
local_action: wait_for host={{item.public_dns_name}} port=22 timeout=320 state=started
with_items: ec2.instances
- name: get instances stats
hosts: ec2-servers
sudo: true
gather_facts: true
view raw launch.yml hosted with ❤ by GitHub


Label this launch_playbook.yml

Execute the script.
ansible-playbook launch_playbook.yml
In your AWS EC2 console, you will see an instance named ansible. Each task is executed in sequence.

Now add this new host in the ansible host file and label it webservers.

vi /etc/ansible/hosts
[webservers]
{the_ip_of_ec2_instance_we_just_created} ansible_connection=ssh ansible_ssh_user=ec2-user ansible_ssh_private_key_file={path_to_aws_private_key}
You don't have to do the above. In fact, you can use the group name "ec2-servers" for the following script. But the following script will need to be in the same file as the first script. I am just separating these files for easier configuration in the future.


Installing php, nginx, mysql

- name: configure instances
hosts: webservers
user: ec2-user
sudo: yes
tasks:
- name: update machine with latest packages
action: command yum -y update
- name: install php
action: yum pkg=php state=latest
- name: install php-mysql
action: yum pkg=php-mysql state=latest
- name: install nginx
action: yum pkg=nginx state=latest
- name: ensure nginx is running
action: service name=nginx state=started
- name: install mysql server
action: yum pkg=mysql-server state=latest
- name: make sure mysql is running
action: service name=mysqld state=started
view raw configure.yml hosted with ❤ by GitHub
Label this configure_playbook.yml

Execute the script.
ansible-playbook configure_playbook.yml
Go to the public address of this instance. You should see the nginx welcoming message.

Remember to terminate the instance when you finish, else it will incur charges.

No comments:

Post a Comment