support creating an image from a volume (#59574)

* support creating an image from a volume

* leave filename/volume optional

* enforce volume/filename mutual exclusivity

* bump version_added to 2.10 for volume option

* add changelog fragment
This commit is contained in:
bverschueren 2019-09-27 14:29:34 +02:00 committed by Felix Fontein
parent 3c8abc0b82
commit c3eceb3a0d
2 changed files with 41 additions and 1 deletions

View file

@ -0,0 +1,3 @@
---
minor_changes:
- os_image - add ``volume`` option to support creating an image from a volume

View file

@ -86,6 +86,11 @@ options:
availability_zone:
description:
- Ignored. Present for backwards compatibility
volume:
version_added: "2.10"
description:
- ID of a volume to create an image from.
- The volume must be in AVAILABLE state.
requirements: ["openstacksdk"]
'''
@ -109,6 +114,33 @@ EXAMPLES = '''
properties:
cpu_arch: x86_64
distro: ubuntu
# Create image from volume attached to an instance
- name: create volume snapshot
os_volume_snapshot:
auth:
"{{ auth }}"
display_name: myvol_snapshot
volume: myvol
force: yes
register: myvol_snapshot
- name: create volume from snapshot
os_volume:
auth:
"{{ auth }}"
size: "{{ myvol_snapshot.snapshot.size }}"
snapshot_id: "{{ myvol_snapshot.snapshot.id }}"
display_name: myvol_snapshot_volume
wait: yes
register: myvol_snapshot_volume
- name: create image from volume snapshot
os_image:
auth:
"{{ auth }}"
volume: "{{ myvol_snapshot_volume.volume.id }}"
name: myvol_image
'''
from ansible.module_utils.basic import AnsibleModule
@ -132,9 +164,13 @@ def main():
ramdisk=dict(default=None),
kernel=dict(default=None),
properties=dict(type='dict', default={}),
volume=dict(default=None),
state=dict(default='present', choices=['absent', 'present']),
)
module_kwargs = openstack_module_kwargs()
module_kwargs = openstack_module_kwargs(
mutually_exclusive=[['filename', 'volume']],
)
module = AnsibleModule(argument_spec, **module_kwargs)
sdk, cloud = openstack_cloud_from_module(module)
@ -162,6 +198,7 @@ def main():
protected=module.params['protected'],
min_disk=module.params['min_disk'],
min_ram=module.params['min_ram'],
volume=module.params['volume'],
**kwargs
)
changed = True