-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathadhoc-provision-ec2-ci.yml
105 lines (95 loc) · 4.18 KB
/
adhoc-provision-ec2-ci.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
102
103
104
# this playbook will need some variables to deploy one or more instances (same instance type per call)
# aws_ec2_region: us-east-2 # (region)
# aws_ec2_instance_type: t2.large
# aws_ec2_instance_name: "duffy-ci-node" #prefix in front of computed name
# aws_ec2_security_group:
# aws_ec2_security_keypair: my_keypair
# aws_ec2_ebs_size: '20' # in GB
# aws_ec2_vpc_subnets: # subnet presents in that region for our vpc
# - subnet-zerr13e
# - subnet-0fqzefq
# - subnet-8648e45
# aws_ec2_user_data: |
# #cloud-config
# disable_root: False
# runcmd:
# - dnf update -y
# aws_access_key:
# aws_secret_key:
# aws_ec2_instances_count: 2
# aws_ec2_prefix: ip-172- # part of the private hostname we'll replace
# aws_ec2_internal_domain: ec2.internal # part of ec2 domain name we'll replace with our domain
---
- hosts: localhost
gather_facts: False
become: False
tasks:
- name: Instantiate EC2 instance[s]
amazon.aws.ec2_instance:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
name: "{{ aws_ec2_instance_name }}-{{ aws_ec2_instance_type }}-{{ aws_ec2_distro }}-{{ aws_ec2_dist_release }}-{{ aws_ec2_arch }}"
state: running
region: "{{ aws_ec2_region }}"
count: "{{ aws_ec2_instances_count }}"
instance_type: "{{ aws_ec2_instance_type }}"
image_id: "{{ aws_ec2_distributions[aws_ec2_distro]['releases'][aws_ec2_dist_release][aws_ec2_arch].ami_id }}"
instance_role: "{{ instance_role | default(omit) }}"
user_data: "{{ aws_ec2_user_data }}"
key_name: "{{ aws_ec2_security_keypair }}"
vpc_subnet_id: "{{ aws_ec2_vpc_subnets | random }}"
network:
assign_public_ip: true
security_group: "{{ aws_ec2_security_group }}"
volumes:
- device_name: /dev/sda1
ebs:
volume_size: "{{ aws_ec2_ebs_size }}"
delete_on_termination: True
- device_name: /dev/sdb
ebs:
volume_size: "{{ aws_ec2_ebs_size }}"
delete_on_termination: True
wait: True
tags:
CentOSGroup: "Duffy"
register: ec2_deploy_result
- name: Parsing returned json from ec2 with info we need
set_fact:
duffy_tmp:
nodes: "{{ ec2_deploy_result | json_query(nodes_query) }}"
vars:
nodes_query: "instances[*].{ec2_instance_id: instance_id, hostname: public_dns_name, ipaddr: public_ip_address, private_ipaddress: private_ip_address, internal_hostname: private_dns_name, ec2_instance_type: instance_type}"
- name: Creating list of returned nodes and transforming data
set_fact:
duffy_ec2_nodes: "{{ duffy_ec2_nodes|default([]) + [ {'ec2_instance_id': item.ec2_instance_id, 'ec2_instance_type': item.ec2_instance_type, 'hostname': item.internal_hostname |replace(aws_ec2_prefix, 'n') | replace(aws_ec2_internal_domain, 'pool.ci.centos.org'), 'ipaddr': item.private_ipaddress, 'public_hostname': item.hostname, 'public_ipaddress': item.ipaddr} ] }}"
with_items: "{{ duffy_tmp['nodes'] }}"
- name: Creating/Updating route53 records
community.aws.route53:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
hosted_zone_id: "{{ aws_route53_zoneid }}"
overwrite: True
record: "{{ item.hostname }}"
value: "{{ item.ipaddr }}"
state: present
ttl: 60
type: A
wait: yes
with_items: "{{ duffy_ec2_nodes }}"
loop_control:
label: "{{ item.hostname }}"
- name: EC2 instance[s] public ip address[es]
debug:
msg: "EC2 public ip address is {{ item }}"
with_items: "{{ ec2_deploy_result | json_query('instances[*].public_ip_address') }}"
- name: Waiting for EC2 instance[s] to be reachable
wait_for:
host: "{{ item }}"
port: 22
delay: 5
with_items: "{{ ec2_deploy_result | json_query('instances[*].public_ip_address') }}"
- name: Sending data back to Duffy provisioner
set_fact:
duffy_out:
nodes: "{{ duffy_ec2_nodes }}"