Skip to content

Instantly share code, notes, and snippets.

@madetech-com
Created April 5, 2017 13:16
Show Gist options
  • Save madetech-com/2ab17213b19b2713d0e072b4382c9246 to your computer and use it in GitHub Desktop.
Save madetech-com/2ab17213b19b2713d0e072b4382c9246 to your computer and use it in GitHub Desktop.
"Using Ansible for infrastucture" post - code samples
[playground]
aws_access_key_id = 13ABCHHASDBYB2U3NG34NG
aws_secret_access_key = nfu8n3787N4F874GN8n7g847878G87NG/GUNREIN
ssh-keygen -t rsa -C web -f ./web -P ''
---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Add web keypair
ec2_key:
name: web
key_material: "{{ lookup('file', 'web.pub') }}"
state: present
AWS_PROFILE=playground AWS_REGION=eu-west-1 ansible-playbook build-infra.yml
- name: Add web instances security group
ec2_group:
name: web_instances
description: Web instances
rules_egress:
- proto: -1
from_port: -1
to_port: -1
cidr_ip: 0.0.0.0/0
rules:
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- name: Provision web instances
ec2:
key_name: web
group: web_instances
instance_type: t2.nano
image: ami-a192bad2
wait: true
exact_count: 3
count_tag:
Name: web
instance_tags:
Name: web
register: web_instances
- name: Assign EIP address for web instances
ec2_eip:
device_id: "{{ item.id }}"
in_vpc: true
release_on_disassociation: true
with_items: "{{ web_instances.tagged_instances }}"
- name: Add load balancer security group
ec2_group:
name: web_load_balancer
description: Web load balancer
rules_egress:
- proto: -1
from_port: -1
to_port: -1
cidr_ip: 0.0.0.0/0
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- name: Setup web load balancer
ec2_elb_lb:
name: web
state: present
idle_timeout: 300
zones:
- eu-west-1a
listeners:
- protocol: http
load_balancer_port: 80
instance_port: 80
security_group_names:
- web_load_balancer
register: web_load_balancer
- name: Add web instances to load balancer
ec2_elb:
instance_id: "{{ item.id }}"
ec2_elbs: web
state: present
wait: false
when: item.id not in web_load_balancer.elb.instances
with_items: "{{ web_instances.tagged_instances }}"
curl -o ec2.py \
https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
chmod u+x ec2.py
curl -o ec2.ini \
https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
---
- hosts: security_group_web_instances
become: yes
become_method: sudo
remote_user: ubuntu
tasks:
- name: Install Apache and PHP
apt:
name: php5
update_cache: true
notify: restart apache
- name: Add a PHP script
copy:
src: files/index.php
dest: /var/www/html/index.php
- name: Remove default index.html
file:
path: /var/www/html/index.html
state: absent
handlers:
- name: restart apache
service:
name: apache2
state: restarted
- hosts: localhost
connection: local
gather_facts: false
tasks:
- ec2_elb_facts:
names: web
register: elb_facts
- debug:
msg: "{{ elb_facts.elbs.0.dns_name }}"
AWS_PROFILE=playground \
AWS_REGION=eu-west-1 \
ANSIBLE_HOST_KEY_CHECKING=false \
ANSIBLE_PRIVATE_KEY_FILE=web \
ansible-playbook -i ec2.py provision-infra.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment