Skip to content

Commit 2ea87b9

Browse files
committed
Add new azure_manage_snapshot role
New role azure_manage_snapshot will help in creating, restoring and deleting snapshots.
1 parent 1572d52 commit 2ea87b9

File tree

20 files changed

+426
-0
lines changed

20 files changed

+426
-0
lines changed

galaxy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tags:
1717
- infrastructure
1818
dependencies:
1919
azure.azcollection: '*'
20+
community.crypto: '2.9.0'
2021
build_ignore:
2122
- '.tox'
2223
- '.git'
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
azure_manage_snapshot_operation: create
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 to restore.
22+
required: true
23+
azure_manage_snapshot_vm_name:
24+
description:
25+
- Name of the virtual machine to restore the snapshot to.
26+
required: true
27+
azure_manage_snapshot_disk_name:
28+
description:
29+
- Optional name of the Managed Disk, Defaults to I(azure_manage_snapshot_name).
30+
required: false
31+
azure_manage_snapshot_location:
32+
description:
33+
- Optional location to use, Defaults to location of the Virtual Machine or Snapshot.
34+
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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_name }}-disk"
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_name: "{{ azure_manage_snapshot_disk_name | default(azure_manage_snapshot_name) }}"
25+
os_disk_resource_group: "{{ azure_manage_snapshot_resource_group }}"
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
- name: Setup virtual host
3+
hosts: localhost
4+
connection: local
5+
gather_facts: false
6+
tasks:
7+
- name: Include default variables
8+
ansible.builtin.include_vars:
9+
file: ../vars/vars.yml
10+
- name: Setup
11+
ansible.builtin.include_tasks: ../tasks/setup.yml
12+
13+
- name: Before snapshot
14+
hosts: azure
15+
gather_facts: false
16+
tasks:
17+
- name: Include default variables
18+
ansible.builtin.include_vars:
19+
file: ../vars/vars.yml
20+
- name: Before snapshot
21+
ansible.builtin.include_tasks: ../tasks/before_snapshot.yml
22+
23+
- name: Create snapshot
24+
hosts: localhost
25+
connection: local
26+
gather_facts: false
27+
tasks:
28+
- name: Create snapshot
29+
ansible.builtin.include_tasks: ../tasks/create_snapshot.yml
30+
31+
- name: After snapshot
32+
hosts: azure
33+
gather_facts: false
34+
tasks:
35+
- name: After snapshot
36+
ansible.builtin.include_tasks: ../tasks/after_snapshot.yml
37+
38+
- name: Restore snapshot
39+
hosts: localhost
40+
connection: local
41+
gather_facts: false
42+
tasks:
43+
- name: Restore snapshot
44+
ansible.builtin.include_tasks: ../tasks/restore_snapshot.yml
45+
46+
- name: Validate snapshot
47+
hosts: azure
48+
gather_facts: false
49+
tasks:
50+
- name: Validate snapshot
51+
ansible.builtin.include_tasks: ../tasks/validate_snapshot.yml
52+
53+
- name: Teardown
54+
hosts: localhost
55+
connection: local
56+
gather_facts: false
57+
tasks:
58+
- name: Teardown
59+
ansible.builtin.include_tasks: ../tasks/teardown.yml

0 commit comments

Comments
 (0)