-
Notifications
You must be signed in to change notification settings - Fork 6
/
createAwsEc2.yml
49 lines (45 loc) · 1.46 KB
/
createAwsEc2.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
---
# Create ec2 instance and add it to ansible inventory
- name: Create a sandbox instance
hosts: localhost
connection: local
gather_facts: False
vars:
keyname: psp_ansible_key
instance_type: t2.micro
security_group: launch-wizard-1
image: ami-0be057a22c63962cb
region: eu-west-2
tagname: Name=TestServer
tasks:
- name: Upload public key to AWS
ec2_key:
name: "{{ keyname }}"
key_material: "{{ lookup('file', '~/.ssh/{{ keyname }}.pub') }}"
region: "{{ region }}"
- name: Launch instance
ec2:
key_name: "{{ keyname }}"
group: "{{ security_group }}"
instance_type: "{{ instance_type }}"
image: "{{ image }}"
wait: true
region: "{{ region }}"
vpc_subnet_id: subnet-02a17e56e6827124a
assign_public_ip: yes
instance_tags: "{{tagname}}"
register: ec2
- name: Add new instance to hosts group for test
local_action: lineinfile
dest="~/ansible_hosts"
regexp={{ item.private_ip }}
insertafter="[tstlaunched]"
line="{{ item.private_ip }}"
state=present
with_items: "{{ ec2.instances }}"
- name: Wait for SSH to come up
local_action: wait_for
host={{ item.private_ip }}
port=22
state=started
with_items: "{{ ec2.instances }}"