Skip to content

Commit 5adf5b9

Browse files
committed
Allow user to restore controllers from snapshots
1 parent 2ca95af commit 5adf5b9

File tree

12 files changed

+145
-25
lines changed

12 files changed

+145
-25
lines changed

roles/aws_controllers/tasks/aws_vbond_ec2_instance.yml

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@
1010
# 2 aws_eip
1111
# 1 ec2 instance
1212

13+
- name: AMI Registration from EBS Snapshot
14+
amazon.aws.ec2_ami:
15+
name: "{{ aws_tag_creator }}-Validator"
16+
region: "{{ aws_region }}"
17+
state: present
18+
architecture: x86_64
19+
virtualization_type: hvm
20+
root_device_name: /dev/xvda
21+
device_mapping:
22+
- device_name: /dev/xvda
23+
snapshot_id: "{{ volume_snapshots[0] }}"
24+
delete_on_termination: true
25+
volume_type: gp2
26+
tags:
27+
Name: "{{ aws_tag_creator }}-Validator"
28+
Creator: "{{ aws_tag_creator }}"
29+
when: volume_snapshots
30+
register: ami_result
31+
1332

1433
# NICs
1534
- name: Filter required subnets for instance creation. Set aws_mgmt_subnet and aws_transport_subnet facts
@@ -95,7 +114,7 @@
95114
count: 1
96115
instance_type: "{{ aws_vbond_instance_type }}"
97116
image:
98-
id: "{{ aws_vbond_ami_id }}"
117+
id: "{{ ami_result.image_id if volume_snapshots else aws_vmanage_ami_id }}"
99118
state: present
100119
vpc_subnet_id: "{{ aws_mgmt_subnet.id }}"
101120
region: "{{ aws_region }}"
@@ -121,6 +140,14 @@
121140
delete_on_termination: true
122141
register: ec2_vbond
123142

143+
- name: Deregister/Delete AMI (keep associated snapshots)
144+
amazon.aws.ec2_ami:
145+
image_id: "{{ ami_result.image_id }}"
146+
delete_snapshot: false
147+
state: absent
148+
region: "{{ aws_region }}"
149+
when: volume_snapshots
150+
124151
- name: Store vBond instance details for deployment_results
125152
ansible.builtin.set_fact:
126153
instance:
@@ -163,15 +190,17 @@
163190
purge_rules: false
164191
purge_tags: false
165192
purge_rules_egress: false
166-
rules:
193+
rules: "{{ sg_rules_vbond }}"
194+
rules_egress: "{{ sg_rules_vbond if aws_sg_block_egress else [] }}"
195+
register: allow_traffic
196+
retries: 3
197+
delay: 3
198+
until: allow_traffic is succeeded
199+
vars:
200+
sg_rules_vbond:
167201
- proto: all
168202
cidr_ip: "{{ eip_vbond.results[0].public_ip }}/32"
169203
rule_desc: "{{ hostname }} - mgmt (VPN 512)"
170204
- proto: all
171205
cidr_ip: "{{ eip_vbond.results[1].public_ip }}/32"
172206
rule_desc: "{{ hostname }} - transport (VPN 0)"
173-
rules_egress: []
174-
register: allow_traffic
175-
retries: 3
176-
delay: 3
177-
until: allow_traffic is succeeded

roles/aws_controllers/tasks/aws_vmanage_ec2_instance.yml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99
# 2 aws_eip
1010
# 1 ec2 instance
1111

12+
- name: AMI Registration from EBS Snapshot
13+
amazon.aws.ec2_ami:
14+
name: "{{ aws_tag_creator }}-Manager"
15+
region: "{{ aws_region }}"
16+
state: present
17+
architecture: x86_64
18+
virtualization_type: hvm
19+
root_device_name: /dev/xvda
20+
device_mapping:
21+
- device_name: /dev/xvda
22+
snapshot_id: "{{ volume_snapshots[0] }}"
23+
delete_on_termination: true
24+
volume_type: gp2
25+
tags:
26+
Name: "{{ aws_tag_creator }}-Manager"
27+
Creator: "{{ aws_tag_creator }}"
28+
when: volume_snapshots
29+
register: ami_result
1230

1331
# NICs
1432
- name: Filter required subnets for instance creation. Set aws_mgmt_subnet and aws_transport_subnet facts
@@ -115,7 +133,7 @@
115133
count: 1
116134
instance_type: "{{ aws_vmanage_instance_type }}"
117135
image:
118-
id: "{{ aws_vmanage_ami_id }}"
136+
id: "{{ ami_result.image_id if volume_snapshots else aws_vmanage_ami_id }}"
119137
state: present
120138
vpc_subnet_id: "{{ aws_mgmt_subnet.id }}"
121139
region: "{{ aws_region }}"
@@ -137,8 +155,17 @@
137155
ebs:
138156
volume_size: 60
139157
delete_on_termination: true
158+
snapshot_id: "{{ volume_snapshots[1] if volume_snapshots else omit }}"
140159
register: ec2_vmanage
141160

