| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2021, Jyrki Gadinger <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
# Make coding more python3-ish |
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: one_template |
| 12 |
|
| 13 |
short_description: Manages OpenNebula templates |
| 14 |
|
| 15 |
version_added: 2.4.0 |
| 16 |
|
| 17 |
requirements: |
| 18 |
- pyone |
| 19 |
|
| 20 |
description: |
| 21 |
- Manages OpenNebula templates. |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
support: partial |
| 25 |
details: |
| 26 |
- Note that check mode always returns RV(ignore:changed=true) for existing templates, even if the template would not actually |
| 27 |
change. |
| 28 |
diff_mode: |
| 29 |
support: none |
| 30 |
|
| 31 |
options: |
| 32 |
id: |
| 33 |
description: |
| 34 |
- A O(id) of the template you would like to manage. If not set then a new template is created with the given O(name). |
| 35 |
type: int |
| 36 |
name: |
| 37 |
description: |
| 38 |
- A O(name) of the template you would like to manage. If a template with the given name does not exist it is created, |
| 39 |
otherwise it is managed by this module. |
| 40 |
type: str |
| 41 |
template: |
| 42 |
description: |
| 43 |
- A string containing the template contents. |
| 44 |
type: str |
| 45 |
state: |
| 46 |
description: |
| 47 |
- V(present) - state that is used to manage the template. |
| 48 |
- V(absent) - delete the template. |
| 49 |
choices: ["present", "absent"] |
| 50 |
default: present |
| 51 |
type: str |
| 52 |
filter: |
| 53 |
description: |
| 54 |
- V(user_primary_group) - Resources belonging to the user's primary group. |
| 55 |
- V(user) - Resources belonging to the user. |
| 56 |
- V(all) - All resources. |
| 57 |
- V(user_groups) - Resources belonging to the user and any of his groups. |
| 58 |
choices: [user_primary_group, user, all, user_groups] |
| 59 |
default: user |
| 60 |
type: str |
| 61 |
version_added: 10.3.0 |
| 62 |
|
| 63 |
extends_documentation_fragment: |
| 64 |
- community.general._opennebula |
| 65 |
- community.general._attributes |
| 66 |
|
| 67 |
author: |
| 68 |
- "Jyrki Gadinger (@nilsding)" |
| 69 |
""" |
| 70 |
|
| 71 |
EXAMPLES = r""" |
| 72 |
- name: Fetch the TEMPLATE by id |
| 73 |
community.general.one_template: |
| 74 |
id: 6459 |
| 75 |
register: result |
| 76 |
|
| 77 |
- name: Print the TEMPLATE properties |
| 78 |
ansible.builtin.debug: |
| 79 |
var: result |
| 80 |
|
| 81 |
- name: Fetch the TEMPLATE by name |
| 82 |
community.general.one_template: |
| 83 |
name: tf-prd-users-workerredis-p6379a |
| 84 |
register: result |
| 85 |
|
| 86 |
- name: Create a new or update an existing TEMPLATE |
| 87 |
community.general.one_template: |
| 88 |
name: generic-opensuse |
| 89 |
template: | |
| 90 |
CONTEXT = [ |
| 91 |
HOSTNAME = "generic-opensuse" |
| 92 |
] |
| 93 |
CPU = "1" |
| 94 |
CUSTOM_ATTRIBUTE = "" |
| 95 |
DISK = [ |
| 96 |
CACHE = "writeback", |
| 97 |
DEV_PREFIX = "sd", |
| 98 |
DISCARD = "unmap", |
| 99 |
IMAGE = "opensuse-leap-15.2", |
| 100 |
IMAGE_UNAME = "oneadmin", |
| 101 |
IO = "threads", |
| 102 |
SIZE = "" ] |
| 103 |
MEMORY = "2048" |
| 104 |
NIC = [ |
| 105 |
MODEL = "virtio", |
| 106 |
NETWORK = "testnet", |
| 107 |
NETWORK_UNAME = "oneadmin" ] |
| 108 |
OS = [ |
| 109 |
ARCH = "x86_64", |
| 110 |
BOOT = "disk0" ] |
| 111 |
SCHED_REQUIREMENTS = "CLUSTER_ID=\\"100\\"" |
| 112 |
VCPU = "2" |
| 113 |
|
| 114 |
- name: Delete the TEMPLATE by id |
| 115 |
community.general.one_template: |
| 116 |
id: 6459 |
| 117 |
state: absent |
| 118 |
""" |
| 119 |
|
| 120 |
RETURN = r""" |
| 121 |
id: |
| 122 |
description: Template ID. |
| 123 |
type: int |
| 124 |
returned: when O(state=present) |
| 125 |
sample: 153 |
| 126 |
name: |
| 127 |
description: Template name. |
| 128 |
type: str |
| 129 |
returned: when O(state=present) |
| 130 |
sample: app1 |
| 131 |
template: |
| 132 |
description: The parsed template. |
| 133 |
type: dict |
| 134 |
returned: when O(state=present) |
| 135 |
group_id: |
| 136 |
description: Template's group ID. |
| 137 |
type: int |
| 138 |
returned: when O(state=present) |
| 139 |
sample: 1 |
| 140 |
group_name: |
| 141 |
description: Template's group name. |
| 142 |
type: str |
| 143 |
returned: when O(state=present) |
| 144 |
sample: one-users |
| 145 |
owner_id: |
| 146 |
description: Template's owner ID. |
| 147 |
type: int |
| 148 |
returned: when O(state=present) |
| 149 |
sample: 143 |
| 150 |
owner_name: |
| 151 |
description: Template's owner name. |
| 152 |
type: str |
| 153 |
returned: when O(state=present) |
| 154 |
sample: ansible-test |
| 155 |
""" |
| 156 |
|
| 157 |
|
| 158 |
from ansible_collections.community.general.plugins.module_utils._opennebula import OpenNebulaModule |
| 159 |
|
| 160 |
|
| 161 |
class TemplateModule(OpenNebulaModule): |
| 162 |
def __init__(self): |
| 163 |
argument_spec = dict( |
| 164 |
id=dict(type="int"), |
| 165 |
name=dict(type="str"), |
| 166 |
state=dict(type="str", choices=["present", "absent"], default="present"), |
| 167 |
template=dict(type="str"), |
| 168 |
filter=dict(type="str", choices=["user_primary_group", "user", "all", "user_groups"], default="user"), |
| 169 |
) |
| 170 |
|
| 171 |
mutually_exclusive = [["id", "name"]] |
| 172 |
|
| 173 |
required_one_of = [("id", "name")] |
| 174 |
|
| 175 |
required_if = [["state", "present", ["template"]]] |
| 176 |
|
| 177 |
OpenNebulaModule.__init__( |
| 178 |
self, |
| 179 |
argument_spec, |
| 180 |
supports_check_mode=True, |
| 181 |
mutually_exclusive=mutually_exclusive, |
| 182 |
required_one_of=required_one_of, |
| 183 |
required_if=required_if, |
| 184 |
) |
| 185 |
|
| 186 |
def run(self, one, module, result): |
| 187 |
params = module.params |
| 188 |
id = params.get("id") |
| 189 |
name = params.get("name") |
| 190 |
desired_state = params.get("state") |
| 191 |
template_data = params.get("template") |
| 192 |
filter = params.get("filter") |
| 193 |
|
| 194 |
self.result = {} |
| 195 |
|
| 196 |
template = self.get_template_instance(id, name, filter) |
| 197 |
needs_creation = False |
| 198 |
if not template and desired_state != "absent": |
| 199 |
if id: |
| 200 |
module.fail_json(msg=f"There is no template with id={id}") |
| 201 |
else: |
| 202 |
needs_creation = True |
| 203 |
|
| 204 |
if desired_state == "absent": |
| 205 |
self.result = self.delete_template(template) |
| 206 |
else: |
| 207 |
if needs_creation: |
| 208 |
self.result = self.create_template(name, template_data, filter) |
| 209 |
else: |
| 210 |
self.result = self.update_template(template, template_data, filter) |
| 211 |
|
| 212 |
self.exit() |
| 213 |
|
| 214 |
def get_template(self, predicate, filter): |
| 215 |
# filter was included, for discussions see: |
| 216 |
# Issue: https://github.com/ansible-collections/community.general/issues/9278 |
| 217 |
# PR: https://github.com/ansible-collections/community.general/pull/9547 |
| 218 |
# the other two parameters are used for pagination, -1 for both essentially means "return all" |
| 219 |
filter_values = {"user_primary_group": -4, "user": -3, "all": -2, "user_groups": -1} |
| 220 |
pool = self.one.templatepool.info(filter_values[filter], -1, -1) |
| 221 |
|
| 222 |
for template in pool.VMTEMPLATE: |
| 223 |
if predicate(template): |
| 224 |
return template |
| 225 |
|
| 226 |
return None |
| 227 |
|
| 228 |
def get_template_by_id(self, template_id, filter): |
| 229 |
return self.get_template(lambda template: template_id == template.ID, filter) |
| 230 |
|
| 231 |
def get_template_by_name(self, name, filter): |
| 232 |
return self.get_template(lambda template: name == template.NAME, filter) |
| 233 |
|
| 234 |
def get_template_instance(self, requested_id, requested_name, filter): |
| 235 |
if requested_id: |
| 236 |
return self.get_template_by_id(requested_id, filter) |
| 237 |
else: |
| 238 |
return self.get_template_by_name(requested_name, filter) |
| 239 |
|
| 240 |
def get_template_info(self, template): |
| 241 |
info = { |
| 242 |
"id": template.ID, |
| 243 |
"name": template.NAME, |
| 244 |
"template": template.TEMPLATE, |
| 245 |
"user_name": template.UNAME, |
| 246 |
"user_id": template.UID, |
| 247 |
"group_name": template.GNAME, |
| 248 |
"group_id": template.GID, |
| 249 |
} |
| 250 |
|
| 251 |
return info |
| 252 |
|
| 253 |
def create_template(self, name, template_data, filter): |
| 254 |
if not self.module.check_mode: |
| 255 |
self.one.template.allocate(f'NAME = "{name}"\n{template_data}') |
| 256 |
|
| 257 |
result = self.get_template_info(self.get_template_by_name(name, filter)) |
| 258 |
result["changed"] = True |
| 259 |
|
| 260 |
return result |
| 261 |
|
| 262 |
def update_template(self, template, template_data, filter): |
| 263 |
if not self.module.check_mode: |
| 264 |
# 0 = replace the whole template |
| 265 |
self.one.template.update(template.ID, template_data, 0) |
| 266 |
|
| 267 |
result = self.get_template_info(self.get_template_by_id(template.ID, filter)) |
| 268 |
if self.module.check_mode: |
| 269 |
# Unfortunately it is not easy to detect if the template would have changed, therefore always report a change here. |
| 270 |
result["changed"] = True |
| 271 |
else: |
| 272 |
# if the previous parsed template data is not equal to the updated one, this has changed |
| 273 |
result["changed"] = result["template"] != template.TEMPLATE |
| 274 |
|
| 275 |
return result |
| 276 |
|
| 277 |
def delete_template(self, template): |
| 278 |
if not template: |
| 279 |
return {"changed": False} |
| 280 |
|
| 281 |
if not self.module.check_mode: |
| 282 |
self.one.template.delete(template.ID) |
| 283 |
|
| 284 |
return {"changed": True} |
| 285 |
|
| 286 |
|
| 287 |
def main(): |
| 288 |
TemplateModule().run_module() |
| 289 |
|
| 290 |
|
| 291 |
if __name__ == "__main__": |
| 292 |
main() |