1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2015, Adam Števko <adam.stevko@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
DOCUMENTATION = '''
---
module: smartos_image_facts
short_description: Get SmartOS image details.
description:
- Retrieve facts about all installed images on SmartOS. Facts will be
inserted to the ansible_facts key.
version_added: "2.2"
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 informaton can be found at U(https://smartos.org/man/1m/imgadm)
under 'imgadm list'.
required: false
default: None
'''
EXAMPLES = '''
# Return facts about all installed images.
smartos_image_facts:
# Return all private active Linux images.
smartos_image_facts: filters="os=linux state=active public=false"
# Show, how many clones does every image have.
smartos_image_facts:
debug: msg="{{ smartos_images[item]['name'] }}-{{smartos_images[item]['version'] }}
has {{ smartos_images[item]['clones'] }} VM(s)"
with_items: "{{ smartos_images.keys() }}"
'''
RETURN = '''
# this module returns ansible_facts
'''
try:
import json
except ImportError:
import simplejson as json
class ImageFacts(object):
def __init__(self, module):
self.module = module
self.filters = module.params['filters']
def return_all_installed_images(self):
cmd = [self.module.get_bin_path('imgadm')]
cmd.append('list')
cmd.append('-j')
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(
filters=dict(default=None),
),
supports_check_mode=False,
)
image_facts = ImageFacts(module)
data = {}
data['smartos_images'] = image_facts.return_all_installed_images()
module.exit_json(ansible_facts=data)
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()
|