2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2015, Adam Števko <adam.stevko@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: smartos_image_info
|
|
|
|
short_description: Get SmartOS image details.
|
|
|
|
description:
|
|
|
|
- Retrieve information about all installed images on SmartOS.
|
|
|
|
- This module was called C(smartos_image_facts) before Ansible 2.9, returning C(ansible_facts).
|
2020-06-29 14:59:15 +02:00
|
|
|
Note that the M(community.general.smartos_image_info) module no longer returns C(ansible_facts)!
|
2020-03-09 10:11:07 +01:00
|
|
|
author: Adam Števko (@xen0l)
|
|
|
|
options:
|
|
|
|
filters:
|
|
|
|
description:
|
|
|
|
- Criteria for selecting image. Can be any value from image
|
|
|
|
manifest and 'published_date', 'published', 'source', 'clones',
|
|
|
|
and 'size'. More information can be found at U(https://smartos.org/man/1m/imgadm)
|
|
|
|
under 'imgadm list'.
|
2021-03-14 10:53:49 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Return information about all installed images
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.smartos_image_info:
|
2020-03-09 10:11:07 +01:00
|
|
|
register: result
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Return all private active Linux images
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.smartos_image_info:
|
2020-05-15 12:12:41 +02:00
|
|
|
filters: "os=linux state=active public=false"
|
2020-03-09 10:11:07 +01:00
|
|
|
register: result
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Show, how many clones does every image have
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.smartos_image_info:
|
2020-03-09 10:11:07 +01:00
|
|
|
register: result
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Print information
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.debug:
|
2020-05-15 12:12:41 +02:00
|
|
|
msg: "{{ result.smartos_images[item]['name'] }}-{{ result.smartos_images[item]['version'] }}
|
|
|
|
has {{ result.smartos_images[item]['clones'] }} VM(s)"
|
2020-03-09 10:11:07 +01:00
|
|
|
with_items: "{{ result.smartos_images.keys() | list }}"
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Print information
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.debug:
|
2020-05-15 12:12:41 +02:00
|
|
|
msg: "{{ smartos_images[item]['name'] }}-{{ smartos_images[item]['version'] }}
|
|
|
|
has {{ smartos_images[item]['clones'] }} VM(s)"
|
2020-03-09 10:11:07 +01:00
|
|
|
with_items: "{{ smartos_images.keys() | list }}"
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
'''
|
|
|
|
|
|
|
|
import json
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
class ImageFacts(object):
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
|
|
|
|
self.filters = module.params['filters']
|
|
|
|
|
|
|
|
def return_all_installed_images(self):
|
2021-04-05 09:22:06 +02:00
|
|
|
cmd = [self.module.get_bin_path('imgadm'), 'list', '-j']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if self.filters:
|
|
|
|
cmd.append(self.filters)
|
|
|
|
|
|
|
|
(rc, out, err) = self.module.run_command(cmd)
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
self.module.exit_json(
|
|
|
|
msg='Failed to get all installed images', stderr=err)
|
|
|
|
|
|
|
|
images = json.loads(out)
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
for image in images:
|
|
|
|
result[image['manifest']['uuid']] = image['manifest']
|
|
|
|
# Merge additional attributes with the image manifest.
|
|
|
|
for attrib in ['clones', 'source', 'zpool']:
|
|
|
|
result[image['manifest']['uuid']][attrib] = image[attrib]
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2022-04-24 10:49:29 +02:00
|
|
|
filters=dict(),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
2021-07-27 18:24:29 +02:00
|
|
|
supports_check_mode=True,
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
image_facts = ImageFacts(module)
|
|
|
|
|
|
|
|
data = dict(smartos_images=image_facts.return_all_installed_images())
|
|
|
|
|
2021-04-13 13:19:25 +02:00
|
|
|
module.exit_json(**data)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|