forked from ansible/product-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into ansible#107
- Loading branch information
Showing
20 changed files
with
588 additions
and
58 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions
49
collections/ansible_collections/demo/openshift/roles/snapshot/tasks/create.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,49 @@ | ||
--- | ||
- name: Get state of VirtualMachine | ||
redhat.openshift_virtualization.kubevirt_vm_info: | ||
name: "{{ item }}" | ||
namespace: "{{ vm_namespace }}" | ||
register: state | ||
|
||
- name: Stop VirtualMachine | ||
redhat.openshift_virtualization.kubevirt_vm: | ||
name: "{{ item }}" | ||
namespace: "{{ vm_namespace }}" | ||
running: false | ||
wait: true | ||
when: state.resources.0.spec.running | ||
|
||
- name: Create a VirtualMachineSnapshot | ||
kubernetes.core.k8s: | ||
definition: | ||
apiVersion: snapshot.kubevirt.io/v1alpha1 | ||
kind: VirtualMachineSnapshot | ||
metadata: | ||
generateName: "{{ item }}-{{ ansible_date_time.epoch }}" | ||
namespace: "{{ vm_namespace }}" | ||
spec: | ||
source: | ||
apiGroup: kubevirt.io | ||
kind: VirtualMachine | ||
name: "{{ item }}" | ||
wait: true | ||
wait_condition: | ||
type: Ready | ||
register: snapshot | ||
|
||
- name: Start VirtualMachine | ||
redhat.openshift_virtualization.kubevirt_vm: | ||
name: "{{ item }}" | ||
namespace: "{{ vm_namespace }}" | ||
running: true | ||
wait: true | ||
when: state.resources.0.spec.running | ||
|
||
- name: Export snapshot name | ||
ansible.builtin.set_stats: | ||
data: | ||
restore_snapshot_name: "{{ snapshot.result.metadata.name }}" | ||
|
||
- name: Output snapshot name | ||
ansible.builtin.debug: | ||
msg: "Successfully created snapshot {{ snapshot.result.metadata.name }}" |
12 changes: 12 additions & 0 deletions
12
collections/ansible_collections/demo/openshift/roles/snapshot/tasks/main.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,12 @@ | ||
--- | ||
# parameters | ||
# snapshot_opeation: <ceate/restore> | ||
- name: Show hostnames we care about | ||
ansible.builtin.debug: | ||
msg: "About to {{ snapshot_operation }} snapshot(s) for the following hosts: | ||
{{ lookup('ansible.builtin.inventory_hostnames', snapshot_hosts) | split(',') | difference(['localhost'])}}" | ||
|
||
- name: Manage snapshots based on operation | ||
ansible.builtin.include_tasks: | ||
file: "{{ snapshot_operation }}.yml" | ||
loop: "{{ lookup('ansible.builtin.inventory_hostnames', snapshot_hosts) | regex_replace(vm_namespace+'-', '') | split(',') | difference(['localhost']) }}" |
51 changes: 51 additions & 0 deletions
51
collections/ansible_collections/demo/openshift/roles/snapshot/tasks/restore.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,51 @@ | ||
--- | ||
- name: Get state of VirtualMachine | ||
redhat.openshift_virtualization.kubevirt_vm_info: | ||
name: "{{ item }}" | ||
namespace: "{{ vm_namespace }}" | ||
register: state | ||
|
||
- name: List snapshots | ||
kubernetes.core.k8s_info: | ||
api_version: snapshot.kubevirt.io/v1alpha1 | ||
kind: VirtualMachineSnapshot | ||
namespace: "{{ vm_namespace }}" | ||
register: snapshot | ||
|
||
- name: Set snapshot name for {{ item }} | ||
ansible.builtin.set_fact: | ||
latest_snapshot: "{{ snapshot.resources|selectattr('spec.source.name', 'equalto', item)|sort(attribute='metadata.creationTimestamp')|first}}" | ||
|
||
- name: Stop VirtualMachine | ||
redhat.openshift_virtualization.kubevirt_vm: | ||
name: "{{ item }}" | ||
namespace: "{{ vm_namespace }}" | ||
running: false | ||
wait: true | ||
when: state.resources.0.spec.running | ||
|
||
- name: Restore a VirtualMachineSnapshot | ||
kubernetes.core.k8s: | ||
definition: | ||
apiVersion: snapshot.kubevirt.io/v1alpha1 | ||
kind: VirtualMachineRestore | ||
metadata: | ||
generateName: "{{ latest_snapshot.metadata.generateName }}" | ||
namespace: "{{ vm_namespace }}" | ||
spec: | ||
target: | ||
apiGroup: kubevirt.io | ||
kind: VirtualMachine | ||
name: "{{ item }}" | ||
virtualMachineSnapshotName: "{{ latest_snapshot.metadata.name }}" | ||
wait: true | ||
wait_condition: | ||
type: Ready | ||
|
||
- name: Start VirtualMachine | ||
redhat.openshift_virtualization.kubevirt_vm: | ||
name: "{{ item }}" | ||
namespace: "{{ vm_namespace }}" | ||
running: true | ||
wait: true | ||
when: state.resources.0.spec.running |
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
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,9 @@ | ||
--- | ||
- name: Manage CNV snapshots | ||
hosts: localhost | ||
tasks: | ||
- name: Include snapshot role | ||
ansible.builtin.include_role: | ||
name: "demo.openshift.snapshot" | ||
vars: | ||
snapshot_hosts: "{{ _hosts }}" |
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
Oops, something went wrong.