| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2018, Dag Wieers (dagwieers) <[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 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: cobbler_system |
| 11 |
short_description: Manage system objects in Cobbler |
| 12 |
description: |
| 13 |
- Add, modify or remove systems in Cobbler. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: full |
| 21 |
options: |
| 22 |
host: |
| 23 |
description: |
| 24 |
- The name or IP address of the Cobbler system. |
| 25 |
default: 127.0.0.1 |
| 26 |
type: str |
| 27 |
port: |
| 28 |
description: |
| 29 |
- Port number to be used for REST connection. |
| 30 |
- The default value depends on parameter O(use_ssl). |
| 31 |
type: int |
| 32 |
username: |
| 33 |
description: |
| 34 |
- The username to log in to Cobbler. |
| 35 |
default: cobbler |
| 36 |
type: str |
| 37 |
password: |
| 38 |
description: |
| 39 |
- The password to log in to Cobbler. |
| 40 |
type: str |
| 41 |
use_ssl: |
| 42 |
description: |
| 43 |
- If V(false), an HTTP connection is used instead of the default HTTPS connection. |
| 44 |
type: bool |
| 45 |
default: true |
| 46 |
validate_certs: |
| 47 |
description: |
| 48 |
- If V(false), SSL certificates are not validated. |
| 49 |
- This should only set to V(false) when used on personally controlled sites using self-signed certificates. |
| 50 |
type: bool |
| 51 |
default: true |
| 52 |
name: |
| 53 |
description: |
| 54 |
- The system name to manage. |
| 55 |
type: str |
| 56 |
properties: |
| 57 |
description: |
| 58 |
- A dictionary with system properties. |
| 59 |
type: dict |
| 60 |
interfaces: |
| 61 |
description: |
| 62 |
- A list of dictionaries containing interface options. |
| 63 |
type: dict |
| 64 |
sync: |
| 65 |
description: |
| 66 |
- Sync on changes. |
| 67 |
- Concurrently syncing Cobbler is bound to fail. |
| 68 |
type: bool |
| 69 |
default: false |
| 70 |
state: |
| 71 |
description: |
| 72 |
- Whether the system should be present, absent or a query is made. |
| 73 |
choices: [absent, present, query] |
| 74 |
default: present |
| 75 |
type: str |
| 76 |
author: |
| 77 |
- Dag Wieers (@dagwieers) |
| 78 |
notes: |
| 79 |
- Concurrently syncing Cobbler is bound to fail with weird errors. |
| 80 |
""" |
| 81 |
|
| 82 |
EXAMPLES = r""" |
| 83 |
- name: Ensure the system exists in Cobbler |
| 84 |
community.general.cobbler_system: |
| 85 |
host: cobbler01 |
| 86 |
username: cobbler |
| 87 |
password: MySuperSecureP4sswOrd |
| 88 |
name: myhost |
| 89 |
properties: |
| 90 |
profile: CentOS6-x86_64 |
| 91 |
name_servers: [2.3.4.5, 3.4.5.6] |
| 92 |
name_servers_search: foo.com, bar.com |
| 93 |
interfaces: |
| 94 |
eth0: |
| 95 |
macaddress: 00:01:02:03:04:05 |
| 96 |
ipaddress: 1.2.3.4 |
| 97 |
delegate_to: localhost |
| 98 |
|
| 99 |
- name: Enable network boot in Cobbler |
| 100 |
community.general.cobbler_system: |
| 101 |
host: bdsol-aci-cobbler-01 |
| 102 |
username: cobbler |
| 103 |
password: ins3965! |
| 104 |
name: bdsol-aci51-apic1.cisco.com |
| 105 |
properties: |
| 106 |
netboot_enabled: true |
| 107 |
state: present |
| 108 |
delegate_to: localhost |
| 109 |
|
| 110 |
- name: Query all systems in Cobbler |
| 111 |
community.general.cobbler_system: |
| 112 |
host: cobbler01 |
| 113 |
username: cobbler |
| 114 |
password: MySuperSecureP4sswOrd |
| 115 |
state: query |
| 116 |
register: cobbler_systems |
| 117 |
delegate_to: localhost |
| 118 |
|
| 119 |
- name: Query a specific system in Cobbler |
| 120 |
community.general.cobbler_system: |
| 121 |
host: cobbler01 |
| 122 |
username: cobbler |
| 123 |
password: MySuperSecureP4sswOrd |
| 124 |
name: '{{ inventory_hostname }}' |
| 125 |
state: query |
| 126 |
register: cobbler_properties |
| 127 |
delegate_to: localhost |
| 128 |
|
| 129 |
- name: Ensure the system does not exist in Cobbler |
| 130 |
community.general.cobbler_system: |
| 131 |
host: cobbler01 |
| 132 |
username: cobbler |
| 133 |
password: MySuperSecureP4sswOrd |
| 134 |
name: myhost |
| 135 |
state: absent |
| 136 |
delegate_to: localhost |
| 137 |
""" |
| 138 |
|
| 139 |
RETURN = r""" |
| 140 |
systems: |
| 141 |
description: List of systems. |
| 142 |
returned: O(state=query) and O(name) is not provided |
| 143 |
type: list |
| 144 |
system: |
| 145 |
description: (Resulting) information about the system we are working with. |
| 146 |
returned: when O(name) is provided |
| 147 |
type: dict |
| 148 |
""" |
| 149 |
|
| 150 |
import ssl |
| 151 |
import xmlrpc.client as xmlrpc_client |
| 152 |
|
| 153 |
from ansible.module_utils.basic import AnsibleModule |
| 154 |
|
| 155 |
from ansible_collections.community.general.plugins.module_utils._datetime import ( |
| 156 |
now, |
| 157 |
) |
| 158 |
|
| 159 |
IFPROPS_MAPPING = dict( |
| 160 |
bondingopts="bonding_opts", |
| 161 |
bridgeopts="bridge_opts", |
| 162 |
connected_mode="connected_mode", |
| 163 |
cnames="cnames", |
| 164 |
dhcptag="dhcp_tag", |
| 165 |
dnsname="dns_name", |
| 166 |
ifgateway="if_gateway", |
| 167 |
interfacetype="interface_type", |
| 168 |
interfacemaster="interface_master", |
| 169 |
ipaddress="ip_address", |
| 170 |
ipv6address="ipv6_address", |
| 171 |
ipv6defaultgateway="ipv6_default_gateway", |
| 172 |
ipv6mtu="ipv6_mtu", |
| 173 |
ipv6prefix="ipv6_prefix", |
| 174 |
ipv6secondaries="ipv6_secondariesu", |
| 175 |
ipv6staticroutes="ipv6_static_routes", |
| 176 |
macaddress="mac_address", |
| 177 |
management="management", |
| 178 |
mtu="mtu", |
| 179 |
netmask="netmask", |
| 180 |
static="static", |
| 181 |
staticroutes="static_routes", |
| 182 |
virtbridge="virt_bridge", |
| 183 |
) |
| 184 |
|
| 185 |
|
| 186 |
def getsystem(conn, name, token): |
| 187 |
system = dict() |
| 188 |
if name: |
| 189 |
# system = conn.get_system(name, token) |
| 190 |
systems = conn.find_system(dict(name=name), token) |
| 191 |
if systems: |
| 192 |
system = systems[0] |
| 193 |
return system |
| 194 |
|
| 195 |
|
| 196 |
def main(): |
| 197 |
module = AnsibleModule( |
| 198 |
argument_spec=dict( |
| 199 |
host=dict(type="str", default="127.0.0.1"), |
| 200 |
port=dict(type="int"), |
| 201 |
username=dict(type="str", default="cobbler"), |
| 202 |
password=dict(type="str", no_log=True), |
| 203 |
use_ssl=dict(type="bool", default=True), |
| 204 |
validate_certs=dict(type="bool", default=True), |
| 205 |
name=dict(type="str"), |
| 206 |
interfaces=dict(type="dict"), |
| 207 |
properties=dict(type="dict"), |
| 208 |
sync=dict(type="bool", default=False), |
| 209 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 210 |
), |
| 211 |
supports_check_mode=True, |
| 212 |
) |
| 213 |
|
| 214 |
username = module.params["username"] |
| 215 |
password = module.params["password"] |
| 216 |
port = module.params["port"] |
| 217 |
use_ssl = module.params["use_ssl"] |
| 218 |
validate_certs = module.params["validate_certs"] |
| 219 |
|
| 220 |
name = module.params["name"] |
| 221 |
state = module.params["state"] |
| 222 |
|
| 223 |
proto = "https" if use_ssl else "http" |
| 224 |
if not port: |
| 225 |
port = "443" if use_ssl else "80" |
| 226 |
|
| 227 |
result = dict( |
| 228 |
changed=False, |
| 229 |
) |
| 230 |
|
| 231 |
start = now() |
| 232 |
|
| 233 |
ssl_context = None if validate_certs or not use_ssl else ssl._create_unverified_context() |
| 234 |
|
| 235 |
url = f"{proto}://{module.params['host']}:{port}/cobbler_api" |
| 236 |
conn = xmlrpc_client.ServerProxy(url, context=ssl_context) |
| 237 |
|
| 238 |
try: |
| 239 |
token = conn.login(username, password) |
| 240 |
except xmlrpc_client.Fault as e: |
| 241 |
module.fail_json(msg=f"Failed to log in to Cobbler '{url}' as '{username}'. {e}") |
| 242 |
except Exception as e: |
| 243 |
module.fail_json(msg=f"Connection to '{url}' failed. {e}") |
| 244 |
|
| 245 |
system = getsystem(conn, name, token) |
| 246 |
# result['system'] = system |
| 247 |
|
| 248 |
if state == "query": |
| 249 |
if name: |
| 250 |
result["system"] = system |
| 251 |
else: |
| 252 |
# Turn it into a dictionary of dictionaries |
| 253 |
# all_systems = conn.get_systems() |
| 254 |
# result['systems'] = { system['name']: system for system in all_systems } |
| 255 |
|
| 256 |
# Return a list of dictionaries |
| 257 |
result["systems"] = conn.get_systems() |
| 258 |
|
| 259 |
elif state == "present": |
| 260 |
if system: |
| 261 |
# Update existing entry |
| 262 |
system_id = "" |
| 263 |
# https://github.com/cobbler/cobbler/blame/v3.3.7/cobbler/api.py#L277 |
| 264 |
if float(conn.version()) >= 3.4: |
| 265 |
system_id = conn.get_system_handle(name) |
| 266 |
else: |
| 267 |
system_id = conn.get_system_handle(name, token) |
| 268 |
|
| 269 |
for key, value in module.params["properties"].items(): |
| 270 |
if key not in system: |
| 271 |
module.warn(f"Property '{key}' is not a valid system property.") |
| 272 |
if system[key] != value: |
| 273 |
try: |
| 274 |
conn.modify_system(system_id, key, value, token) |
| 275 |
result["changed"] = True |
| 276 |
except Exception as e: |
| 277 |
module.fail_json(msg=f"Unable to change '{key}' to '{value}'. {e}") |
| 278 |
|
| 279 |
else: |
| 280 |
# Create a new entry |
| 281 |
system_id = conn.new_system(token) |
| 282 |
conn.modify_system(system_id, "name", name, token) |
| 283 |
result["changed"] = True |
| 284 |
|
| 285 |
if module.params["properties"]: |
| 286 |
for key, value in module.params["properties"].items(): |
| 287 |
try: |
| 288 |
conn.modify_system(system_id, key, value, token) |
| 289 |
except Exception as e: |
| 290 |
module.fail_json(msg=f"Unable to change '{key}' to '{value}'. {e}") |
| 291 |
|
| 292 |
# Add interface properties |
| 293 |
interface_properties = dict() |
| 294 |
if module.params["interfaces"]: |
| 295 |
for device, values in module.params["interfaces"].items(): |
| 296 |
for key, value in values.items(): |
| 297 |
if key == "name": |
| 298 |
continue |
| 299 |
if key not in IFPROPS_MAPPING: |
| 300 |
module.warn(f"Property '{key}' is not a valid system property.") |
| 301 |
continue |
| 302 |
if not system or system["interfaces"].get(device, {}).get(IFPROPS_MAPPING[key]) != value: |
| 303 |
result["changed"] = True |
| 304 |
interface_properties[f"{key}-{device}"] = value |
| 305 |
|
| 306 |
if result["changed"] is True: |
| 307 |
conn.modify_system(system_id, "modify_interface", interface_properties, token) |
| 308 |
|
| 309 |
# Only save when the entry was changed |
| 310 |
if not module.check_mode and result["changed"]: |
| 311 |
conn.save_system(system_id, token) |
| 312 |
|
| 313 |
elif state == "absent": |
| 314 |
if system: |
| 315 |
if not module.check_mode: |
| 316 |
conn.remove_system(name, token) |
| 317 |
result["changed"] = True |
| 318 |
|
| 319 |
if not module.check_mode and module.params["sync"] and result["changed"]: |
| 320 |
try: |
| 321 |
conn.sync(token) |
| 322 |
except Exception as e: |
| 323 |
module.fail_json(msg=f"Failed to sync Cobbler. {e}") |
| 324 |
|
| 325 |
if state in ("absent", "present"): |
| 326 |
result["system"] = getsystem(conn, name, token) |
| 327 |
|
| 328 |
if module._diff: |
| 329 |
result["diff"] = dict(before=system, after=result["system"]) |
| 330 |
|
| 331 |
elapsed = now() - start |
| 332 |
module.exit_json(elapsed=elapsed.seconds, **result) |
| 333 |
|
| 334 |
|
| 335 |
if __name__ == "__main__": |
| 336 |
main() |