-
Notifications
You must be signed in to change notification settings - Fork 13
/
create-vm.yaml
61 lines (57 loc) · 1.69 KB
/
create-vm.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/ansible-playbook
#
# Deploy a virtual machine in AWS EC2 default VPC
#
# - find default VPC in AWS region specified with environment variables
# - create a virtual machine in default subnet
# - adjust default security group to allow SSH access to default subnet
#
# Input parameters:
# - AWS region specified in environment variable (default: us-west-1)
# - ec2_ami: image ID
# - ec2_keypair: name of the keypair
# - ec2_keydata: public SSH key (optional, default: take from .ssh)
# - ec2_vm_tag: instance tag
#
---
- hosts: localhost
vars_files:
- parameters.yaml
tasks:
- name: Find default VPC
ec2_vpc_net_info:
filters:
isDefault: "true"
register: vpc_info
- name: Find subnets in default VPC
ec2_vpc_subnet_info:
filters:
vpc-id: "{{ vpc_info.vpcs[0].vpc_id }}"
register: subnet_info
- name: Add inbound access to default SG
ec2_group:
vpc_id: "{{ vpc_info.vpcs[0].vpc_id }}"
name: default
description: default VPC security group
purge_rules: no
purge_rules_egress: no
rules:
- proto: tcp
ports: "{{ [ 22,ec2_open_ports|default([]) ]|flatten }}"
cidr_ip: 0.0.0.0/0
- name: create ec2 key pair
ec2_key:
name: "{{ ec2_keypair }}"
key_material: "{{ ec2_keydata|default(lookup('file', '~/.ssh/id_rsa.pub')) }}"
- name: Launch an EC2 instance
ec2:
image: "{{ ec2_ami }}"
instance_type: t2.micro
wait: true
key_name: "test_key"
vpc_subnet_id: "{{ subnet_info.subnets[0].subnet_id }}"
exact_count: "{{ ec2_inst_count|default(1) }}"
count_tag:
Name: "{{ ec2_vm_tag }}"
instance_tags:
Name: "{{ ec2_vm_tag }}"