161+
- name: Deregister/Delete AMI (keep associated snapshots)
162+
amazon.aws.ec2_ami:
163+
image_id: "{{ ami_result.image_id }}"
164+
delete_snapshot: false
165+
state: absent
166+
region: "{{ aws_region }}"
167+
when: volume_snapshots
168+
142169
- name: Store vManage instance details for deployment_results
143170
ansible.builtin.set_fact:
144171
instance:
@@ -199,7 +226,7 @@
199226
purge_tags: false
200227
purge_rules_egress: false
201228
rules: "{{ sg_rules }}"
202-
rules_egress: []
229+
rules_egress: "{{ sg_rules if aws_sg_block_egress else [] }}"
203230
register: allow_traffic
204231
retries: 3
205232
delay: 3

roles/aws_controllers/tasks/aws_vsmart_ec2_instance.yml

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@
1010
# 2 aws_eip
1111
# 1 ec2 instance
1212

13+
- name: AMI Registration from EBS Snapshot
14+
amazon.aws.ec2_ami:
15+
name: "{{ aws_tag_creator }}-Controller"
16+
region: "{{ aws_region }}"
17+
state: present
18+
architecture: x86_64
19+
virtualization_type: hvm
20+
root_device_name: /dev/xvda
21+
device_mapping:
22+
- device_name: /dev/xvda
23+
snapshot_id: "{{ volume_snapshots[0] }}"
24+
delete_on_termination: true
25+
volume_type: gp2
26+
tags:
27+
Name: "{{ aws_tag_creator }}-Controller"
28+
Creator: "{{ aws_tag_creator }}"
29+
when: volume_snapshots
30+
register: ami_result
31+
1332

1433
# NICs
1534
- name: Filter required subnets for instance creation. Set aws_mgmt_subnet and aws_transport_subnet facts
@@ -91,7 +110,7 @@
91110
count: 1
92111
instance_type: "{{ aws_vsmart_instance_type }}"
93112
image:
94-
id: "{{ aws_vsmart_ami_id }}"
113+
id: "{{ ami_result.image_id if volume_snapshots else aws_vmanage_ami_id }}"
95114
state: present
96115
vpc_subnet_id: "{{ aws_mgmt_subnet.id }}"
97116
region: "{{ aws_region }}"
@@ -117,6 +136,14 @@
117136
delete_on_termination: true
118137
register: ec2_vsmart
119138

139+
- name: Deregister/Delete AMI (keep associated snapshots)
140+
amazon.aws.ec2_ami:
141+
image_id: "{{ ami_result.image_id }}"
142+
delete_snapshot: false
143+
state: absent
144+
region: "{{ aws_region }}"
145+
when: volume_snapshots
146+
120147
# TODO:
121148
# Note that the variable: ec2_vsmart.instances[0].network_interfaces is returning a list of interfaces
122149
# but that list can be different than device_index (so mgmt and transport are mixed)
@@ -166,15 +193,17 @@
166193
purge_rules: false
167194
purge_tags: false
168195
purge_rules_egress: false
169-
rules:
196+
rules: "{{ sg_rules_vsmart }}"
197+
rules_egress: "{{ sg_rules_vsmart if aws_sg_block_egress else [] }}"
198+
register: allow_traffic
199+
retries: 3
200+
delay: 3
201+
until: allow_traffic is succeeded
202+
vars:
203+
sg_rules_vsmart:
170204
- proto: all
171205
cidr_ip: "{{ eip_vsmart.results[0].public_ip }}/32"
172206
rule_desc: "{{ hostname }} - mgmt (VPN 512)"
173207
- proto: all
174208
cidr_ip: "{{ eip_vsmart.results[1].public_ip }}/32"
175209
rule_desc: "{{ hostname }} - transport (VPN 0)"
176-
rules_egress: []
177-
register: allow_traffic
178-
retries: 3
179-
delay: 3
180-
until: allow_traffic is succeeded

roles/aws_controllers/tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
hostname: "{{ instance_item.hostname }}"
7373
system_ip: "{{ instance_item.system_ip }}"
7474
site_id: "{{ instance_item.site_id }}"
75+
volume_snapshots: "{{ instance_item.volume_snapshots | default([]) }}"
7576
loop: "{{ vbond_instances }}"
7677
loop_control:
7778
loop_var: instance_item
@@ -84,6 +85,7 @@
8485
system_ip: "{{ instance_item.system_ip }}"
8586
site_id: "{{ instance_item.site_id }}"
8687
persona: "{{ instance_item.persona }}"
88+
volume_snapshots: "{{ instance_item.volume_snapshots | default([]) }}"
8789
loop: "{{ vmanage_instances }}"
8890
loop_control:
8991
loop_var: instance_item
@@ -95,6 +97,7 @@
9597
hostname: "{{ instance_item.hostname }}"
9698
system_ip: "{{ instance_item.system_ip }}"
9799
site_id: "{{ instance_item.site_id }}"
100+
volume_snapshots: "{{ instance_item.volume_snapshots | default([]) }}"
98101
loop: "{{ vsmart_instances }}"
99102
loop_control:
100103
loop_var: instance_item

