-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: zjleblanc <[email protected]> Co-authored-by: willtome <[email protected]>
- Loading branch information
1 parent
a78e74e
commit 7e4399e
Showing
9 changed files
with
295 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
- name: Restore ec2 instance from snapshot | ||
hosts: "{{ _hosts | default(omit) }}" | ||
gather_facts: false | ||
|
||
tasks: | ||
- name: Include restore from snapshot role | ||
ansible.builtin.include_role: | ||
name: "demo.cloud.aws" | ||
tasks_from: restore_vm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
- name: Snapshot ec2 instance | ||
hosts: "{{ _hosts | default(omit) }}" | ||
gather_facts: false | ||
|
||
tasks: | ||
- name: Include snapshot role | ||
ansible.builtin.include_role: | ||
name: "demo.cloud.aws" | ||
tasks_from: snapshot_vm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
collections/ansible_collections/demo/cloud/roles/aws/tasks/restore_vm.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
- name: AWS | RESTORE VM | ||
delegate_to: localhost | ||
block: | ||
- name: AWS | RESTORE VM | stop vm | ||
amazon.aws.ec2_instance: | ||
region: "{{ aws_region }}" | ||
instance_ids: "{{ instance_id }}" | ||
state: stopped | ||
wait: true | ||
|
||
- name: AWS | RESTORE VM | get volumes | ||
register: r_vol_info | ||
amazon.aws.ec2_vol_info: | ||
region: "{{ aws_region }}" | ||
filters: | ||
attachment.instance-id: "{{ instance_id }}" | ||
|
||
- name: AWS | RESTORE VM | detach volumes | ||
loop: "{{ r_vol_info.volumes }}" | ||
loop_control: | ||
loop_var: volume | ||
label: "{{ volume.id }}" | ||
amazon.aws.ec2_vol: | ||
region: "{{ aws_region }}" | ||
id: "{{ volume.id }}" | ||
instance: None | ||
|
||
- name: AWS | RESTORE VM | attach snapshots from stat | ||
when: inventory_hostname in aws_snapshots | ||
loop: "{{ aws_snapshots[inventory_hostname] }}" | ||
loop_control: | ||
loop_var: snap | ||
label: "{{ snap.snapshot_id }}" | ||
amazon.aws.ec2_vol: | ||
region: "{{ aws_region }}" | ||
instance: "{{ instance_id }}" | ||
snapshot: "{{ snap.snapshot_id }}" | ||
device_name: "{{ snap.device }}" | ||
|
||
- name: AWS | RESTORE VM | get all snapshots | ||
when: inventory_hostname not in aws_snapshots | ||
register: r_snapshots | ||
amazon.aws.ec2_snapshot_info: | ||
region: "{{ aws_region }}" | ||
filters: | ||
"tag:Name": "{{ inventory_hostname }}" | ||
|
||
- name: AWS | RESTORE VM | create volume from latest snapshot | ||
when: inventory_hostname not in aws_snapshots | ||
amazon.aws.ec2_vol: | ||
region: "{{ aws_region }}" | ||
instance: "{{ instance_id }}" | ||
snapshot: "{{ r_snapshots.snapshots[0].snapshot_id }}" | ||
device_name: "/dev/sda1" | ||
|
||
- name: AWS | RESTORE VM | start vm | ||
amazon.aws.ec2_instance: | ||
region: "{{ aws_region }}" | ||
instance_ids: "{{ instance_id }}" | ||
state: started | ||
wait: true | ||
43 changes: 43 additions & 0 deletions
43
collections/ansible_collections/demo/cloud/roles/aws/tasks/snapshot_vm.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
- name: AWS | SNAPSHOT VM | ||
delegate_to: localhost | ||
block: | ||
- name: AWS | SNAPSHOT VM | assert id | ||
ansible.builtin.assert: | ||
that: instance_id is defined | ||
fail_msg: "instance_id is required for snapshot operations" | ||
|
||
- name: AWS | SNAPSHOT VM | include vars | ||
ansible.builtin.include_vars: | ||
file: snapshot_vm.yml | ||
|
||
- name: AWS | SNAPSHOT VM | get volumes | ||
register: r_vol_info | ||
amazon.aws.ec2_vol_info: | ||
region: "{{ aws_region }}" | ||
filters: | ||
attachment.instance-id: "{{ instance_id }}" | ||
|
||
- name: AWS | SNAPSHOT VM | take snapshots | ||
loop: "{{ r_vol_info.volumes }}" | ||
loop_control: | ||
loop_var: volume | ||
label: "{{ volume.id }}" | ||
register: r_snapshots | ||
amazon.aws.ec2_snapshot: | ||
region: "{{ aws_region }}" | ||
volume_id: "{{ volume.id }}" | ||
description: "Snapshot taken by Red Hat Product demos" | ||
snapshot_tags: "{{ tags }}" | ||
|
||
- name: AWS | SNAPSHOT VM | format snapshot stat | ||
ansible.builtin.set_fact: | ||
snapshot_stat: | ||
- key: "{{ inventory_hostname }}" | ||
value: "{{ r_snapshots.results | json_query(aws_ec2_snapshot_query) }}" | ||
|
||
- name: AWS | SNAPSHOT VM | record snapshot with host key | ||
ansible.builtin.set_stats: | ||
data: | ||
aws_snapshots: "{{ snapshot_stat | items2dict }}" | ||
|
||
10 changes: 10 additions & 0 deletions
10
collections/ansible_collections/demo/cloud/roles/aws/vars/snapshot_vm.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Set stat_snapshots with model: | ||
# [ | ||
# { | ||
# "snapshot_id": "snap-0e981f05704e19ffd", | ||
# "vol_id": "vol-0bd55f313bb7bcdd8", | ||
# "device": "/dev/sda1" | ||
# }, | ||
# ... | ||
# ] | ||
aws_ec2_snapshot_query: "[].{snapshot_id: snapshot_id, vol_id: volume.id, device: volume.attachment_set[?instance_id=='{{ instance_id }}'].device | [0]}" | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters