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
|
---
- set_fact:
created_by: "{{ lookup('env', 'LOGNAME')|default(cluster, true) }}"
env: "{{ cluster }}"
env_host_type: "{{ cluster }}-openshift-{{ type }}"
host_type: "{{ type }}"
machine_type: "{{ lookup('env', 'ec2_instance_type')
| default(deployment_vars[deployment_type].type, true) }}"
machine_image: "{{ lookup('env', 'ec2_ami')
| default(deployment_vars[deployment_type].image, true) }}"
machine_region: "{{ lookup('env', 'ec2_region')
| default(deployment_vars[deployment_type].region, true) }}"
machine_keypair: "{{ lookup('env', 'ec2_keypair')
| default(deployment_vars[deployment_type].keypair, true) }}"
machine_subnet: "{{ lookup('env', 'ec2_vpc_subnet')
| default(deployment_vars[deployment_type].vpc_subnet, true) }}"
machine_public_ip: "{{ lookup('env', 'ec2_public_ip')
| default(deployment_vars[deployment_type].assign_public_ip, true) }}"
security_groups: "{{ lookup('env', 'ec2_security_groups')
| default(deployment_vars[deployment_type].security_groups, true) }}"
- name: Launch instance(s)
ec2:
state: present
region: "{{ machine_region }}"
keypair: "{{ machine_keypair }}"
group: "{{ security_groups }}"
instance_type: "{{ machine_type }}"
image: "{{ machine_image }}"
count: "{{ instances | oo_len }}"
vpc_subnet_id: "{{ machine_subnet | default(omit, true) }}"
assign_public_ip: "{{ machine_public_ip | default(omit, true) }}"
wait: yes
instance_tags:
created-by: "{{ created_by }}"
env: "{{ env }}"
host-type: "{{ host_type }}"
env-host-type: "{{ env_host_type }}"
deployment-type: "{{ deployment_type }}"
register: ec2
- name: Add Name tag to instances
ec2_tag: resource={{ item.1.id }} region={{ machine_region }} state=present
with_together:
- instances
- ec2.instances
args:
tags:
Name: "{{ item.0 }}"
- set_fact:
instance_groups: tag_created-by_{{ created_by }}, tag_env_{{ env }}, tag_host-type_{{ host_type }}, tag_env-host-type_{{ env_host_type }}, tag_deployment-type_{{ deployment_type }}
- name: Add new instances groups and variables
add_host:
hostname: "{{ item.0 }}"
ansible_ssh_host: "{{ item.1.dns_name }}"
ansible_ssh_user: "{{ deployment_vars[deployment_type].ssh_user }}"
ansible_sudo: "{{ deployment_vars[deployment_type].sudo }}"
groups: "{{ instance_groups }}"
ec2_private_ip_address: "{{ item.1.private_ip }}"
ec2_ip_address: "{{ item.1.public_ip }}"
with_together:
- instances
- ec2.instances
- name: Wait for ssh
wait_for: "port=22 host={{ item.dns_name }}"
with_items: ec2.instances
- name: Wait for user setup
command: "ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null {{ hostvars[item.0].ansible_ssh_user }}@{{ item.1.dns_name }} echo {{ hostvars[item.0].ansible_ssh_user }} user is setup"
register: result
until: result.rc == 0
retries: 20
delay: 10
with_together:
- instances
- ec2.instances
|