This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
deploy-aws.yaml
184 lines (157 loc) · 5.76 KB
/
deploy-aws.yaml
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
- hosts: cloud
gather_facts: false
become: false
vars:
test_os: "{{ lookup('env', 'TEST_OS') | default('centos-stream-9', true) }}"
arch: "{{ lookup('env', 'ARCH') | default('x86_64', true) }}"
ssh_key_pub: ""
inventory_file: ""
download_node: "{{ lookup('env', 'DOWNLOAD_NODE') | default('', true) }}"
ami_id: ""
ami:
x86_64:
rhel-9-4: ami-0f7ce5456dd6fab1e
centos-stream-9: ami-08f2fe20b72b2ffa7
aarch64:
rhel-9-4: ami-03c8fde5ead1c5215
centos-stream-9: ami-09866c25c2d97b6cc
instance_type:
x86_64:
"0": t2.medium
"1": t3.medium
"2": m6a.large
aarch64:
"0": t4g.medium
"1": c7g.medium
"2": m6g.medium
tasks:
- set_fact:
random_num: "{{ 9999 | random(start=1001) }}"
- set_fact:
instance_name: "bootc-aws-{{ test_os }}-{{ random_num }}"
- name: random number for instance type
set_fact:
instance_type_index: "{{ 3 | random(start=0) }}"
- name: set random instance type
set_fact:
random_instance_type: "{{ lookup('env', 'instance_type') | default(instance_type[arch][instance_type_index], true) }}"
- name: "get available zone for instance {{ random_instance_type }}"
shell: aws ec2 describe-instance-type-offerings --location-type availability-zone --filters="Name=instance-type,Values={{ random_instance_type }}" --query InstanceTypeOfferings | jq -r '.[0].Location'
register: ec2_zone
when: "'rhel' not in test_os"
- name: get subnet
amazon.aws.ec2_vpc_subnet_info:
filters:
"tag:Name": "kite-ci"
"availabilityZone": "{{ ec2_zone.stdout }}"
register: ec2_vpc_subnet
when: "'rhel' not in test_os"
- set_fact:
subnet_id: "{{ ec2_vpc_subnet.subnets[0].subnet_id }}"
when: "'rhel' not in test_os"
- name: get virtqe subnet
amazon.aws.ec2_vpc_subnet_info:
filters:
"tag:Name": "InternalA-virtqe"
register: ec2_vpc_subnet
when: '"rhel" in test_os'
- set_fact:
subnet_id: "{{ ec2_vpc_subnet.subnets[0].subnet_id }}"
when: "'rhel' in test_os"
- name: get security group
amazon.aws.ec2_security_group_info:
filters:
"tag:Name": "kite-ci"
register: ec2_security_group
when: "'rhel' not in test_os"
- set_fact:
group_id: "{{ ec2_security_group.security_groups[0].group_id }}"
when: "'rhel' not in test_os"
- name: get virtqe security group
amazon.aws.ec2_security_group_info:
filters:
"tag:Name": "bootc-test"
register: ec2_security_group
when: "'rhel' in test_os"
- set_fact:
group_id: "{{ ec2_security_group.security_groups[0].group_id }}"
when: "'rhel' in test_os"
- name: config ssh keypair used by test
amazon.aws.ec2_key:
name: "kp-bootc-{{ random_num }}"
key_material: "{{ lookup('file', ssh_key_pub) }}"
tags:
name: "bootc-test"
- name: generate ec2_run_instance script
template:
src: ec2_run_instance.j2
dest: "{{ playbook_dir }}/ec2_run_instance.sh"
mode: 0755
- name: run ec2 instance with script
command: "{{ playbook_dir }}/ec2_run_instance.sh"
register: result_instance
- name: convert run_instance output to json
set_fact:
instance_json: "{{ result_instance.stdout | from_json }}"
- name: wait for instance running
shell: aws ec2 describe-instances --instance-ids {{ instance_json.Instances[0].InstanceId }} --query 'Reservations[0].Instances[0].State.Name' --output text
register: describe_result
retries: 60
delay: 5
until: describe_result.stdout == "running"
- name: get instance public ip
shell: aws ec2 describe-instances --instance-ids {{ instance_json.Instances[0].InstanceId }} --query 'Reservations[*].Instances[*].PublicIpAddress' --output text
register: ip_result
when: "'rhel' not in test_os"
- set_fact:
instance_ip: "{{ ip_result.stdout }}"
when: "'rhel' not in test_os"
- name: get instance private ip
shell: aws ec2 describe-instances --instance-ids {{ instance_json.Instances[0].InstanceId }} --query 'Reservations[*].Instances[*].PrivateIpAddress' --output text
register: ip_result
when: "'rhel' in test_os"
- set_fact:
instance_ip: "{{ ip_result.stdout }}"
when: "'rhel' in test_os"
- name: waits until instance is reachable
wait_for:
host: "{{ instance_ip }}"
port: 22
search_regex: OpenSSH
delay: 10
retries: 30
register: result_ssh_check
until: result_ssh_check is success
- name: add instance ip into host group guest
add_host:
name: "{{ instance_ip }}"
groups: guest
- name: Write instance ip to inventory file
community.general.ini_file:
path: "{{ inventory_file }}"
section: guest
option: guest ansible_host
value: "{{ instance_ip }}"
no_extra_spaces: true
- name: Write random number to inventory file
community.general.ini_file:
path: "{{ inventory_file }}"
section: cloud:vars
option: random_num
value: "{{ random_num }}"
no_extra_spaces: true
- name: write AWS EC2 instance id to inventory file
community.general.ini_file:
path: "{{ inventory_file }}"
section: cloud:vars
option: instance_id
value: "{{ instance_json.Instances[0].InstanceId }}"
no_extra_spaces: true
- name: write AWS ami id to inventory file
community.general.ini_file:
path: "{{ inventory_file }}"
section: cloud:vars
option: ami_id
value: "{{ ami_id }}"
no_extra_spaces: true