Skip to content

Commit 49601cf

Browse files
committed
Add new azure_manage_snapshot role
New role azure_manage_snapshot will help in creating, restoring and deleting snapshots.
1 parent 5212df8 commit 49601cf

File tree

19 files changed

+399
-0
lines changed

19 files changed

+399
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
azure_manage_snapshot
2+
==================
3+
4+
A role to Create/Delete/Restore an Azure OS Disk Snapshot.
5+
6+
Requirements
7+
------------
8+
9+
* Azure User Account with valid permission
10+
11+
Role Variables
12+
--------------
13+
14+
* **azure_manage_snapshot_operation**: Operation to perform. Valid values are 'create', 'delete', 'restore'. Default is 'create'.
15+
* **azure_manage_snapshot_resource_group**: (Required) Resource group on/from which the snapshot will reside.
16+
* **azure_manage_snapshot_location**: The location to use, if not specified it will use the location of the virtual machine.
17+
* **azure_manage_snapshot_name**: (Required) The name of the snapshot volume.
18+
* **azure_manage_snapshot_vm_name**: The name of the virtual machine where the snapshot came from or will be applied.
19+
* **azure_manage_snapshot_disk_name**: The name of the disk that the snapshot will be restored to.
20+
21+
Limitations
22+
------------
23+
24+
- NA
25+
26+
Dependencies
27+
------------
28+
29+
- NA
30+
31+
Example Playbook
32+
----------------
33+
34+
- hosts: localhost
35+
tasks:
36+
- name: Create a snapshot from a virtual machine
37+
ansible.builtin.include_role:
38+
name: cloud.azure_ops.azure_manage_snapshot
39+
vars:
40+
azure_manage_snapshot_operation: create
41+
azure_manage_snapshot_resource_group: 'resource-group'
42+
azure_manage_snapshot_vm_name: 'example-vm'
43+
azure_manage_snapshot_name: 'example-snapshot-volume'
44+
azure_manage_disk_name: 'example-disk-volume'
45+
46+
- name: Restore a snapshot to a virtual machine
47+
ansible.builtin.include_role:
48+
name: cloud.azure_ops.azure_manage_snapshot
49+
vars:
50+
azure_manage_snapshot_operation: restore
51+
azure_manage_snapshot_resource_group: 'resource-group'
52+
azure_manage_snapshot_vm_name: 'example-vm'
53+
azure_manage_snapshot_name: 'example-snapshot-volume'
54+
azure_manage_disk_name: 'example-disk-volume'
55+
56+
- name: Delete a snapshot
57+
ansible.builtin.include_role:
58+
name: cloud.azure_ops.azure_manage_snapshot
59+
vars:
60+
azure_manage_snapshot_operation: delete
61+
azure_manage_snapshot_resource_group: 'resource-group'
62+
azure_manage_snapshot_name: 'example-snapshot-volume'
63+
64+
License
65+
-------
66+
67+
GNU General Public License v3.0 or later
68+
69+
See [LICENCE](https://github.com/redhat-cop/cloud.azure_ops/blob/main/LICENSE) to see the full text.
70+
71+
Author Information
72+
------------------
73+
74+
- Ansible Cloud Content Team
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
azure_manage_snapshot_operation: create
3+
azure_manage_snapshot_disk_name: "{{ azure_manage_snapshot_name }}-disk"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
argument_specs:
3+
main:
4+
version_added: 3.1.0
5+
short_description: A role to manage a snapshot to an Azure Virtual Machine.
6+
description:
7+
- A role to manage a snapshot to an Azure Virtual Machine.
8+
- This role requires an azure user account with valid permission.
9+
options:
10+
azure_manage_snapshot_operation:
11+
description:
12+
- Operation to perform
13+
default: "create"
14+
choices: ["create", "delete", "restore"]
15+
azure_manage_snapshot_resource_group:
16+
description:
17+
- Resource group from which the snapshot resides.
18+
required: true
19+
azure_manage_snapshot_name:
20+
description:
21+
- Name of the snapshot volume.
22+
required: true
23+
azure_manage_snapshot_vm_name:
24+
description:
25+
- Name of the virtual machine to perform the snapshot operation on.
26+
required: false
27+
azure_manage_snapshot_disk_name:
28+
description:
29+
- Optional name of the Managed Disk.
30+
- Default value is I(azure_manage_snapshot_name).
31+
required: false
32+
azure_manage_snapshot_location:
33+
description:
34+
- Optional location to use.
35+
- Default value is the location of the Virtual Machine or Snapshot.
36+
required: false
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
- name: Get virtualmachine info
3+
azure.azcollection.azure_rm_virtualmachine_info:
4+
resource_group: "{{ azure_manage_snapshot_resource_group }}"
5+
name: "{{ azure_manage_snapshot_vm_name }}"
6+
register: azure_manage_snapshot_vm_info
7+
8+
- name: Confirm OS is Managed Disk
9+
ansible.builtin.fail:
10+
msg: "{{ azure_manage_snapshot_vm_name }} OS Disk is not of Managed Type"
11+
when: '"managed_disk" not in azure_manage_snapshot_vm_info.vms[0].os_disk'
12+
13+
- name: Set OS Disk ID
14+
ansible.builtin.set_fact:
15+
azure_manage_snapshot_os_disk_id: "{{ azure_manage_snapshot_vm_info.vms[0].os_disk.managed_disk.id }}"
16+
17+
- name: Create a snapshot
18+
azure.azcollection.azure_rm_snapshot:
19+
resource_group: "{{ azure_manage_snapshot_resource_group }}"
20+
name: "{{ azure_manage_snapshot_name }}"
21+
location: "{{ azure_manage_snapshot_location | default(azure_manage_snapshot_vm_info.vms[0].location) }}"
22+
creation_data:
23+
create_option: Copy
24+
source_id: "{{ azure_manage_snapshot_os_disk_id }}"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- name: Delete snapshot
3+
azure.azcollection.azure_rm_snapshot:
4+
resource_group: "{{ azure_manage_snapshot_resource_group }}"
5+
name: "{{ azure_manage_snapshot_name }}"
6+
state: absent
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- name: Create, delete or restore Snapshot
2+
ansible.builtin.include_tasks: "{{ azure_manage_snapshot_operation }}.yml"
3+
when: azure_manage_snapshot_operation in ['create', 'delete', 'restore']
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
- name: Get snapshot info
3+
azure.azcollection.azure_rm_snapshot_info:
4+
resource_group: "{{ azure_manage_snapshot_resource_group }}"
5+
name: "{{ azure_manage_snapshot_name }}"
6+
register: azure_manage_snapshot_info
7+
8+
- name: Create Managed disk from Snapshot
9+
azure.azcollection.azure_rm_manageddisk:
10+
name: "{{ azure_manage_snapshot_disk_name | default(azure_manage_snapshot_name) }}"
11+
location: "{{ azure_manage_snapshot_location | default(azure_manage_snapshot_info.state[0].location) }}"
12+
resource_group: "{{ azure_manage_snapshot_resource_group }}"
13+
create_option: copy
14+
source_uri: "{{ azure_manage_snapshot_info.state[0].id }}"
15+
storage_account_type: "{{ azure_manage_snapshot_info.state[0].sku.name }}"
16+
register: azure_manage_snapshot_disk
17+
18+
- name: Swap OS Disk
19+
azure.azcollection.azure_rm_virtualmachine:
20+
name: "{{ azure_manage_snapshot_vm_name }}"
21+
resource_group: "{{ azure_manage_snapshot_resource_group }}"
22+
swap_os_disk:
23+
os_disk_id: "{{ azure_manage_snapshot_disk.state.id }}"
24+
os_disk_resource_group: "{{ azure_manage_snapshot_resource_group }}"

tests/integration/requirements.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
collections:
3+
- community.crypto
4+
- azure.azcollection
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cloud/azure
2+
role/azure_manage_snapshot
3+
time=10m
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
vm_name: "{{ resource_prefix }}-vm"
3+
vm_image:
4+
offer: RHEL
5+
publisher: RedHat
6+
sku: 8-LVM
7+
version: latest
8+
vm_username: 'azureuser'
9+
vm_size: Standard_B1s
10+
vm_managed_disk_type: Premium_LRS
11+
snapshot_name: "{{ resource_prefix }}-snapshot"
12+
before_snapshot_file: "/home/{{ vm_username }}/before_snapshot"
13+
after_snapshot_file: "/home/{{ vm_username }}/after_snapshot"
14+
ssh_public_key_path_default: "~/.ssh/id_rsa.pub"
15+
ssh_private_key_path_default: "~/.ssh/id_rsa"
16+
ssh_port_default: 22
17+
resource_group_name: "{{ resource_prefix }}-{{ resource_group }}"

0 commit comments

Comments
 (0)