Refactored image{,_info} modules

Code of the image module has been refactored partially only.
It will have to be completed in a follow up patch.

Change-Id: Id2360163117db9639a0af502ab44e02cae5cebaa
This commit is contained in:
Jakob Meng
2023-01-13 14:29:17 +01:00
parent 1f8c9fee05
commit cc9ca5a34b
6 changed files with 737 additions and 978 deletions

View File

@@ -1,199 +1,211 @@
--- ---
- name: Ensure image does not exist before tests - name: Test images
openstack.cloud.image: block:
- name: List all images
openstack.cloud.image_info:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: absent register: images
name: ansible_image
- name: Create a test image file - name: Assert existence of CirrOS image
shell: mktemp assert:
that:
- images.images | length > 0
- name: Ensure clean environment
ansible.builtin.set_fact:
tmp_file: !!null
- name: Create a test image file
ansible.builtin.tempfile:
register: tmp_file register: tmp_file
- name: Fill test image file to 1MB - name: Fill test image file to 1MB
shell: truncate -s 1048576 {{ tmp_file.stdout }} community.general.filesize:
path: '{{ tmp_file.path }}'
size: 1M
- name: ensure mock kernel and ramdisk images (defaults) - name: Ensure mock kernel and ramdisk images (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: "{{ item }}" name: "{{ item }}"
filename: "{{ tmp_file.stdout }}" filename: "{{ tmp_file.path }}"
disk_format: raw disk_format: raw
loop: loop:
- cirros-vmlinuz - cirros-vmlinuz
- cirros-initrd - cirros-initrd
- name: Create raw image (defaults) - name: Create raw image (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: ansible_image name: ansible_image
filename: "{{ tmp_file.stdout }}" filename: "{{ tmp_file.path }}"
is_protected: true is_protected: true
disk_format: raw disk_format: raw
tags: tags:
- test - test
- ansible - ansible
register: returned_image register: image
- debug: - name: Assert changed
var: returned_image
- name: Assert changed
assert:
that: returned_image is changed
- name: Assert fields
assert: assert:
that: that:
- item in returned_image.image - image is changed
loop: "{{ expected_fields }}"
- name: Get details of created image - name: Assert return values of image module
assert:
that:
- image is changed
- image.image.name == 'ansible_image'
# allow new fields to be introduced but prevent fields from being removed
- expected_fields|difference(image.image.keys())|length == 0
- name: Get details of created image
openstack.cloud.image_info: openstack.cloud.image_info:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
image: ansible_image image: ansible_image
register: image_info_result register: images
- name: Verify image info - name: Assert return values of image_info module
assert: assert:
that: that:
- image_info_result.images[0].name == "ansible_image" - images.images | length > 0
- image_info_result.images[0].tags | sort == ['test', 'ansible'] | sort - images.images.0.name == "ansible_image"
- images.images.0.tags | sort == ['test', 'ansible'] | sort
# allow new fields to be introduced but prevent fields from being removed
- expected_fields|difference(images.images.0.keys())|length == 0
- name: Create raw image again (defaults) - name: Create raw image again (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: ansible_image name: ansible_image
filename: "{{ tmp_file.stdout }}" filename: "{{ tmp_file.path }}"
is_protected: true is_protected: true
disk_format: raw disk_format: raw
tags: tags:
- test - test
- ansible - ansible
register: returned_image register: image
- name: Assert not changed - name: Assert not changed
assert:
that: returned_image is not changed
- name: Assert fields
assert: assert:
that: that:
- item in returned_image.image - image is not changed
loop: "{{ expected_fields }}"
- name: Update is_protected on raw image (defaults) - name: Update is_protected on raw image (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: ansible_image name: ansible_image
is_protected: false is_protected: false
register: returned_image register: image
- name: Assert changed - name: Assert changed
assert: assert:
that: that:
- returned_image is changed - image is changed
- returned_image.image.is_protected == false - image.image.is_protected == false
- name: Assert changed - name: Assert changed
assert: assert:
that: that:
- returned_image is changed - image is changed
- name: Update visibility on raw image (defaults) - name: Update visibility on raw image (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: ansible_image name: ansible_image
is_public: false is_public: false
register: returned_image register: image
- name: Assert changed - name: Assert changed
assert: assert:
that: that:
- returned_image.image.visibility == 'private' - image.image.visibility == 'private'
- name: Update again visibility on raw image (defaults) - name: Update again visibility on raw image (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: ansible_image name: ansible_image
is_public: true is_public: true
register: returned_image register: image
- name: Assert changed - name: Assert changed
assert: assert:
that: that:
- returned_image is changed - image is changed
- returned_image.image.visibility == 'public' - image.image.visibility == 'public'
- name: Define visibility on raw image (defaults) - name: Define visibility on raw image (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: ansible_image name: ansible_image
visibility: shared visibility: shared
register: returned_image register: image
- name: Assert changed - name: Assert changed
assert: assert:
that: that:
- returned_image is changed - image is changed
- returned_image.image.visibility == 'shared' - image.image.visibility == 'shared'
- name: Rename raw image (defaults) - name: Rename raw image (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
id: "{{ returned_image.id }}" id: "{{ image.image.id }}"
name: 'ansible_image-changed' name: 'ansible_image-changed'
register: returned_image register: image
- name: Assert changed - name: Assert changed
assert: assert:
that: that:
- returned_image is changed - image is changed
- returned_image.image.name == 'ansible_image-changed' - image.image.name == 'ansible_image-changed'
- name: Rename back raw image (defaults) - name: Rename back raw image (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
id: "{{ returned_image.id }}" id: "{{ image.image.id }}"
name: ansible_image name: ansible_image
register: returned_image register: image
- name: Delete raw image (defaults) - name: Delete raw image (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: absent state: absent
name: ansible_image name: ansible_image
register: returned_image register: image
- name: assert image changed - name: assert image changed
assert: assert:
that: returned_image is changed that:
- image is changed
- name: Delete raw image again (defaults) - name: Delete raw image again (defaults)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: absent state: absent
name: ansible_image name: ansible_image
register: returned_image register: image
- name: assert image not changed - name: assert image not changed
assert: assert:
that: returned_image is not changed that:
- image is not changed
- name: Create raw image (complex) - name: Create raw image (complex)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: ansible_image name: ansible_image
filename: "{{ tmp_file.stdout }}" filename: "{{ tmp_file.path }}"
disk_format: raw disk_format: raw
is_public: True is_public: True
min_disk: 10 min_disk: 10
@@ -204,114 +216,116 @@
properties: properties:
cpu_arch: x86_64 cpu_arch: x86_64
distro: ubuntu distro: ubuntu
register: returned_image register: image
- name: Assert visibility - name: Assert visibility
assert:
that: returned_image.image.visibility == 'public'
- name: Assert fields
assert: assert:
that: that:
- item in returned_image.image - image.image.visibility == 'public'
loop: "{{ expected_fields }}"
- name: Delete raw image (complex) - name: Delete raw image (complex)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: absent state: absent
name: ansible_image name: ansible_image
- name: Try to get details of deleted image - name: Try to get details of deleted image
openstack.cloud.image_info: openstack.cloud.image_info:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
image: ansible_image image: ansible_image
register: deleted_image_info_result register: images
- name: Verify image is deleted - name: Verify image is deleted
assert: assert:
that: that:
- not deleted_image_info_result.images - images.images | length == 0
- name: Create owner project - name: Create owner project
openstack.cloud.project: openstack.cloud.project:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: image_owner_project name: ansible_project
description: Project owning test image description: Project owning test image
domain: default domain: default
is_enabled: True is_enabled: True
register: owner_project register: project
- name: Create raw image (owner by project name) - name: Create raw image (owner by project name)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: ansible_image name: ansible_image
filename: "{{ tmp_file.stdout }}" filename: "{{ tmp_file.path }}"
disk_format: raw disk_format: raw
tags: tags:
- test - test
- ansible - ansible
project: image_owner_project project: ansible_project
register: returned_image register: image
- name: Get details of created image (owner by project name) - name: Get details of created image (owner by project name)
openstack.cloud.image_info: openstack.cloud.image_info:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
image: ansible_image image: ansible_image
register: image_info_result register: images
- name: Verify image owner (owner by project name) - name: Verify image owner (owner by project name)
assert: assert:
that: that:
- image_info_result.images[0].owner == owner_project.project.id - images.images.0.owner == project.project.id
- name: Delete raw image (owner by project name) - name: Delete raw image (owner by project name)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: absent state: absent
name: ansible_image name: ansible_image
- name: Create raw image (owner by project name and domain name) - name: Create raw image (owner by project name and domain name)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: present state: present
name: ansible_image name: ansible_image
filename: "{{ tmp_file.stdout }}" filename: "{{ tmp_file.path }}"
disk_format: raw disk_format: raw
tags: tags:
- test - test
- ansible - ansible
project: image_owner_project project: ansible_project
project_domain: default project_domain: default
register: returned_image register: image
- name: Get details of created image (owner by project name and domain name) - name: Get details of created image (owner by project name and domain name)
openstack.cloud.image_info: openstack.cloud.image_info:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
image: ansible_image image: ansible_image
register: image_info_result register: images
- name: Verify image owner (owner by project name and domain name) - name: Verify image owner (owner by project name and domain name)
assert: assert:
that: that:
- image_info_result.images[0].owner == owner_project.project.id - images.images.0.owner == project.project.id
- name: Delete raw image (owner by project name and domain name) - name: Delete raw image (owner by project name and domain name)
openstack.cloud.image: openstack.cloud.image:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: absent state: absent
name: ansible_image name: ansible_image
- name: Delete owner project - name: Delete owner project
openstack.cloud.project: openstack.cloud.project:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
state: absent state: absent
name: image_owner_project name: ansible_project
domain: default domain: default
- name: Delete test image file - name: Delete test image file
file: file:
name: "{{ tmp_file.stdout }}" name: "{{ tmp_file.path }}"
state: absent state: absent
always:
- name: Remove temporary image file
ansible.builtin.file:
path: "{{ tmp_file.path }}"
state: absent
when: tmp_file is defined and 'path' in tmp_file

View File

@@ -1,65 +0,0 @@
expected_fields:
- architecture
- checksum
- container_format
- created_at
- direct_url
- disk_format
- file
- has_auto_disk_config
- hash_algo
- hash_value
- hw_cpu_cores
- hw_cpu_policy
- hw_cpu_sockets
- hw_cpu_thread_policy
- hw_cpu_threads
- hw_disk_bus
- hw_machine_type
- hw_qemu_guest_agent
- hw_rng_model
- hw_scsi_model
- hw_serial_port_count
- hw_video_model
- hw_video_ram
- hw_vif_model
- hw_watchdog_action
- hypervisor_type
- id
- instance_type_rxtx_factor
- instance_uuid
- is_hidden
- is_hw_boot_menu_enabled
- is_hw_vif_multiqueue_enabled
- is_protected
- kernel_id
- locations
- metadata
- min_disk
- min_ram
- name
- needs_config_drive
- needs_secure_boot
- os_admin_user
- os_command_line
- os_distro
- os_require_quiesce
- os_shutdown_timeout
- os_type
- os_version
- owner
- owner_id
- properties
- ramdisk_id
- schema
- size
- status
- store
- tags
- updated_at
- url
- virtual_size
- visibility
- vm_mode
- vmware_adaptertype
- vmware_ostype

View File

@@ -1,11 +0,0 @@
---
- name: List all images # This will list at least the default cirros image of devstack
openstack.cloud.image_info:
cloud: "{{ cloud }}"
register: image_list_result
- name: Assert fields
assert:
that:
- item in image_list_result.images.0.keys()
loop: "{{ expected_fields }}"

View File

@@ -26,7 +26,6 @@
- { role: identity_role, tags: identity_role } - { role: identity_role, tags: identity_role }
- { role: identity_role_info, tags: identity_role_info } - { role: identity_role_info, tags: identity_role_info }
- { role: image, tags: image } - { role: image, tags: image }
- { role: image_info, tags: image_info }
- { role: keypair, tags: keypair } - { role: keypair, tags: keypair }
- { role: keystone_domain, tags: keystone_domain } - { role: keystone_domain, tags: keystone_domain }
- role: keystone_idp - role: keystone_idp

View File

@@ -5,93 +5,99 @@
# Copyright (c) 2013, Benno Joy <benno@ansible.com> # Copyright (c) 2013, Benno Joy <benno@ansible.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = ''' DOCUMENTATION = r'''
---
module: image module: image
short_description: Add/Delete images from OpenStack Cloud short_description: Manage images of OpenStack image (Glance) service.
author: OpenStack Ansible SIG author: OpenStack Ansible SIG
description: description:
- Add or Remove images from the OpenStack Image Repository - Create or delete images in OpenStack image (Glance) service.
options: options:
name:
description:
- The name of the image when uploading - or the name/ID of the image if deleting
- If provided with the id, it can be used to change the name of existing image
required: true
type: str
id:
description:
- The ID of the image when uploading an image
- This image attribute cannot be changed.
type: str
checksum: checksum:
description: description:
- The checksum of the image - The checksum of the image.
type: str
disk_format:
description:
- The format of the disk that is getting uploaded
- This image attribute cannot be changed.
default: qcow2
choices: ['ami', 'ari', 'aki', 'vhd', 'vmdk', 'raw', 'qcow2', 'vdi', 'iso', 'vhdx', 'ploop']
type: str type: str
container_format: container_format:
description: description:
- The format of the container - The format of the container.
- This image attribute cannot be changed. - This image attribute cannot be changed.
- Examples are C(ami), C(aki), C(ari), C(bare), C(ovf), C(ova) or
C(docker).
default: bare default: bare
choices: ['ami', 'aki', 'ari', 'bare', 'ovf', 'ova', 'docker'] type: str
disk_format:
description:
- The format of the disk that is getting uploaded.
- This image attribute cannot be changed.
- Examples are C(ami), C(ari), C(aki), C(vhd), C(vmdk), C(raw),
C(qcow2), C(vdi), c(iso), C(vhdx) or C(ploop).
default: qcow2
type: str
filename:
description:
- The path to the file which has to be uploaded.
- This image attribute cannot be changed.
type: str
id:
description:
- The ID of the image when uploading an image.
- This image attribute cannot be changed.
type: str
is_protected:
description:
- Prevent image from being deleted.
aliases: ['protected']
type: bool
is_public:
description:
- Whether the image can be accessed publicly.
- Setting I(is_public) to C(true) requires admin role by default.
- I(is_public) has been deprecated. Use I(visibility) instead of
I(is_public).
type: bool
default: false
kernel:
description:
- The name of an existing kernel image that will be associated with this
image.
type: str
min_disk:
description:
- The minimum disk space (in GB) required to boot this image.
type: int
min_ram:
description:
- The minimum ram (in MB) required to boot this image.
type: int
name:
description:
- The name of the image when uploading - or the name/ID of the image if
deleting.
- If provided with the id, it can be used to change the name of existing
image.
required: true
type: str type: str
owner: owner:
description: description:
- The name or ID of the project owning the image - The name or ID of the project owning the image.
type: str type: str
aliases: ['project'] aliases: ['project']
owner_domain: owner_domain:
description: description:
- The name or id of the domain the project owning the image belongs to - The name or id of the domain the project owning the image belongs to.
- May be used to identify a unique project when providing a name to the project argument and multiple projects with such name exist - May be used to identify a unique project when providing a name to the
project argument and multiple projects with such name exist.
type: str type: str
aliases: ['project_domain'] aliases: ['project_domain']
min_disk:
description:
- The minimum disk space (in GB) required to boot this image
type: int
min_ram:
description:
- The minimum ram (in MB) required to boot this image
type: int
is_public:
description:
- Whether the image can be accessed publicly.
Note that publicizing an image requires admin role by default.
- Use I(visibility) instead of I(is_public),
the latter has been deprecated.
type: bool
default: false
is_protected:
description:
- Prevent image from being deleted
aliases: ['protected']
type: bool
filename:
description:
- The path to the file which has to be uploaded
- This image attribute cannot be changed.
type: str
ramdisk:
description:
- The name of an existing ramdisk image that will be associated with this image
type: str
kernel:
description:
- The name of an existing kernel image that will be associated with this image
type: str
properties: properties:
description: description:
- Additional properties to be associated with this image - Additional properties to be associated with this image.
default: {} default: {}
type: dict type: dict
ramdisk:
description:
- The name of an existing ramdisk image that will be associated with this
image.
type: str
state: state:
description: description:
- Should the resource be present or absent. - Should the resource be present or absent.
@@ -100,40 +106,33 @@ options:
type: str type: str
tags: tags:
description: description:
- List of tags to be applied to the image - List of tags to be applied to the image.
default: [] default: []
type: list type: list
elements: str elements: str
visibility: visibility:
description: description:
- The image visibility - The image visibility.
type: str type: str
choices: [public, private, shared, community] choices: [public, private, shared, community]
volume: volume:
description: description:
- ID of a volume to create an image from. - ID of a volume to create an image from.
- The volume must be in AVAILABLE state. - The volume must be in AVAILABLE state.
- Switch to module M(openstack.cloud.volume) instead of using I(volume), - I(volume) has been deprecated. Use module M(openstack.cloud.volume)
the latter has been deprecated. instead.
type: str type: str
requirements: requirements:
- "python >= 3.6" - "python >= 3.6"
- "openstacksdk" - "openstacksdk"
extends_documentation_fragment: extends_documentation_fragment:
- openstack.cloud.openstack - openstack.cloud.openstack
''' '''
EXAMPLES = ''' EXAMPLES = r'''
# Upload an image from a local file named cirros-0.3.0-x86_64-disk.img - name: Upload an image from a local file named cirros-0.3.0-x86_64-disk.img
- openstack.cloud.image: openstack.cloud.image:
auth: cloud: devstack-admin
auth_url: https://identity.example.com
username: admin
password: passme
project_name: admin
openstack.cloud.identity_user_domain_name: Default
openstack.cloud.project_domain_name: Default
name: cirros name: cirros
container_format: bare container_format: bare
disk_format: qcow2 disk_format: qcow2
@@ -148,303 +147,226 @@ EXAMPLES = '''
distro: ubuntu distro: ubuntu
''' '''
RETURN = ''' RETURN = r'''
id:
description: ID of the image.
returned: On success when I(state) is 'present'.
type: str
image: image:
description: Dictionary describing the image. description: Dictionary describing the Glance image.
returned: On success when I(state) is C(present).
type: dict type: dict
returned: On success when I(state) is 'present'.
contains: contains:
id: id:
description: Unique UUID. description: Unique UUID.
returned: success
type: str type: str
name: name:
description: Name given to the image. description: Name given to the image.
returned: success
type: str type: str
status: status:
description: Image status. description: Image status.
returned: success
type: str type: str
architecture: architecture:
description: | description: The CPU architecture that must be supported by
The CPU architecture that must be supported by the hypervisor. the hypervisor.
returned: success
type: str type: str
created_at: created_at:
description: Image created at timestamp. description: Image created at timestamp.
returned: success
type: str type: str
container_format: container_format:
description: Container format of the image. description: Container format of the image.
returned: success
type: str type: str
direct_url: direct_url:
description: URL to access the image file kept in external store. description: URL to access the image file kept in external store.
returned: success
type: str type: str
min_ram: min_ram:
description: Min amount of RAM required for this image. description: Min amount of RAM required for this image.
returned: success
type: int type: int
disk_format: disk_format:
description: Disk format of the image. description: Disk format of the image.
returned: success
type: str type: str
file: file:
description: The URL for the virtual machine image file. description: The URL for the virtual machine image file.
returned: success
type: str type: str
has_auto_disk_config: has_auto_disk_config:
description: > description: If root partition on disk is automatically resized
If root partition on disk is automatically resized before the instance before the instance boots.
boots.
returned: success
type: bool type: bool
hash_algo: hash_algo:
description: | description: The algorithm used to compute a secure hash of the
The algorithm used to compute a secure hash of the image data. image data.
returned: success
type: str type: str
hash_value: hash_value:
description: > description: The hexdigest of the secure hash of the image data
The hexdigest of the secure hash of the image data computed using the computed using the algorithm whose name is the value of the
algorithm whose name is the value of the os_hash_algo property. os_hash_algo property.
returned: success
type: str type: str
hw_cpu_cores: hw_cpu_cores:
description: > description: Used to pin the virtual CPUs (vCPUs) of instances to
Used to pin the virtual CPUs (vCPUs) of instances to the host's physical the host's physical CPU cores (pCPUs).
CPU cores (pCPUs).
returned: success
type: str type: str
hw_cpu_policy: hw_cpu_policy:
description: The hexdigest of the secure hash of the image data. description: The hexdigest of the secure hash of the image data.
returned: success
type: str type: str
hw_cpu_sockets: hw_cpu_sockets:
description: Preferred number of sockets to expose to the guest. description: Preferred number of sockets to expose to the guest.
returned: success
type: str type: str
hw_cpu_thread_policy: hw_cpu_thread_policy:
description: > description: Defines how hardware CPU threads in a simultaneous
Defines how hardware CPU threads in a simultaneous multithreading-based multithreading-based (SMT) architecture be used.
(SMT) architecture be used.
returned: success
type: str type: str
hw_cpu_threads: hw_cpu_threads:
description: | description: The preferred number of threads to expose to the guest.
The preferred number of threads to expose to the guest.
returned: success
type: str type: str
hw_disk_bus: hw_disk_bus:
description: | description: Specifies the type of disk controller to attach disk
Specifies the type of disk controller to attach disk devices to. devices to.
returned: success
type: str type: str
hw_machine_type: hw_machine_type:
description: | description: Enables booting an ARM system using the
Enables booting an ARM system using the specified machine type. specified machine type.
returned: success
type: str type: str
hw_qemu_guest_agent: hw_qemu_guest_agent:
description: > description: "A string boolean, which if 'true', QEMU guest agent
A string boolean, which if "true", QEMU guest agent will be exposed to will be exposed to the instance."
the instance.
returned: success
type: str type: str
hw_rng_model: hw_rng_model:
description: Adds a random-number generator device to the image's instances. description: "Adds a random-number generator device to the image's
returned: success instances."
type: str type: str
hw_scsi_model: hw_scsi_model:
description: > description: Enables the use of VirtIO SCSI (virtio-scsi) to
Enables the use of VirtIO SCSI (virtio-scsi) to provide block device provide block device access for compute instances.
access for compute instances.
returned: success
type: str type: str
hw_video_model: hw_video_model:
description: The video image driver used. description: The video image driver used.
returned: success
type: str type: str
hw_video_ram: hw_video_ram:
description: Maximum RAM for the video image. description: Maximum RAM for the video image.
returned: success
type: str type: str
hw_vif_model: hw_vif_model:
description: Specifies the model of virtual network interface device to use. description: Specifies the model of virtual network interface device to
returned: success use.
type: str type: str
hw_watchdog_action: hw_watchdog_action:
description: > description: Enables a virtual hardware watchdog device that
Enables a virtual hardware watchdog device that carries out the carries out the specified action if the server hangs.
specified action if the server hangs.
returned: success
type: str type: str
hypervisor_type: hypervisor_type:
description: The hypervisor type. description: The hypervisor type.
returned: success
type: str type: str
instance_type_rxtx_factor: instance_type_rxtx_factor:
description: > description: Optional property allows created servers to have a
Optional property allows created servers to have a different bandwidth different bandwidth cap than that defined in the network
cap than that defined in the network they are attached to. they are attached to.
returned: success
type: str type: str
instance_uuid: instance_uuid:
description: > description: For snapshot images, this is the UUID of the server
For snapshot images, this is the UUID of the server used to create this used to create this image.
image.
returned: success
type: str type: str
is_hidden: is_hidden:
description: >- description: Controls whether an image is displayed in the default
Controls whether an image is displayed in the default image-list image-list response
response
returned: success
type: bool type: bool
is_hw_boot_menu_enabled: is_hw_boot_menu_enabled:
description: Enables the BIOS bootmenu. description: Enables the BIOS bootmenu.
returned: success
type: bool type: bool
is_hw_vif_multiqueue_enabled: is_hw_vif_multiqueue_enabled:
description: | description: Enables the virtio-net multiqueue feature.
Enables the virtio-net multiqueue feature.
returned: success
type: bool type: bool
kernel_id: kernel_id:
description: > description: The ID of an image stored in the Image service that
The ID of an image stored in the Image service that should be used as should be used as the kernel when booting an AMI-style
the kernel when booting an AMI-style image. image.
returned: success
type: str type: str
locations: locations:
description: A list of URLs to access the image file in external store. description: A list of URLs to access the image file in external store.
returned: success
type: str type: str
metadata: metadata:
description: The location metadata. description: The location metadata.
returned: success
type: str type: str
needs_config_drive: needs_config_drive:
description: Specifies whether the image needs a config drive. description: Specifies whether the image needs a config drive.
returned: success
type: bool type: bool
needs_secure_boot: needs_secure_boot:
description: Whether Secure Boot is needed. description: Whether Secure Boot is needed.
returned: success
type: bool type: bool
os_admin_user: os_admin_user:
description: The operating system admin username. description: The operating system admin username.
returned: success
type: str type: str
os_command_line: os_command_line:
description: The kernel command line to be used by libvirt driver. description: The kernel command line to be used by libvirt driver.
returned: success
type: str type: str
os_distro: os_distro:
description: | description: The common name of the operating system distribution
The common name of the operating system distribution in lowercase. in lowercase.
returned: success
type: str type: str
os_require_quiesce: os_require_quiesce:
description: | description: If true, require quiesce on snapshot via
If true, require quiesce on snapshot via QEMU guest agent. QEMU guest agent.
returned: success
type: str type: str
os_shutdown_timeout: os_shutdown_timeout:
description: Time for graceful shutdown. description: Time for graceful shutdown.
returned: success
type: str type: str
os_type: os_type:
description: The operating system installed on the image. description: The operating system installed on the image.
returned: success
type: str type: str
os_version: os_version:
description: | description: The operating system version as specified by
The operating system version as specified by the distributor. the distributor.
returned: success
type: str type: str
owner_id: owner_id:
description: 'The ID of the owner, or project, of the image.' description: The ID of the owner, or project, of the image.
returned: success
type: str type: str
ramdisk_id: ramdisk_id:
description: > description: The ID of image stored in the Image service that should
The ID of image stored in the Image service that should be used as the be used as the ramdisk when booting an AMI-style image.
ramdisk when booting an AMI-style image.
returned: success
type: str type: str
schema: schema:
description: URL for the schema describing a virtual machine image. description: URL for the schema describing a virtual machine image.
returned: success
type: str type: str
store: store:
description: > description: Glance will attempt to store the disk image data in the
Glance will attempt to store the disk image data in the backing store backing store indicated by the value of the header.
indicated by the value of the header.
returned: success
type: str type: str
updated_at: updated_at:
description: Image updated at timestamp. description: Image updated at timestamp.
returned: success
type: str type: str
url: url:
description: URL to access the image file kept in external store. description: URL to access the image file kept in external store.
returned: success
type: str type: str
virtual_size: virtual_size:
description: The virtual size of the image. description: The virtual size of the image.
returned: success
type: str type: str
vm_mode: vm_mode:
description: The virtual machine mode. description: The virtual machine mode.
returned: success
type: str type: str
vmware_adaptertype: vmware_adaptertype:
description: | description: The virtual SCSI or IDE controller used by the
The virtual SCSI or IDE controller used by the hypervisor. hypervisor.
returned: success
type: str type: str
vmware_ostype: vmware_ostype:
description: Operating system installed in the image. description: Operating system installed in the image.
returned: success
type: str type: str
filters: filters:
description: Additional properties associated with the image. description: Additional properties associated with the image.
returned: success
type: dict type: dict
min_disk: min_disk:
description: Min amount of disk space required for this image. description: Min amount of disk space required for this image.
returned: success
type: int type: int
is_protected: is_protected:
description: Image protected flag. description: Image protected flag.
returned: success
type: bool type: bool
checksum: checksum:
description: Checksum for the image. description: Checksum for the image.
returned: success
type: str type: str
owner: owner:
description: Owner for the image. description: Owner for the image.
returned: success
type: str type: str
visibility: visibility:
description: Indicates who has access to the image. description: Indicates who has access to the image.
returned: success
type: str type: str
size: size:
description: Size of the image. description: Size of the image.
returned: success
type: int type: int
tags: tags:
description: List of tags assigned to the image description: List of tags assigned to the image
returned: success
type: list type: list
''' '''
@@ -454,26 +376,25 @@ from ansible_collections.openstack.cloud.plugins.module_utils.openstack import O
class ImageModule(OpenStackModule): class ImageModule(OpenStackModule):
argument_spec = dict( argument_spec = dict(
name=dict(required=True),
id=dict(),
checksum=dict(), checksum=dict(),
disk_format=dict(default='qcow2', container_format=dict(default='bare'),
choices=['ami', 'ari', 'aki', 'vhd', 'vmdk', 'raw', 'qcow2', 'vdi', 'iso', 'vhdx', 'ploop']), disk_format=dict(default='qcow2'),
container_format=dict(default='bare', choices=['ami', 'aki', 'ari', 'bare', 'ovf', 'ova', 'docker']), filename=dict(),
owner=dict(aliases=['project']), id=dict(),
owner_domain=dict(aliases=['project_domain']), is_protected=dict(type='bool', aliases=['protected']),
is_public=dict(type='bool', default=False),
kernel=dict(),
min_disk=dict(type='int'), min_disk=dict(type='int'),
min_ram=dict(type='int'), min_ram=dict(type='int'),
is_public=dict(type='bool', default=False), name=dict(required=True),
is_protected=dict(type='bool', aliases=['protected']), owner=dict(aliases=['project']),
filename=dict(), owner_domain=dict(aliases=['project_domain']),
ramdisk=dict(),
kernel=dict(),
properties=dict(type='dict', default={}), properties=dict(type='dict', default={}),
volume=dict(), ramdisk=dict(),
tags=dict(type='list', default=[], elements='str'),
state=dict(default='present', choices=['absent', 'present']), state=dict(default='present', choices=['absent', 'present']),
tags=dict(type='list', default=[], elements='str'),
visibility=dict(choices=['public', 'private', 'shared', 'community']), visibility=dict(choices=['public', 'private', 'shared', 'community']),
volume=dict(),
) )
module_kwargs = dict( module_kwargs = dict(
@@ -574,15 +495,15 @@ class ImageModule(OpenStackModule):
if self.params['state'] == 'present': if self.params['state'] == 'present':
attrs = self._build_params(owner) attrs = self._build_params(owner)
if not image: if not image:
# self.conn.image.create_image cannot be used because # self.conn.image.create_image() cannot be used because it does
# self.conn.create_image provides a volume parameter # not provide self.conn.create_image()'s volume parameter [0].
# Ref.: https://opendev.org/openstack/openstacksdk/src/commit/a41d04ea197439c2f134ce3554995693933a46ac/openstack/cloud/_image.py#L306 # [0] https://opendev.org/openstack/openstacksdk/src/commit/
# a41d04ea197439c2f134ce3554995693933a46ac/openstack/cloud/_image.py#L306
image = self.conn.create_image(**attrs) image = self.conn.create_image(**attrs)
changed = True changed = True
if not self.params['wait']: if not self.params['wait']:
self.exit_json(changed=changed, self.exit_json(changed=changed,
image=self._return_value(image.id), image=self._return_value(image.id))
id=image.id)
update_payload = self._build_update(image) update_payload = self._build_update(image)
@@ -590,8 +511,7 @@ class ImageModule(OpenStackModule):
self.conn.image.update_image(image.id, **update_payload) self.conn.image.update_image(image.id, **update_payload)
changed = True changed = True
self.exit_json(changed=changed, image=self._return_value(image.id), self.exit_json(changed=changed, image=self._return_value(image.id))
id=image.id)
elif self.params['state'] == 'absent' and image is not None: elif self.params['state'] == 'absent' and image is not None:
# self.conn.image.delete_image() does not offer a wait parameter # self.conn.image.delete_image() does not offer a wait parameter

View File

@@ -4,394 +4,296 @@
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. # Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = ''' DOCUMENTATION = r'''
--- ---
module: image_info module: image_info
short_description: Retrieve information about an image within OpenStack. short_description: Fetch images from OpenStack image (Glance) service.
author: OpenStack Ansible SIG author: OpenStack Ansible SIG
description: description:
- Retrieve information about a image image from OpenStack. - Fetch images from OpenStack image (Glance) service.
options: options:
image: name:
description: description:
- Name or ID of the image - Name or ID of the image
required: false
type: str type: str
aliases: ['image']
filters: filters:
description: description:
- Dict of properties of the images used for query - Dict of properties of the images used for query
type: dict type: dict
required: false
aliases: ['properties'] aliases: ['properties']
requirements: requirements:
- "python >= 3.6" - "python >= 3.6"
- "openstacksdk" - "openstacksdk"
extends_documentation_fragment: extends_documentation_fragment:
- openstack.cloud.openstack - openstack.cloud.openstack
''' '''
EXAMPLES = ''' EXAMPLES = r'''
- name: Gather information about a previously created image named image1 - name: Gather previously created image named image1
openstack.cloud.image_info: openstack.cloud.image_info:
auth: cloud: devstack-admin
auth_url: https://identity.example.com
username: user
password: password
project_name: someproject
image: image1 image: image1
register: result
- name: Show openstack information - name: List all images
debug:
msg: "{{ result.image }}"
# Show all available Openstack images
- name: Retrieve all available Openstack images
openstack.cloud.image_info: openstack.cloud.image_info:
register: result
- name: Show images - name: Retrieve and filter images
debug: openstack.cloud.image_info:
msg: "{{ result.image }}"
# Show images matching requested properties
- name: Retrieve images having properties with desired values
openstack.cloud.image_facts:
filters: filters:
some_property: some_value is_protected: False
OtherProp: OtherVal
- name: Show images
debug:
msg: "{{ result.image }}"
''' '''
RETURN = ''' RETURN = r'''
images: images:
description: has all the openstack information about the image description: List of dictionaries describing matching images.
returned: always, but can be null returned: always
type: complex type: list
elements: dict
contains: contains:
id: id:
description: Unique UUID. description: Unique UUID.
returned: success
type: str type: str
name: name:
description: Name given to the image. description: Name given to the image.
returned: success
type: str type: str
status: status:
description: Image status. description: Image status.
returned: success
type: str type: str
architecture: architecture:
description: > description: The CPU architecture that must be supported by
The CPU architecture that must be supported by
the hypervisor. the hypervisor.
returned: success
type: str type: str
created_at: created_at:
description: Image created at timestamp. description: Image created at timestamp.
returned: success
type: str type: str
container_format: container_format:
description: Container format of the image. description: Container format of the image.
returned: success
type: str type: str
direct_url: direct_url:
description: URL to access the image file kept in external store. description: URL to access the image file kept in external store.
returned: success
type: str type: str
min_ram: min_ram:
description: Min amount of RAM required for this image. description: Min amount of RAM required for this image.
returned: success
type: int type: int
disk_format: disk_format:
description: Disk format of the image. description: Disk format of the image.
returned: success
type: str type: str
file: file:
description: The URL for the virtual machine image file. description: The URL for the virtual machine image file.
returned: success
type: str type: str
has_auto_disk_config: has_auto_disk_config:
description: > description: If root partition on disk is automatically resized
If root partition on disk is automatically resized
before the instance boots. before the instance boots.
returned: success
type: bool type: bool
hash_algo: hash_algo:
description: > description: The algorithm used to compute a secure hash of the
The algorithm used to compute a secure hash of the
image data. image data.
returned: success
type: str type: str
hash_value: hash_value:
description: > description: The hexdigest of the secure hash of the image data
The hexdigest of the secure hash of the image data computed using the algorithm whose name is the value of the
computed using the algorithm whose name is the value of the os_hash_algo property. os_hash_algo property.
returned: success
type: str type: str
hw_cpu_cores: hw_cpu_cores:
description: > description: Used to pin the virtual CPUs (vCPUs) of instances to
Used to pin the virtual CPUs (vCPUs) of instances to
the host's physical CPU cores (pCPUs). the host's physical CPU cores (pCPUs).
returned: success
type: str type: str
hw_cpu_policy: hw_cpu_policy:
description: The hexdigest of the secure hash of the image data. description: The hexdigest of the secure hash of the image data.
returned: success
type: str type: str
hw_cpu_sockets: hw_cpu_sockets:
description: Preferred number of sockets to expose to the guest. description: Preferred number of sockets to expose to the guest.
returned: success
type: str type: str
hw_cpu_thread_policy: hw_cpu_thread_policy:
description: > description: Defines how hardware CPU threads in a simultaneous
Defines how hardware CPU threads in a simultaneous
multithreading-based (SMT) architecture be used. multithreading-based (SMT) architecture be used.
returned: success
type: str type: str
hw_cpu_threads: hw_cpu_threads:
description: > description: The preferred number of threads to expose to the guest.
The preferred number of threads to expose to the guest.
returned: success
type: str type: str
hw_disk_bus: hw_disk_bus:
description: > description: Specifies the type of disk controller to attach disk
Specifies the type of disk controller to attach disk
devices to. devices to.
returned: success
type: str type: str
hw_machine_type: hw_machine_type:
description: > description: Enables booting an ARM system using the
Enables booting an ARM system using the
specified machine type. specified machine type.
returned: success
type: str type: str
hw_qemu_guest_agent: hw_qemu_guest_agent:
description: > description: "A string boolean, which if 'true', QEMU guest agent
A string boolean, which if "true", QEMU guest agent will be exposed to the instance."
will be exposed to the instance.
returned: success
type: str type: str
hw_rng_model: hw_rng_model:
description: Adds a random-number generator device to the image's instances. description: "Adds a random-number generator device to the image's
returned: success instances."
type: str type: str
hw_scsi_model: hw_scsi_model:
description: > description: Enables the use of VirtIO SCSI (virtio-scsi) to
Enables the use of VirtIO SCSI (virtio-scsi) to
provide block device access for compute instances. provide block device access for compute instances.
returned: success
type: str type: str
hw_video_model: hw_video_model:
description: The video image driver used. description: The video image driver used.
returned: success
type: str type: str
hw_video_ram: hw_video_ram:
description: Maximum RAM for the video image. description: Maximum RAM for the video image.
returned: success
type: str type: str
hw_vif_model: hw_vif_model:
description: Specifies the model of virtual network interface device to use. description: Specifies the model of virtual network interface device to
returned: success use.
type: str type: str
hw_watchdog_action: hw_watchdog_action:
description: > description: Enables a virtual hardware watchdog device that
Enables a virtual hardware watchdog device that
carries out the specified action if the server hangs. carries out the specified action if the server hangs.
returned: success
type: str type: str
hypervisor_type: hypervisor_type:
description: The hypervisor type. description: The hypervisor type.
returned: success
type: str type: str
instance_type_rxtx_factor: instance_type_rxtx_factor:
description: > description: Optional property allows created servers to have a
Optional property allows created servers to have a different bandwidth cap than that defined in the network
different bandwidth cap than that defined in the network they are attached to. they are attached to.
returned: success
type: str type: str
instance_uuid: instance_uuid:
description: > description: For snapshot images, this is the UUID of the server
For snapshot images, this is the UUID of the server
used to create this image. used to create this image.
returned: success
type: str type: str
is_hidden: is_hidden:
description: Controls whether an image is displayed in the default image-list response description: Controls whether an image is displayed in the default
returned: success image-list response
type: bool type: bool
is_hw_boot_menu_enabled: is_hw_boot_menu_enabled:
description: Enables the BIOS bootmenu. description: Enables the BIOS bootmenu.
returned: success
type: bool type: bool
is_hw_vif_multiqueue_enabled: is_hw_vif_multiqueue_enabled:
description: > description: Enables the virtio-net multiqueue feature.
Enables the virtio-net multiqueue
feature.
returned: success
type: bool type: bool
kernel_id: kernel_id:
description: > description: The ID of an image stored in the Image service that
The ID of an image stored in the Image service that should be used as the kernel when booting an AMI-style
should be used as the kernel when booting an AMI-style image. image.
returned: success
type: str type: str
locations: locations:
description: A list of URLs to access the image file in external store. description: A list of URLs to access the image file in external store.
returned: success
type: str type: str
metadata: metadata:
description: The location metadata. description: The location metadata.
returned: success
type: str type: str
needs_config_drive: needs_config_drive:
description: Specifies whether the image needs a config drive. description: Specifies whether the image needs a config drive.
returned: success
type: bool type: bool
needs_secure_boot: needs_secure_boot:
description: Whether Secure Boot is needed. description: Whether Secure Boot is needed.
returned: success
type: bool type: bool
os_admin_user: os_admin_user:
description: The operating system admin username. description: The operating system admin username.
returned: success
type: str type: str
os_command_line: os_command_line:
description: The kernel command line to be used by libvirt driver. description: The kernel command line to be used by libvirt driver.
returned: success
type: str type: str
os_distro: os_distro:
description: > description: The common name of the operating system distribution
The common name of the operating system distribution
in lowercase. in lowercase.
returned: success
type: str type: str
os_require_quiesce: os_require_quiesce:
description: > description: If true, require quiesce on snapshot via
If true, require quiesce on snapshot via
QEMU guest agent. QEMU guest agent.
returned: success
type: str type: str
os_shutdown_timeout: os_shutdown_timeout:
description: Time for graceful shutdown. description: Time for graceful shutdown.
returned: success
type: str type: str
os_type: os_type:
description: The operating system installed on the image. description: The operating system installed on the image.
returned: success
type: str type: str
os_version: os_version:
description: > description: The operating system version as specified by
The operating system version as specified by
the distributor. the distributor.
returned: success
type: str type: str
owner_id: owner_id:
description: The ID of the owner, or project, of the image. description: The ID of the owner, or project, of the image.
returned: success
type: str type: str
ramdisk_id: ramdisk_id:
description: > description: The ID of image stored in the Image service that should
The ID of image stored in the Image service that be used as the ramdisk when booting an AMI-style image.
should be used as the ramdisk when booting an AMI-style image.
returned: success
type: str type: str
schema: schema:
description: URL for the schema describing a virtual machine image. description: URL for the schema describing a virtual machine image.
returned: success
type: str type: str
store: store:
description: > description: Glance will attempt to store the disk image data in the
Glance will attempt to store the disk backing store indicated by the value of the header.
image data in the backing store indicated by the value of the
header.
returned: success
type: str type: str
updated_at: updated_at:
description: Image updated at timestamp. description: Image updated at timestamp.
returned: success
type: str type: str
url: url:
description: URL to access the image file kept in external store. description: URL to access the image file kept in external store.
returned: success
type: str type: str
virtual_size: virtual_size:
description: The virtual size of the image. description: The virtual size of the image.
returned: success
type: str type: str
vm_mode: vm_mode:
description: The virtual machine mode. description: The virtual machine mode.
returned: success
type: str type: str
vmware_adaptertype: vmware_adaptertype:
description: > description: The virtual SCSI or IDE controller used by the
The virtual SCSI or IDE controller used by the
hypervisor. hypervisor.
returned: success
type: str type: str
vmware_ostype: vmware_ostype:
description: Operating system installed in the image. description: Operating system installed in the image.
returned: success
type: str type: str
filters: filters:
description: Additional properties associated with the image. description: Additional properties associated with the image.
returned: success
type: dict type: dict
min_disk: min_disk:
description: Min amount of disk space required for this image. description: Min amount of disk space required for this image.
returned: success
type: int type: int
is_protected: is_protected:
description: Image protected flag. description: Image protected flag.
returned: success
type: bool type: bool
checksum: checksum:
description: Checksum for the image. description: Checksum for the image.
returned: success
type: str type: str
owner: owner:
description: Owner for the image. description: Owner for the image.
returned: success
type: str type: str
visibility: visibility:
description: Indicates who has access to the image. description: Indicates who has access to the image.
returned: success
type: str type: str
size: size:
description: Size of the image. description: Size of the image.
returned: success
type: int type: int
tags: tags:
description: List of tags assigned to the image description: List of tags assigned to the image
returned: success
type: list type: list
''' '''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
class ImageInfoModule(OpenStackModule): class ImageInfoModule(OpenStackModule):
argument_spec = dict( argument_spec = dict(
image=dict(),
filters=dict(type='dict', aliases=['properties']), filters=dict(type='dict', aliases=['properties']),
name=dict(aliases=['image']),
) )
module_kwargs = dict( module_kwargs = dict(
supports_check_mode=True supports_check_mode=True
) )
def run(self): def run(self):
args = { kwargs = dict((k, self.params[k])
'name_or_id': self.params['image'], for k in ['filters']
'filters': self.params['filters'], if self.params[k] is not None)
}
args = {k: v for k, v in args.items() if v is not None} name_or_id = self.params['name']
images = [image.to_dict(computed=False) for image in if name_or_id is not None:
self.conn.search_images(**args)] kwargs['name_or_id'] = name_or_id
self.exit(changed=False, images=images)
self.exit(changed=False,
images=[i.to_dict(computed=False)
for i in self.conn.search_images(**kwargs)])
def main(): def main():