-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
26 changed files
with
537 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
profile: production | ||
profile: shared | ||
|
||
exclude_paths: | ||
- tests/integration/ |
2 changes: 2 additions & 0 deletions
2
changelogs/fragments/20250129-azure_virtual_machine_with_public_ip.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,2 @@ | ||
bugfixes: | ||
- Added support for swap_os_disk which allows you to create a VM from a Snapshot |
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,74 @@ | ||
azure_manage_snapshot | ||
================== | ||
|
||
A role to Create/Delete/Restore an Azure OS Disk Snapshot. | ||
|
||
Requirements | ||
------------ | ||
|
||
* Azure User Account with valid permission | ||
|
||
Role Variables | ||
-------------- | ||
|
||
* **azure_manage_snapshot_operation**: Operation to perform. Valid values are 'create', 'delete', 'restore'. Default is 'create'. | ||
* **azure_manage_snapshot_resource_group**: (Required) Resource group on/from which the snapshot will reside. | ||
* **azure_manage_snapshot_location**: The location to use, if not specified it will use the location of the virtual machine. | ||
* **azure_manage_snapshot_name**: (Required) The name of the snapshot volume. | ||
* **azure_manage_snapshot_vm_name**: The name of the virtual machine where the snapshot came from or will be applied. | ||
* **azure_manage_snapshot_disk_name**: The name of the disk that the snapshot will be restored to. | ||
|
||
Limitations | ||
------------ | ||
|
||
- NA | ||
|
||
Dependencies | ||
------------ | ||
|
||
- NA | ||
|
||
Example Playbook | ||
---------------- | ||
|
||
- hosts: localhost | ||
tasks: | ||
- name: Create a snapshot from a virtual machine | ||
ansible.builtin.include_role: | ||
name: cloud.azure_ops.azure_manage_snapshot | ||
vars: | ||
azure_manage_snapshot_operation: create | ||
azure_manage_snapshot_resource_group: 'resource-group' | ||
azure_manage_snapshot_vm_name: 'example-vm' | ||
azure_manage_snapshot_name: 'example-snapshot-volume' | ||
azure_manage_disk_name: 'example-disk-volume' | ||
|
||
- name: Restore a snapshot to a virtual machine | ||
ansible.builtin.include_role: | ||
name: cloud.azure_ops.azure_manage_snapshot | ||
vars: | ||
azure_manage_snapshot_operation: restore | ||
azure_manage_snapshot_resource_group: 'resource-group' | ||
azure_manage_snapshot_vm_name: 'example-vm' | ||
azure_manage_snapshot_name: 'example-snapshot-volume' | ||
azure_manage_disk_name: 'example-disk-volume' | ||
|
||
- name: Delete a snapshot | ||
ansible.builtin.include_role: | ||
name: cloud.azure_ops.azure_manage_snapshot | ||
vars: | ||
azure_manage_snapshot_operation: delete | ||
azure_manage_snapshot_resource_group: 'resource-group' | ||
azure_manage_snapshot_name: 'example-snapshot-volume' | ||
|
||
License | ||
------- | ||
|
||
GNU General Public License v3.0 or later | ||
|
||
See [LICENCE](https://github.com/redhat-cop/cloud.azure_ops/blob/main/LICENSE) to see the full text. | ||
|
||
Author Information | ||
------------------ | ||
|
||
- Ansible Cloud Content Team |
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,3 @@ | ||
--- | ||
azure_manage_snapshot_operation: create | ||
azure_manage_snapshot_disk_name: "{{ azure_manage_snapshot_name }}-disk" |
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,36 @@ | ||
--- | ||
argument_specs: | ||
main: | ||
version_added: 3.1.0 | ||
short_description: A role to manage a snapshot to an Azure Virtual Machine. | ||
description: | ||
- A role to manage a snapshot to an Azure Virtual Machine. | ||
- This role requires an azure user account with valid permission. | ||
options: | ||
azure_manage_snapshot_operation: | ||
description: | ||
- Operation to perform | ||
default: "create" | ||
choices: ["create", "delete", "restore"] | ||
azure_manage_snapshot_resource_group: | ||
description: | ||
- Resource group from which the snapshot resides. | ||
required: true | ||
azure_manage_snapshot_name: | ||
description: | ||
- Name of the snapshot volume. | ||
required: true | ||
azure_manage_snapshot_vm_name: | ||
description: | ||
- Name of the virtual machine to perform the snapshot operation on. | ||
required: false | ||
azure_manage_snapshot_disk_name: | ||
description: | ||
- Optional name of the Managed Disk. | ||
- Default value is I(azure_manage_snapshot_name). | ||
required: false | ||
azure_manage_snapshot_location: | ||
description: | ||
- Optional location to use. | ||
- Default value is the location of the Virtual Machine or Snapshot. | ||
required: false |
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,24 @@ | ||
--- | ||
- name: Get virtualmachine info | ||
azure.azcollection.azure_rm_virtualmachine_info: | ||
resource_group: "{{ azure_manage_snapshot_resource_group }}" | ||
name: "{{ azure_manage_snapshot_vm_name }}" | ||
register: azure_manage_snapshot_vm_info | ||
|
||
- name: Confirm OS is Managed Disk | ||
ansible.builtin.fail: | ||
msg: "{{ azure_manage_snapshot_vm_name }} OS Disk is not of Managed Type" | ||
when: '"managed_disk" not in azure_manage_snapshot_vm_info.vms[0].os_disk' | ||
|
||
- name: Set OS Disk ID | ||
ansible.builtin.set_fact: | ||
azure_manage_snapshot_os_disk_id: "{{ azure_manage_snapshot_vm_info.vms[0].os_disk.managed_disk.id }}" | ||
|
||
- name: Create a snapshot | ||
azure.azcollection.azure_rm_snapshot: | ||
resource_group: "{{ azure_manage_snapshot_resource_group }}" | ||
name: "{{ azure_manage_snapshot_name }}" | ||
location: "{{ azure_manage_snapshot_location | default(azure_manage_snapshot_vm_info.vms[0].location) }}" | ||
creation_data: | ||
create_option: Copy | ||
source_id: "{{ azure_manage_snapshot_os_disk_id }}" |
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,6 @@ | ||
--- | ||
- name: Delete snapshot | ||
azure.azcollection.azure_rm_snapshot: | ||
resource_group: "{{ azure_manage_snapshot_resource_group }}" | ||
name: "{{ azure_manage_snapshot_name }}" | ||
state: absent |
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,3 @@ | ||
- name: Create, delete or restore Snapshot | ||
ansible.builtin.include_tasks: "{{ azure_manage_snapshot_operation }}.yml" | ||
when: azure_manage_snapshot_operation in ['create', 'delete', 'restore'] |
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,24 @@ | ||
--- | ||
- name: Get snapshot info | ||
azure.azcollection.azure_rm_snapshot_info: | ||
resource_group: "{{ azure_manage_snapshot_resource_group }}" | ||
name: "{{ azure_manage_snapshot_name }}" | ||
register: azure_manage_snapshot_info | ||
|
||
- name: Create Managed disk from Snapshot | ||
azure.azcollection.azure_rm_manageddisk: | ||
name: "{{ azure_manage_snapshot_disk_name | default(azure_manage_snapshot_name) }}" | ||
location: "{{ azure_manage_snapshot_location | default(azure_manage_snapshot_info.state[0].location) }}" | ||
resource_group: "{{ azure_manage_snapshot_resource_group }}" | ||
create_option: copy | ||
source_uri: "{{ azure_manage_snapshot_info.state[0].id }}" | ||
storage_account_type: "{{ azure_manage_snapshot_info.state[0].sku.name }}" | ||
register: azure_manage_snapshot_disk | ||
|
||
- name: Swap OS Disk | ||
azure.azcollection.azure_rm_virtualmachine: | ||
name: "{{ azure_manage_snapshot_vm_name }}" | ||
resource_group: "{{ azure_manage_snapshot_resource_group }}" | ||
swap_os_disk: | ||
os_disk_id: "{{ azure_manage_snapshot_disk.state.id }}" | ||
os_disk_resource_group: "{{ azure_manage_snapshot_resource_group }}" |
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
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,4 @@ | ||
--- | ||
collections: | ||
- community.crypto | ||
- azure.azcollection |
3 changes: 3 additions & 0 deletions
3
tests/integration/targets/azure_ops_test_azure_manage_snapshot/aliases
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,3 @@ | ||
cloud/azure | ||
role/azure_manage_snapshot | ||
time=10m |
19 changes: 19 additions & 0 deletions
19
tests/integration/targets/azure_ops_test_azure_manage_snapshot/defaults/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,19 @@ | ||
--- | ||
vm_name: "{{ resource_prefix }}-vm" | ||
vm_from_snapshot_name: "{{ resource_prefix }}-vm-from-snapshot" | ||
vm_from_snapshot_disk_name: "{{ resource_prefix }}-disk-from-snapshot" | ||
vm_image: | ||
offer: RHEL | ||
publisher: RedHat | ||
sku: 8-LVM | ||
version: latest | ||
vm_username: 'azureuser' | ||
vm_size: Standard_B1s | ||
vm_managed_disk_type: Premium_LRS | ||
snapshot_name: "{{ resource_prefix }}-snapshot" | ||
before_snapshot_file: "/home/{{ vm_username }}/before_snapshot" | ||
after_snapshot_file: "/home/{{ vm_username }}/after_snapshot" | ||
ssh_public_key_path_default: "~/.ssh/id_rsa.pub" | ||
ssh_private_key_path_default: "~/.ssh/id_rsa" | ||
ssh_port_default: 22 | ||
resource_group_name: "{{ resource_prefix }}-{{ resource_group }}" |
19 changes: 19 additions & 0 deletions
19
tests/integration/targets/azure_ops_test_azure_manage_snapshot/tasks/after_snapshot.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,19 @@ | ||
--- | ||
- name: Wait 300 seconds, but only start checking after 10 seconds | ||
ansible.builtin.wait_for_connection: | ||
delay: 10 | ||
timeout: 300 | ||
delegate_to: "{{ groups['azure'][0] }}" | ||
|
||
- name: Touch a file | ||
ansible.builtin.file: | ||
path: "{{ after_snapshot_file }}" | ||
state: touch | ||
mode: '0755' | ||
delegate_to: "{{ groups['azure'][0] }}" | ||
|
||
- name: Sync Filesystems | ||
ansible.builtin.command: sync | ||
register: my_output | ||
changed_when: my_output.rc != 0 | ||
delegate_to: "{{ groups['azure'][0] }}" |
19 changes: 19 additions & 0 deletions
19
tests/integration/targets/azure_ops_test_azure_manage_snapshot/tasks/before_snapshot.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,19 @@ | ||
--- | ||
- name: Wait 300 seconds, but only start checking after 10 seconds | ||
ansible.builtin.wait_for_connection: | ||
delay: 10 | ||
timeout: 300 | ||
delegate_to: "{{ groups['azure'][0] }}" | ||
|
||
- name: Touch a file | ||
ansible.builtin.file: | ||
path: "{{ before_snapshot_file }}" | ||
state: touch | ||
mode: '0755' | ||
delegate_to: "{{ groups['azure'][0] }}" | ||
|
||
- name: Sync Filesystems | ||
ansible.builtin.command: sync | ||
register: my_output | ||
changed_when: my_output.rc != 0 | ||
delegate_to: "{{ groups['azure'][0] }}" |
9 changes: 9 additions & 0 deletions
9
tests/integration/targets/azure_ops_test_azure_manage_snapshot/tasks/create_snapshot.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,9 @@ | ||
--- | ||
- name: Create snapshot | ||
ansible.builtin.include_role: | ||
name: cloud.azure_ops.azure_manage_snapshot | ||
vars: | ||
azure_manage_snapshot_operation: create | ||
azure_manage_snapshot_name: "{{ snapshot_name }}" | ||
azure_manage_snapshot_vm_name: "{{ vm_name }}" | ||
azure_manage_snapshot_resource_group: "{{ resource_group_name }}" |
8 changes: 8 additions & 0 deletions
8
tests/integration/targets/azure_ops_test_azure_manage_snapshot/tasks/delete_snapshot.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,8 @@ | ||
--- | ||
- name: Delete snapshot | ||
ansible.builtin.include_role: | ||
name: cloud.azure_ops.azure_manage_snapshot | ||
vars: | ||
azure_manage_snapshot_operation: delete | ||
azure_manage_snapshot_name: "{{ snapshot_name }}" | ||
azure_manage_snapshot_resource_group: "{{ resource_group_name }}" |
29 changes: 29 additions & 0 deletions
29
tests/integration/targets/azure_ops_test_azure_manage_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,29 @@ | ||
--- | ||
- name: Wrap test in block | ||
block: | ||
- name: Setup | ||
ansible.builtin.include_tasks: setup.yml | ||
|
||
- name: Before snapshot | ||
ansible.builtin.include_tasks: before_snapshot.yml | ||
|
||
- name: Create snapshot | ||
ansible.builtin.include_tasks: create_snapshot.yml | ||
|
||
- name: After snapshot | ||
ansible.builtin.include_tasks: after_snapshot.yml | ||
|
||
- name: Restore snapshot | ||
ansible.builtin.include_tasks: restore_snapshot.yml | ||
|
||
- name: Validate snapshot | ||
ansible.builtin.include_tasks: validate_snapshot.yml | ||
|
||
- name: Create VM from snapshot | ||
ansible.builtin.include_tasks: vm_from_snapshot.yml | ||
|
||
- name: Validate vm from snapshot | ||
ansible.builtin.include_tasks: validate_vm_from_snapshot.yml | ||
always: | ||
- name: Teardown | ||
ansible.builtin.include_tasks: teardown.yml |
9 changes: 9 additions & 0 deletions
9
tests/integration/targets/azure_ops_test_azure_manage_snapshot/tasks/restore_snapshot.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,9 @@ | ||
--- | ||
- name: Restore snapshot | ||
ansible.builtin.include_role: | ||
name: cloud.azure_ops.azure_manage_snapshot | ||
vars: | ||
azure_manage_snapshot_operation: restore | ||
azure_manage_snapshot_name: "{{ snapshot_name }}" | ||
azure_manage_snapshot_vm_name: "{{ vm_name }}" | ||
azure_manage_snapshot_resource_group: "{{ resource_group_name }}" |
Oops, something went wrong.