Skip to content

Commit 3b2c2d1

Browse files
authored
Add new azure_manage_snapshot role (#101)
* Add new azure_manage_snapshot role New role azure_manage_snapshot will help in creating, restoring and deleting snapshots. * Add support for creating vm from snapshot
1 parent 5212df8 commit 3b2c2d1

File tree

26 files changed

+537
-5
lines changed

26 files changed

+537
-5
lines changed

.ansible-lint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
profile: production
1+
profile: shared
22

33
exclude_paths:
44
- tests/integration/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- Added support for swap_os_disk which allows you to create a VM from a Snapshot
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 }}"

roles/azure_virtual_machine_with_public_ip/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Role Variables
2929
- **load_balancer_backend_address_pools**: List of existing load balancer backend address pools in which the network interface will be load balanced.
3030
- **os**: Type of Operating System. Default is 'Linux'
3131
- **image**: The image used to build the VM. For custom images, the name of the image. To narrow the search to a specific resource group, a dict with the keys name and resource_group. For Marketplace images, a dict with the keys publisher, offer, sku, and version. Set version=latest to get the most recent version of a given image.
32+
- **swap_os_disk**: The managed disk to use as the OS disk.
33+
- **os_disk_id**: The swap OS disk's ID.
34+
- **os_disk_name**: The swap OS disk's name.
35+
- **os_disk_resource_group**: The swap OS disk's resource group.
3236
- **managed_disk_type**: Managed OS disk type.
3337
- **ssh_pw_enabled**: Enable/disable SSH passwords. Valid values are 'true', 'false'. When `os='Linux'` and `ssh_pw_enabled='false'` requires the use of SSH keys.
3438
Default value is 'true'.

0 commit comments

Comments
 (0)