basic playbooks to create/delete ec2 instances
This commit is contained in:
parent
31d02fd173
commit
5721c57e40
5 changed files with 109 additions and 0 deletions
10
ansible/create_ec2.yml
Normal file
10
ansible/create_ec2.yml
Normal file
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
|
||||
- name: Create EC2 instance
|
||||
hosts: localhost
|
||||
|
||||
vars_files:
|
||||
- "host_vars/{{initiative_name}}.yml"
|
||||
|
||||
roles:
|
||||
- ec2_instance
|
10
ansible/delete_ec2.yml
Normal file
10
ansible/delete_ec2.yml
Normal file
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
|
||||
- name: Create EC2 instance
|
||||
hosts: localhost
|
||||
|
||||
vars_files:
|
||||
- "host_vars/{{initiative_name}}.yml"
|
||||
|
||||
roles:
|
||||
- delete_ec2_instance
|
11
ansible/host_vars/arc-test.yml
Normal file
11
ansible/host_vars/arc-test.yml
Normal file
|
@ -0,0 +1,11 @@
|
|||
ami_id: ami-01efb339f953fdf36 #Fedora33 cloud image us-east-1
|
||||
ssh_key_name: 'Ansible Key'
|
||||
instance_name: "arc-{{initiative_name}}"
|
||||
disk_size: 30
|
||||
subnet_id: subnet-0ebb9d7d8e4db80df
|
||||
security_group_name: arc_default
|
||||
vpc_id: vpc-0fe7ead908f270a09
|
||||
aws_region: us-east-1
|
||||
open_ports: 22
|
||||
instance_type: t2.medium
|
||||
|
40
ansible/roles/delete_ec2_instance/tasks/main.yml
Normal file
40
ansible/roles/delete_ec2_instance/tasks/main.yml
Normal file
|
@ -0,0 +1,40 @@
|
|||
---
|
||||
|
||||
- name: Get instance id for termination
|
||||
ec2_instance_info:
|
||||
region: "{{ aws_region }}"
|
||||
filters:
|
||||
"tag:Name": "{{ instance_name }}"
|
||||
register: ec2_instance
|
||||
|
||||
- debug: msg="{{ec2_instance.instances[0].instance_id}}"
|
||||
|
||||
- name: Get security group id for deletion
|
||||
ec2_group_info:
|
||||
region: "{{ aws_region }}"
|
||||
filters:
|
||||
group_name: "{{ security_group_name }}"
|
||||
register: security_group
|
||||
|
||||
- debug: msg="{{security_group.security_groups[0].group_id}}"
|
||||
|
||||
- pause:
|
||||
prompt: "Are yout sure you want to delete {{ instance_name }} and {{ security_group_name }} (yes/no)?"
|
||||
register: my_pause
|
||||
delegate_to: localhost
|
||||
|
||||
- name: terminate instance
|
||||
ec2_instance:
|
||||
state: absent
|
||||
region: "{{ aws_region }}"
|
||||
instance_ids:
|
||||
- "{{ec2_instance.instances[0].instance_id}}"
|
||||
wait: yes
|
||||
when: hostvars['localhost'].my_pause.user_input | bool
|
||||
|
||||
- name: create security group
|
||||
ec2_group:
|
||||
name: "{{security_group.security_groups[0].group_id}}"
|
||||
region: "{{ aws_region }}"
|
||||
state: absent
|
||||
when: hostvars['localhost'].my_pause.user_input | bool
|
38
ansible/roles/ec2_instance/tasks/main.yml
Normal file
38
ansible/roles/ec2_instance/tasks/main.yml
Normal file
|
@ -0,0 +1,38 @@
|
|||
---
|
||||
|
||||
- name: create security group
|
||||
ec2_group:
|
||||
name: "{{ security_group_name }}"
|
||||
description: "Security group for {{initiative_name}}"
|
||||
vpc_id: "{{ vpc_id }}"
|
||||
region: "{{ aws_region }}"
|
||||
rules:
|
||||
- proto: tcp
|
||||
ports:
|
||||
- "{{item}}"
|
||||
cidr_ip: 0.0.0.0/0
|
||||
rule_desc: allow all on port {{item}}
|
||||
with_items: "{{ open_ports }}"
|
||||
register: security_group
|
||||
|
||||
|
||||
- name: create instance
|
||||
ec2_instance:
|
||||
state: present
|
||||
region: "{{ aws_region }}"
|
||||
instance_type: "{{ instance_type }}"
|
||||
image_id: "{{ ami_id }}"
|
||||
instance_role: "{{ instance_role | default(omit) }}"
|
||||
key_name: "{{ ssh_key_name }}"
|
||||
name: "{{ instance_name }}"
|
||||
security_group: "{{ security_group.results[0].group_id }}"
|
||||
tags:
|
||||
FedoraGroup: "Infra"
|
||||
Owner: "ARC"
|
||||
Initiative: "{{ initiative_name }}"
|
||||
volumes:
|
||||
- device_name: /dev/sda1
|
||||
ebs:
|
||||
volume_size: "{{ disk_size }}"
|
||||
delete_on_termination: true
|
||||
vpc_subnet_id: "{{ subnet_id }}"
|
Loading…
Add table
Add a link
Reference in a new issue