-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_ec2_vm.yml
73 lines (63 loc) · 1.97 KB
/
create_ec2_vm.yml
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
62
63
64
65
66
67
68
69
70
---
- name: Creating an EC2 Instance with Creating a VPC and Subnet Id
hosts: localhost
tasks:
- name: Create EC2 SSH key pair
amazon.aws.ec2_key:
name: "{{ prefix }}-ssh-key"
region: "{{ region }}"
no_log: true
register: aws_ec2_key_pair
- name: Create VPC Network
amazon.aws.ec2_vpc_net:
name: "{{ prefix }}-vpc_net"
cidr_block: "{{ cidr_block }}"
region: "{{ region }}"
register: vpc
- name: Create VPC Network
amazon.aws.ec2_vpc_subnet:
cidr: "{{ cidr }}"
region: "{{ region }}"
vpc_id: "{{ vpc.vpc.id }}"
register: subnet
- name: Create a Security Group
amazon.aws.ec2_security_group:
name: "{{ prefix }}-security_group"
description: "{{ prefix }}-test-sg"
vpc_id: "{{ vpc.vpc.id }}"
region: "{{ region }}"
rules:
- proto: tcp
ports:
- 80
cidr_ip: 0.0.0.0/0
rule_desc: "allow all on port 80"
- proto: tcp
ports:
- 22
cidr_ip: 0.0.0.0/0
rule_desc: "allow all on port 22"
register: security_group
- name: Create Virtual Machine
amazon.aws.ec2_instance:
name: "{{ prefix }}-vm"
key_name: "{{ prefix }}-ssh-key"
vpc_subnet_id: "{{ subnet.subnet.id }}"
instance_type: "{{ instance_type }}"
security_group: "{{ security_group.group_id }}"
count: 1
wait: yes
wait_timeout: 600
aws_region: "{{ region }}"
network:
assign_public_ip: true
image_id: "{{ ami }}"
register: vm
- name: Gathering VM information using instance ID
amazon.aws.ec2_instance_info:
instance_ids: "{{ vm.instance_ids[0] }}"
region: "{{ region }}"
register: info
- name: Display VM Public IP
debug:
msg: "Your public IP address is: {{ info.instances[0].public_ip_address }}"