roles/aws_network_infrastructure/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
# VPN subnets from which we can connect to AWS EIPs (Security Group config)
77
aws_allowed_subnets: null
8+
aws_sg_block_egress: false
89

910
#####################################
1011
# General AWS configuration #

roles/aws_network_infrastructure/tasks/aws_create_network_infrastructure.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,20 @@
156156
from_port: 8
157157
to_port: -1
158158
cidr_ip: "{{ aws_allowed_subnets }}"
159-
rules_egress:
160-
- proto: -1
161-
from_port: 0
162-
to_port: 0
163-
cidr_ip: 0.0.0.0/0
159+
rules_egress: "{{ egress_allow_all if not aws_sg_block_egress else [] }}"
164160
purge_rules: false
165161
purge_tags: false
166-
purge_rules_egress: false
162+
purge_rules_egress: "{{ aws_sg_block_egress and _created_vpc.changed }}"
167163
tags:
168164
Name: "{{ aws_security_group_name }}"
169165
Creator: "{{ aws_tag_creator }}"
170166
register: _created_security_group
167+
vars:
168+
egress_allow_all:
169+
- proto: -1
170+
from_port: 0
171+
to_port: 0
172+
cidr_ip: 0.0.0.0/0
171173

172174
- name: Copy Security Group resources information to log file
173175
ansible.builtin.blockinfile:

roles/azure_controllers/tasks/azure_vbond_vm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
location: "{{ az_location }}"
137137
os_type: "Linux"
138138
hyper_v_generation: "V1"
139-
source: "{{ az_vbond_image_vhd_source }}"
139+
source: "{{ volume_snapshots[0] if volume_snapshots else az_vbond_image_vhd_source }}"
140140

141141
- name: "Create vBond VM: {{ hostname }}"
142142
azure.azcollection.azure_rm_virtualmachine:

roles/azure_controllers/tasks/azure_vmanage_vm.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,17 @@
212212
location: "{{ az_location }}"
213213
os_type: "Linux"
214214
hyper_v_generation: "V1"
215-
source: "{{ az_vmanage_image_vhd_source }}"
215+
source: "{{ volume_snapshots[0] if volume_snapshots else az_vmanage_image_vhd_source }}"
216+
217+
- name: "Create an image from a VHD for vManage: {{ hostname }}-image"
218+
azure.azcollection.azure_rm_manageddisk:
219+
resource_group: "{{ az_resource_group }}"
220+
name: "{{ hostname }}-datadisk1"
221+
location: "{{ az_location }}"
222+
source_uri: "{{ volume_snapshots[1] }}"
223+
create_option: copy
224+
when: volume_snapshots
225+
register: datadisk_info
216226

217227
- name: "Create VM for vmanage: {{ hostname }}"
218228
azure.azcollection.azure_rm_virtualmachine:
@@ -239,6 +249,7 @@
239249
disk_size_gb: 100
240250
managed_disk_type: Premium_LRS
241251
storage_container_name: "{{ hostname }}-datadisk1"
252+
managed_disk_id: "{{ datadisk_info.state.id if volume_snapshots else omit }}"
242253
tags:
243254
Name: "{{ hostname }}"
244255
Creator: "{{ az_tag_creator }}"

roles/azure_controllers/tasks/azure_vsmart_vm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
location: "{{ az_location }}"
127127
os_type: "Linux"
128128
hyper_v_generation: "V1"
129-
source: "{{ az_vsmart_image_vhd_source }}"
129+
source: "{{ volume_snapshots[0] if volume_snapshots else az_vsmart_image_vhd_source }}"
130130

131131
- name: "Create vSmart VM: {{ hostname }}"
132132
azure.azcollection.azure_rm_virtualmachine:

roles/azure_controllers/tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
hostname: "{{ instance_item.hostname }}"
5454
system_ip: "{{ instance_item.system_ip }}"
5555
site_id: "{{ instance_item.site_id }}"
56+
volume_snapshots: "{{ instance_item.volume_snapshots | default([]) }}"
5657
loop: "{{ vbond_instances }}"
5758
loop_control:
5859
loop_var: instance_item
@@ -101,6 +102,7 @@
101102
hostname: "{{ instance_item.hostname }}"
102103
system_ip: "{{ instance_item.system_ip }}"
103104
site_id: "{{ instance_item.site_id }}"
105+
volume_snapshots: "{{ instance_item.volume_snapshots | default([]) }}"
104106
loop: "{{ vsmart_instances }}"
105107
loop_control:
106108
loop_var: instance_item
@@ -113,6 +115,7 @@
113115
system_ip: "{{ instance_item.system_ip }}"
114116
site_id: "{{ instance_item.site_id }}"
115117
persona: "{{ instance_item.persona }}"
118+
volume_snapshots: "{{ instance_item.volume_snapshots | default([]) }}"
116119
loop: "{{ vmanage_instances }}"
117120
loop_control:
118121
loop_var: instance_item

0 commit comments

Comments
 (0)