-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovision_aws.yml
More file actions
77 lines (68 loc) · 2.68 KB
/
provision_aws.yml
File metadata and controls
77 lines (68 loc) · 2.68 KB
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
71
72
73
74
75
76
77
---
- name: Create and Launch AWS instance
hosts: localhost
gather_facts: no
tasks:
- name: Launch new AWS instance
amazon.aws.ec2_instance:
key_name: mykeyfile # Key created from AWS
instance_type: t2.medium
image_id: ami-0cf2b4e024cdb6960
region: us-west-2 #Oregon
security_group: "Minecraft Security" #need to create/fix
count: 1
wait: yes
tags:
Name: minecraft-servers1
register: ec2
- name: Find the instance with the specific tag
set_fact:
named_instance: "{{ ec2.instances | selectattr('tags.Name', 'equalto', 'minecraft-servers1') | first }}"
when: ec2.instances | length > 0
# - name: Debug named_instance
# debug:
# var: named_instance
- name: Wait for instance public IP to be available
local_action:
module: wait_for
host: "{{ named_instance.public_ip_address }}"
port: 22
delay: 60
timeout: 320
state: started
until: named_instance.public_ip_address is defined and named_instance.public_ip_address != ''
retries: 10
delay: 30
when: named_instance is defined and named_instance.public_ip_address is defined and named_instance.public_ip_address != ''
- name: Refresh instance details to get the public IP
amazon.aws.ec2_instance_info:
region: us-west-2
instance_ids: "{{ named_instance.instance_id }}"
register: ec2_info
until: ec2_info.instances[0].public_ip_address is defined and ec2_info.instances[0].public_ip_address != ''
retries: 10
delay: 30
# - name: Debug refreshed instance details
# debug:
# var: ec2_info
- name: Update named_instance fact with refreshed instance details
set_fact:
named_instance: "{{ ec2_info.instances[0] }}"
when: ec2_info.instances[0].public_ip_address is defined and ec2_info.instances[0].public_ip_address != ''
- name: Add new instance to group
add_host:
hostname: "{{ named_instance.public_ip_address }}"
groupname: launched
when: named_instance.public_ip_address is defined and named_instance.public_ip_address != ''
- name: Ensure temporary inventory directory exists
file:
path: /tmp/ansible-inventory
state: directory
mode: '0755'
- name: Add new instance to inventory file
copy:
content: |
[launched]
{{ named_instance.public_ip_address }} ansible_user=ubuntu ansible_ssh_private_key_file=~/Downloads/mykeyfile.pem
dest: /tmp/ansible-inventory/hosts
when: named_instance.public_ip_address is defined and named_instance.public_ip_address != ''