2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-11-06 20:28:56 +01:00
|
|
|
# Copyright (c) 2018, Milan Ilic <milani@nordeus.com>
|
2022-08-07 13:37:23 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: one_image_info
|
|
|
|
short_description: Gather information on OpenNebula images
|
|
|
|
description:
|
|
|
|
- Gather information on OpenNebula images.
|
|
|
|
- This module was called C(one_image_facts) before Ansible 2.9. The usage did not change.
|
|
|
|
requirements:
|
|
|
|
- pyone
|
2022-11-06 21:16:43 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
- community.general.attributes.info_module
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
api_url:
|
|
|
|
description:
|
|
|
|
- URL of the OpenNebula RPC server.
|
|
|
|
- It is recommended to use HTTPS so that the username/password are not
|
|
|
|
- transferred over the network unencrypted.
|
2023-06-20 08:14:02 +02:00
|
|
|
- If not set then the value of the E(ONE_URL) environment variable is used.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
api_username:
|
|
|
|
description:
|
|
|
|
- Name of the user to login into the OpenNebula RPC server. If not set
|
2023-06-20 08:14:02 +02:00
|
|
|
- then the value of the E(ONE_USERNAME) environment variable is used.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
api_password:
|
|
|
|
description:
|
|
|
|
- Password of the user to login into OpenNebula RPC server. If not set
|
2023-06-20 08:14:02 +02:00
|
|
|
- then the value of the E(ONE_PASSWORD) environment variable is used.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
ids:
|
|
|
|
description:
|
|
|
|
- A list of images ids whose facts you want to gather.
|
|
|
|
aliases: ['id']
|
2020-12-01 21:28:40 +01:00
|
|
|
type: list
|
2021-03-08 08:35:09 +01:00
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
name:
|
|
|
|
description:
|
2023-06-20 08:14:02 +02:00
|
|
|
- A O(name) of the image whose facts will be gathered.
|
|
|
|
- If the O(name) begins with V(~) the O(name) will be used as regex pattern
|
2020-03-09 10:11:07 +01:00
|
|
|
- which restricts the list of images (whose facts will be returned) whose names match specified regex.
|
2023-06-20 08:14:02 +02:00
|
|
|
- Also, if the O(name) begins with V(~*) case-insensitive matching will be performed.
|
2020-03-09 10:11:07 +01:00
|
|
|
- See examples for more details.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
author:
|
|
|
|
- "Milan Ilic (@ilicmilan)"
|
|
|
|
- "Jan Meerkamp (@meerkampdvv)"
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Gather facts about all images
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.one_image_info:
|
2020-03-09 10:11:07 +01:00
|
|
|
register: result
|
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Print all images facts
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.debug:
|
2020-03-09 10:11:07 +01:00
|
|
|
msg: result
|
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Gather facts about an image using ID
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.one_image_info:
|
2020-03-09 10:11:07 +01:00
|
|
|
ids:
|
|
|
|
- 123
|
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Gather facts about an image using the name
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.one_image_info:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: 'foo-image'
|
|
|
|
register: foo_image
|
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Gather facts about all IMAGEs whose name matches regex 'app-image-.*'
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.one_image_info:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: '~app-image-.*'
|
|
|
|
register: app_images
|
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Gather facts about all IMAGEs whose name matches regex 'foo-image-.*' ignoring cases
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.one_image_info:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: '~*foo-image-.*'
|
|
|
|
register: foo_images
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
images:
|
|
|
|
description: A list of images info
|
|
|
|
type: complex
|
|
|
|
returned: success
|
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: image id
|
|
|
|
type: int
|
|
|
|
sample: 153
|
|
|
|
name:
|
|
|
|
description: image name
|
|
|
|
type: str
|
|
|
|
sample: app1
|
|
|
|
group_id:
|
|
|
|
description: image's group id
|
|
|
|
type: int
|
|
|
|
sample: 1
|
|
|
|
group_name:
|
|
|
|
description: image's group name
|
|
|
|
type: str
|
|
|
|
sample: one-users
|
|
|
|
owner_id:
|
|
|
|
description: image's owner id
|
|
|
|
type: int
|
|
|
|
sample: 143
|
|
|
|
owner_name:
|
|
|
|
description: image's owner name
|
|
|
|
type: str
|
|
|
|
sample: ansible-test
|
|
|
|
state:
|
|
|
|
description: state of image instance
|
|
|
|
type: str
|
|
|
|
sample: READY
|
|
|
|
used:
|
|
|
|
description: is image in use
|
|
|
|
type: bool
|
|
|
|
sample: true
|
|
|
|
running_vms:
|
|
|
|
description: count of running vms that use this image
|
|
|
|
type: int
|
|
|
|
sample: 7
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import pyone
|
|
|
|
HAS_PYONE = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_PYONE = False
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_images(client):
|
|
|
|
pool = client.imagepool.info(-2, -1, -1, -1)
|
|
|
|
# Filter -2 means fetch all images user can Use
|
|
|
|
|
|
|
|
return pool
|
|
|
|
|
|
|
|
|
|
|
|
IMAGE_STATES = ['INIT', 'READY', 'USED', 'DISABLED', 'LOCKED', 'ERROR', 'CLONE', 'DELETE', 'USED_PERS', 'LOCKED_USED', 'LOCKED_USED_PERS']
|
|
|
|
|
|
|
|
|
|
|
|
def get_image_info(image):
|
|
|
|
info = {
|
|
|
|
'id': image.ID,
|
|
|
|
'name': image.NAME,
|
|
|
|
'state': IMAGE_STATES[image.STATE],
|
|
|
|
'running_vms': image.RUNNING_VMS,
|
|
|
|
'used': bool(image.RUNNING_VMS),
|
|
|
|
'user_name': image.UNAME,
|
|
|
|
'user_id': image.UID,
|
|
|
|
'group_name': image.GNAME,
|
|
|
|
'group_id': image.GID,
|
|
|
|
}
|
|
|
|
return info
|
|
|
|
|
|
|
|
|
|
|
|
def get_images_by_ids(module, client, ids):
|
|
|
|
images = []
|
|
|
|
pool = get_all_images(client)
|
|
|
|
|
|
|
|
for image in pool.IMAGE:
|
|
|
|
if str(image.ID) in ids:
|
|
|
|
images.append(image)
|
|
|
|
ids.remove(str(image.ID))
|
|
|
|
if len(ids) == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
if len(ids) > 0:
|
|
|
|
module.fail_json(msg='There is no IMAGE(s) with id(s)=' + ', '.join('{id}'.format(id=str(image_id)) for image_id in ids))
|
|
|
|
|
|
|
|
return images
|
|
|
|
|
|
|
|
|
|
|
|
def get_images_by_name(module, client, name_pattern):
|
|
|
|
|
|
|
|
images = []
|
|
|
|
pattern = None
|
|
|
|
|
|
|
|
pool = get_all_images(client)
|
|
|
|
|
|
|
|
if name_pattern.startswith('~'):
|
|
|
|
import re
|
|
|
|
if name_pattern[1] == '*':
|
|
|
|
pattern = re.compile(name_pattern[2:], re.IGNORECASE)
|
|
|
|
else:
|
|
|
|
pattern = re.compile(name_pattern[1:])
|
|
|
|
|
|
|
|
for image in pool.IMAGE:
|
|
|
|
if pattern is not None:
|
|
|
|
if pattern.match(image.NAME):
|
|
|
|
images.append(image)
|
|
|
|
elif name_pattern == image.NAME:
|
|
|
|
images.append(image)
|
|
|
|
break
|
|
|
|
|
|
|
|
# if the specific name is indicated
|
|
|
|
if pattern is None and len(images) == 0:
|
|
|
|
module.fail_json(msg="There is no IMAGE with name=" + name_pattern)
|
|
|
|
|
|
|
|
return images
|
|
|
|
|
|
|
|
|
|
|
|
def get_connection_info(module):
|
|
|
|
|
|
|
|
url = module.params.get('api_url')
|
|
|
|
username = module.params.get('api_username')
|
|
|
|
password = module.params.get('api_password')
|
|
|
|
|
|
|
|
if not url:
|
|
|
|
url = os.environ.get('ONE_URL')
|
|
|
|
|
|
|
|
if not username:
|
|
|
|
username = os.environ.get('ONE_USERNAME')
|
|
|
|
|
|
|
|
if not password:
|
|
|
|
password = os.environ.get('ONE_PASSWORD')
|
|
|
|
|
2022-08-12 11:07:30 +02:00
|
|
|
if not (url and username and password):
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json(msg="One or more connection parameters (api_url, api_username, api_password) were not specified")
|
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
auth_params = namedtuple('auth', ('url', 'username', 'password'))
|
|
|
|
|
|
|
|
return auth_params(url=url, username=username, password=password)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
fields = {
|
|
|
|
"api_url": {"required": False, "type": "str"},
|
|
|
|
"api_username": {"required": False, "type": "str"},
|
|
|
|
"api_password": {"required": False, "type": "str", "no_log": True},
|
2021-03-08 08:35:09 +01:00
|
|
|
"ids": {"required": False, "aliases": ['id'], "type": "list", "elements": "str"},
|
2020-03-09 10:11:07 +01:00
|
|
|
"name": {"required": False, "type": "str"},
|
|
|
|
}
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=fields,
|
|
|
|
mutually_exclusive=[['ids', 'name']],
|
|
|
|
supports_check_mode=True)
|
|
|
|
|
|
|
|
if not HAS_PYONE:
|
|
|
|
module.fail_json(msg='This module requires pyone to work!')
|
|
|
|
|
|
|
|
auth = get_connection_info(module)
|
|
|
|
params = module.params
|
|
|
|
ids = params.get('ids')
|
|
|
|
name = params.get('name')
|
|
|
|
client = pyone.OneServer(auth.url, session=auth.username + ':' + auth.password)
|
|
|
|
|
|
|
|
if ids:
|
|
|
|
images = get_images_by_ids(module, client, ids)
|
|
|
|
elif name:
|
|
|
|
images = get_images_by_name(module, client, name)
|
|
|
|
else:
|
|
|
|
images = get_all_images(client).IMAGE
|
|
|
|
|
2021-03-08 08:35:09 +01:00
|
|
|
result = {
|
|
|
|
'images': [get_image_info(image) for image in images],
|
|
|
|
}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|