-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprov_aws.yml
101 lines (89 loc) · 3.28 KB
/
prov_aws.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
- name: Launch an instance on AWS and run Ansible roles on it
hosts: localhost
gather_facts: false
vars_files:
- vars/defaults_prov_aws.yml
tasks:
- name: Set the project name
set_fact:
my_project: my_default_project
# when: my_project == ""
when:
- my_project is not defined
- my_default_project is defined
- name: Launch instance
ec2:
# aws_access_key: "{{ my_aws_access_key }}"
# aws_secret_key: "{{ my_aws_secret_key }}"
key_name: "{{ my_keypair | default(my_default_keypair) }}"
instance_type: "{{ my_instance_type | default(my_default_instance_type) }}"
group: "{{ my_security_group | default(my_default_security_group) }}"
image: "{{ my_image | default(my_default_image) }}"
region: "{{ my_region | default(my_default_region) }}"
vpc_subnet_id: "{{ my_vpc_subnet_id | default(my_default_vpc_subnet_id) }}"
instance_tags:
Name: "{{ my_hostname | default(my_default_hostname) }}"
Project: "{{ my_project | default(my_default_project) | default(omit) }}"
assign_public_ip: true
wait: true
register: my_new_instance
- name: Do not assign an Elastic IP
set_fact:
elastic_ip_needed: default_elastic_ip_needed
when: elastic_ip_needed is not defined
- name: Add elastic IP to the new instance
ec2_eip:
region: "{{ my_region | default(my_default_region) }}"
instance_id: "{{ my_new_instance.instance_ids }}"
when: elastic_ip_needed
register: my_new_eip
# - name: Tag the volume of the new instance
# ec2_tag:
# region: "{{ my_region }}"
# state: present
# resource: "{{ my_new_eip. }}"
# tags:
# Name: "{{ my_hostname | default(my_default_hostname) }}"
# Project: "{{ my_project | default(my_default_project) | default(omit) }}"
# when: elastic_ip_needed
- name: Add a DNS record (A) for the new instance
route53:
state: present
zone: "{{ my_dns_zone | default(my_default_zone) }}"
record: "{{ item.tags.Name }}.{{ my_dns_zone | default(my_default_zone) }}"
value: "{{ item.public_ip }}"
type: A
ttl: 30
# wait: yes # No to don't wait until the changes have been replicated to all Amazon Route 53 DNS servers
loop: "{{ my_new_instance.instances }}"
when: my_dns_zone is defined
- name: Add new instance to the on-memory Inventory
add_host:
hostname: "{{ item.tags.Name }}.{{ my_dns_zone | default(my_default_zone) }}"
groups: launched
loop: "{{ my_new_instance.instances }}"
when: my_dns_zone is defined
- name: Add new instance to the on-memory Inventory
add_host:
hostname: "{{ item.public_dns_name }}"
groups: launched
loop: "{{ my_new_instance.instances }}"
when: my_dns_zone is not defined
- name: Wait for SSH to come up
delegate_to: "{{ item }}"
wait_for_connection:
delay: 10
timeout: 320
loop: "{{ groups['launched'] }}"
- name: Configure instance
hosts: launched
become: true
gather_facts: true
tasks:
- name: Execute post tasks for Linux Hosts
include_tasks: tasks/prov_aws-post_linux.yml
when: ansible_system == "Linux"
- name: Execute post tasks for Windows Hosts
include_tasks: tasks/prov_aws-post_windows.yml
when: ansible_system == "Windows"