jcdarwin.ec2
ansible-role-ec2
This is an Ansible role that helps you create an EC2 Ubuntu image, and it's available on Ansible Galaxy.
You might need to change or add to the following file:
defaults/main.yml
Installation
To create EC2 instances, we need to install boto
:
# Install boto using pip
pip install boto
# Save your AWS credentials for boto
echo -e "[Credentials]\naws_access_key_id = REDACTED\naws_secret_access_key = REDACTED" > ~/.boto
Assuming you have a requirements.yml
file that looks like this:
# Install a role from GitHub
- name: ansible-role-ec2
src: https://github.com/jcdarwin/ansible-role-ec2
You can install the role locally using this requirements.yml
file:
# Install a role from GitHub
- name: ansible-role-ec2
src: https://github.com/jcdarwin/ansible-role-ec2
path: roles/
To install the role, use:
ansible-galaxy install -r requirements.yml -p ./roles
Requirements
None.
Role Variables
Here are the available variables and their default values found in defaults/main.yml
:
site:
name: demo
environment: development
ansible_role_ec2:
instance_type: m3.large
security_group: "{{ site.environment }}-{{ site.name }}"
# from https://cloud-images.ubuntu.com/locator/ec2/
image: ami-bf86aedc # Ubuntu 16.04 (ap-southeast-2)
image_user: ubuntu
region: ap-southeast-2
# image: ami-ba4ea4da # Amazon Linux (us-west-2)
# image_user: ec2-user
# region: us-west-2
keypair: aws
private_key: ~/.ssh/aws.pem
count: 1
Dependencies
None.
Example Playbook
When you run the Ansible role, the hosts
file will be created and will look like this:
[local]
127.0.0.1 ansible_python_interpreter=/usr/local/bin/python
[demo]
52.63.226.70 ansible_user=ubuntu ansible_private_key_file=~/.ssh/aws.pem
We have a playbook located at main.yml
.
To run the playbook:
# Make sure your hosts file is generated by your Vagrant repo
ansible all -m ping -i ansible/hosts -l all
ansible-playbook -l all create.yml -i ansible/hosts --tags create
You can then SSH into the instance using your key pair. The IP address will be in the ./ansible/hosts
file created by the role:
# Find the actual IP in ansible/hosts
ssh -i ~/.ssh/aws.pem ubuntu@52.63.226.70
After you're done with the EC2 instance, you can delete it:
ansible-playbook -l all destroy.yml -i ansible/hosts --tags destroy
License
MIT