| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2016, Hugh Ma <[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: stacki_host |
| 11 |
short_description: Add or remove host to stacki front-end |
| 12 |
description: |
| 13 |
- Use this module to add or remove hosts to a stacki front-end using API. |
| 14 |
- Information on stacki can be found at U(https://github.com/StackIQ/stacki). |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: none |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Name of the host to be added to Stacki. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
stacki_user: |
| 29 |
description: |
| 30 |
- Username for authenticating with Stacki API, but if not specified, the environment variable E(stacki_user) is used |
| 31 |
instead. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
stacki_password: |
| 35 |
description: |
| 36 |
- Password for authenticating with Stacki API, but if not specified, the environment variable E(stacki_password) is |
| 37 |
used instead. |
| 38 |
required: true |
| 39 |
type: str |
| 40 |
stacki_endpoint: |
| 41 |
description: |
| 42 |
- URL for the Stacki API Endpoint. |
| 43 |
required: true |
| 44 |
type: str |
| 45 |
prim_intf_mac: |
| 46 |
description: |
| 47 |
- MAC Address for the primary PXE boot network interface. |
| 48 |
- Currently not used by the module. |
| 49 |
type: str |
| 50 |
prim_intf_ip: |
| 51 |
description: |
| 52 |
- IP Address for the primary network interface. |
| 53 |
- Currently not used by the module. |
| 54 |
type: str |
| 55 |
prim_intf: |
| 56 |
description: |
| 57 |
- Name of the primary network interface. |
| 58 |
- Currently not used by the module. |
| 59 |
type: str |
| 60 |
force_install: |
| 61 |
description: |
| 62 |
- Set value to V(true) to force node into install state if it already exists in stacki. |
| 63 |
type: bool |
| 64 |
default: false |
| 65 |
state: |
| 66 |
description: |
| 67 |
- Set value to the desired state for the specified host. |
| 68 |
type: str |
| 69 |
choices: [absent, present] |
| 70 |
default: present |
| 71 |
appliance: |
| 72 |
description: |
| 73 |
- Appliance to be used in host creation. |
| 74 |
- Required if O(state=present) and host does not yet exist. |
| 75 |
type: str |
| 76 |
default: backend |
| 77 |
rack: |
| 78 |
description: |
| 79 |
- Rack to be used in host creation. |
| 80 |
- Required if O(state=present) and host does not yet exist. |
| 81 |
type: int |
| 82 |
default: 0 |
| 83 |
rank: |
| 84 |
description: |
| 85 |
- Rank to be used in host creation. |
| 86 |
- In Stacki terminology, the rank is the position of the machine in a rack. |
| 87 |
- Required if O(state=present) and host does not yet exist. |
| 88 |
type: int |
| 89 |
default: 0 |
| 90 |
network: |
| 91 |
description: |
| 92 |
- Network to be configured in the host. |
| 93 |
- Currently not used by the module. |
| 94 |
type: str |
| 95 |
default: private |
| 96 |
author: |
| 97 |
- Hugh Ma (@bbyhuy) <[email protected]> |
| 98 |
""" |
| 99 |
|
| 100 |
EXAMPLES = r""" |
| 101 |
- name: Add a host named test-1 |
| 102 |
community.general.stacki_host: |
| 103 |
name: test-1 |
| 104 |
stacki_user: usr |
| 105 |
stacki_password: pwd |
| 106 |
stacki_endpoint: url |
| 107 |
prim_intf_mac: mac_addr |
| 108 |
prim_intf_ip: x.x.x.x |
| 109 |
prim_intf: eth0 |
| 110 |
|
| 111 |
- name: Remove a host named test-1 |
| 112 |
community.general.stacki_host: |
| 113 |
name: test-1 |
| 114 |
stacki_user: usr |
| 115 |
stacki_password: pwd |
| 116 |
stacki_endpoint: url |
| 117 |
state: absent |
| 118 |
""" |
| 119 |
|
| 120 |
|
| 121 |
import json |
| 122 |
from urllib.parse import urlencode |
| 123 |
|
| 124 |
from ansible.module_utils.basic import AnsibleModule, env_fallback |
| 125 |
from ansible.module_utils.urls import fetch_url |
| 126 |
|
| 127 |
|
| 128 |
class StackiHost: |
| 129 |
def __init__(self, module): |
| 130 |
self.module = module |
| 131 |
self.hostname = module.params["name"] |
| 132 |
self.rack = module.params["rack"] |
| 133 |
self.rank = module.params["rank"] |
| 134 |
self.appliance = module.params["appliance"] |
| 135 |
self.prim_intf = module.params["prim_intf"] |
| 136 |
self.prim_intf_ip = module.params["prim_intf_ip"] |
| 137 |
self.network = module.params["network"] |
| 138 |
self.prim_intf_mac = module.params["prim_intf_mac"] |
| 139 |
self.endpoint = module.params["stacki_endpoint"] |
| 140 |
|
| 141 |
auth_creds = {"USERNAME": module.params["stacki_user"], "PASSWORD": module.params["stacki_password"]} |
| 142 |
|
| 143 |
# Get Initial CSRF |
| 144 |
cred_a = self.do_request(self.endpoint, method="GET") |
| 145 |
cookie_a = cred_a.headers.get("Set-Cookie").split(";") |
| 146 |
init_csrftoken = None |
| 147 |
for c in cookie_a: |
| 148 |
if "csrftoken" in c: |
| 149 |
init_csrftoken = c.replace("csrftoken=", "") |
| 150 |
init_csrftoken = init_csrftoken.rstrip("\r\n") |
| 151 |
break |
| 152 |
|
| 153 |
# Make Header Dictionary with initial CSRF |
| 154 |
header = { |
| 155 |
"csrftoken": init_csrftoken, |
| 156 |
"X-CSRFToken": init_csrftoken, |
| 157 |
"Content-type": "application/x-www-form-urlencoded", |
| 158 |
"Cookie": cred_a.headers.get("Set-Cookie"), |
| 159 |
} |
| 160 |
|
| 161 |
# Endpoint to get final authentication header |
| 162 |
login_endpoint = f"{self.endpoint}/login" |
| 163 |
|
| 164 |
# Get Final CSRF and Session ID |
| 165 |
login_req = self.do_request(login_endpoint, headers=header, payload=urlencode(auth_creds), method="POST") |
| 166 |
|
| 167 |
cookie_f = login_req.headers.get("Set-Cookie").split(";") |
| 168 |
csrftoken = None |
| 169 |
for f in cookie_f: |
| 170 |
if "csrftoken" in f: |
| 171 |
csrftoken = f.replace("csrftoken=", "") |
| 172 |
if "sessionid" in f: |
| 173 |
sessionid = c.split("sessionid=", 1)[-1] |
| 174 |
sessionid = sessionid.rstrip("\r\n") |
| 175 |
|
| 176 |
self.header = { |
| 177 |
"csrftoken": csrftoken, |
| 178 |
"X-CSRFToken": csrftoken, |
| 179 |
"sessionid": sessionid, |
| 180 |
"Content-type": "application/json", |
| 181 |
"Cookie": login_req.headers.get("Set-Cookie"), |
| 182 |
} |
| 183 |
|
| 184 |
def do_request(self, url, payload=None, headers=None, method=None): |
| 185 |
res, info = fetch_url(self.module, url, data=payload, headers=headers, method=method) |
| 186 |
|
| 187 |
if info["status"] != 200: |
| 188 |
self.module.fail_json(changed=False, msg=info["msg"]) |
| 189 |
|
| 190 |
return res |
| 191 |
|
| 192 |
def stack_check_host(self): |
| 193 |
res = self.do_request( |
| 194 |
self.endpoint, payload=json.dumps({"cmd": "list host"}), headers=self.header, method="POST" |
| 195 |
) |
| 196 |
return self.hostname in res.read() |
| 197 |
|
| 198 |
def stack_sync(self): |
| 199 |
self.do_request(self.endpoint, payload=json.dumps({"cmd": "sync config"}), headers=self.header, method="POST") |
| 200 |
self.do_request( |
| 201 |
self.endpoint, payload=json.dumps({"cmd": "sync host config"}), headers=self.header, method="POST" |
| 202 |
) |
| 203 |
|
| 204 |
def stack_force_install(self, result): |
| 205 |
data = {"cmd": f"set host boot {self.hostname} action=install"} |
| 206 |
self.do_request(self.endpoint, payload=json.dumps(data), headers=self.header, method="POST") |
| 207 |
changed = True |
| 208 |
|
| 209 |
self.stack_sync() |
| 210 |
|
| 211 |
result["changed"] = changed |
| 212 |
result["stdout"] = "api call successful".rstrip("\r\n") |
| 213 |
|
| 214 |
def stack_add(self, result): |
| 215 |
data = dict() |
| 216 |
changed = False |
| 217 |
|
| 218 |
data["cmd"] = f"add host {self.hostname} rack={self.rack} rank={self.rank} appliance={self.appliance}" |
| 219 |
self.do_request(self.endpoint, payload=json.dumps(data), headers=self.header, method="POST") |
| 220 |
|
| 221 |
self.stack_sync() |
| 222 |
|
| 223 |
result["changed"] = changed |
| 224 |
result["stdout"] = "api call successful".rstrip("\r\n") |
| 225 |
|
| 226 |
def stack_remove(self, result): |
| 227 |
data = dict() |
| 228 |
|
| 229 |
data["cmd"] = f"remove host {self.hostname}" |
| 230 |
self.do_request(self.endpoint, payload=json.dumps(data), headers=self.header, method="POST") |
| 231 |
|
| 232 |
self.stack_sync() |
| 233 |
|
| 234 |
result["changed"] = True |
| 235 |
result["stdout"] = "api call successful".rstrip("\r\n") |
| 236 |
|
| 237 |
|
| 238 |
def main(): |
| 239 |
module = AnsibleModule( |
| 240 |
argument_spec=dict( |
| 241 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 242 |
name=dict(type="str", required=True), |
| 243 |
rack=dict(type="int", default=0), |
| 244 |
rank=dict(type="int", default=0), |
| 245 |
appliance=dict(type="str", default="backend"), |
| 246 |
prim_intf=dict(type="str"), |
| 247 |
prim_intf_ip=dict(type="str"), |
| 248 |
network=dict(type="str", default="private"), |
| 249 |
prim_intf_mac=dict(type="str"), |
| 250 |
stacki_user=dict(type="str", required=True, fallback=(env_fallback, ["stacki_user"])), |
| 251 |
stacki_password=dict(type="str", required=True, fallback=(env_fallback, ["stacki_password"]), no_log=True), |
| 252 |
stacki_endpoint=dict(type="str", required=True, fallback=(env_fallback, ["stacki_endpoint"])), |
| 253 |
force_install=dict(type="bool", default=False), |
| 254 |
), |
| 255 |
supports_check_mode=False, |
| 256 |
) |
| 257 |
|
| 258 |
result = {"changed": False} |
| 259 |
missing_params = list() |
| 260 |
|
| 261 |
stacki = StackiHost(module) |
| 262 |
host_exists = stacki.stack_check_host() |
| 263 |
|
| 264 |
# If state is present, but host exists, need force_install flag to put host back into install state |
| 265 |
if module.params["state"] == "present" and host_exists and module.params["force_install"]: |
| 266 |
stacki.stack_force_install(result) |
| 267 |
# If state is present, but host exists, and force_install and false, do nothing |
| 268 |
elif module.params["state"] == "present" and host_exists and not module.params["force_install"]: |
| 269 |
result["stdout"] = f"{module.params['name']} already exists. Set 'force_install' to true to bootstrap" |
| 270 |
# Otherwise, state is present, but host doesn't exists, require more params to add host |
| 271 |
elif module.params["state"] == "present" and not host_exists: |
| 272 |
for param in ["appliance", "rack", "rank", "prim_intf", "prim_intf_ip", "network", "prim_intf_mac"]: |
| 273 |
if not module.params[param]: |
| 274 |
missing_params.append(param) |
| 275 |
if len(missing_params) > 0: |
| 276 |
module.fail_json(msg=f"missing required arguments: {missing_params}") |
| 277 |
|
| 278 |
stacki.stack_add(result) |
| 279 |
# If state is absent, and host exists, lets remove it. |
| 280 |
elif module.params["state"] == "absent" and host_exists: |
| 281 |
stacki.stack_remove(result) |
| 282 |
|
| 283 |
module.exit_json(**result) |
| 284 |
|
| 285 |
|
| 286 |
if __name__ == "__main__": |
| 287 |
main() |