| 1 |
#!/usr/bin/python |
| 2 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 |
|
| 4 |
# Copyright © Thorsten Glaser <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
--- |
| 11 |
module: kea_command |
| 12 |
short_description: Submits generic command to ISC KEA server on target |
| 13 |
description: |
| 14 |
- Submits a command to the JSON API of an ISC KEA server running on the target and obtains the result. |
| 15 |
- This module supports sending arbitrary commands and returns the server response unchecked; |
| 16 |
while it would be possible to write individual modules for specific KEA service commands, |
| 17 |
that approach would not scale, as the FOSS hooks alone provide dozens of commands. |
| 18 |
- Between sending the command and parsing the result status, RV(ignore:changed) registers as V(true) if an error occurs, |
| 19 |
to err on the safe side. |
| 20 |
version_added: '12.0.0' |
| 21 |
author: Thorsten Glaser (@mirabilos) |
| 22 |
options: |
| 23 |
command: |
| 24 |
description: |
| 25 |
- The name of the command to send, for example V(status-get). |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
arguments: |
| 29 |
description: |
| 30 |
- The arguments sent along with the command, if any. |
| 31 |
- Use V({}) to send an empty arguments dict/object instead of omitting it. |
| 32 |
type: dict |
| 33 |
rv_unchanged: |
| 34 |
description: |
| 35 |
- A list of C(result) codes to indicate success but unchanged system state. |
| 36 |
- Set this to V([0]) for most acquisition commands. |
| 37 |
- Use V([3]) for O(command=lease4-del) and similar which have a separate code for this. |
| 38 |
- Any C(result) codes not listed in either O(rv_unchanged) or O(rv_changed) are interpreted as indicating an error result. |
| 39 |
- O(rv_unchanged) has precedence over O(rv_changed) if a result code is in both lists. |
| 40 |
type: list |
| 41 |
elements: int |
| 42 |
default: [] |
| 43 |
rv_changed: |
| 44 |
description: |
| 45 |
- A list of C(result) codes to indicate success and changed system state. |
| 46 |
- Omit this for most acquisition commands. |
| 47 |
- Set it to V([0]) for O(command=lease4-del) and similar which return changed system state that way. |
| 48 |
- Any C(result) codes not listed in either O(rv_unchanged) or O(rv_changed) are interpreted as indicating an error result. |
| 49 |
- O(rv_unchanged) has precedence over O(rv_changed) if a result code is in both lists. |
| 50 |
type: list |
| 51 |
elements: int |
| 52 |
default: [] |
| 53 |
socket: |
| 54 |
description: |
| 55 |
- The full pathname of the Unix Domain Socket to connect to. |
| 56 |
- The default value is suitable for C(kea-dhcp4-server) on Debian trixie. |
| 57 |
- This module directly interfaces using UDS; the HTTP wrappers are not supported. |
| 58 |
type: path |
| 59 |
default: /run/kea/kea4-ctrl-socket |
| 60 |
extends_documentation_fragment: |
| 61 |
- community.general._attributes |
| 62 |
- community.general._attributes.platform |
| 63 |
attributes: |
| 64 |
check_mode: |
| 65 |
support: none |
| 66 |
diff_mode: |
| 67 |
support: none |
| 68 |
platform: |
| 69 |
support: full |
| 70 |
platforms: posix |
| 71 |
""" |
| 72 |
|
| 73 |
EXAMPLES = r""" |
| 74 |
vars: |
| 75 |
ipaddr: "192.168.123.45" |
| 76 |
hwaddr: "00:00:5E:00:53:00" |
| 77 |
tasks: |
| 78 |
|
| 79 |
# an example for a request acquiring information |
| 80 |
- name: Get KEA DHCP6 status |
| 81 |
kea_command: |
| 82 |
command: status-get |
| 83 |
rv_unchanged: [0] |
| 84 |
socket: /run/kea/kea6-ctrl-socket |
| 85 |
register: kea6_status |
| 86 |
- name: Display registered status result |
| 87 |
ansible.builtin.debug: |
| 88 |
msg: KEA DHCP6 running on PID {{ kea6_status.response.arguments.pid }} |
| 89 |
|
| 90 |
# an example for requests modifying state |
| 91 |
- name: Remove existing leases for {{ ipaddr }}, if any |
| 92 |
kea_command: |
| 93 |
command: lease4-del |
| 94 |
arguments: |
| 95 |
ip-address: "{{ ipaddr }}" |
| 96 |
rv_changed: [0] |
| 97 |
rv_unchanged: [3] |
| 98 |
- name: Add DHCP lease for {{ ipaddr }} |
| 99 |
kea_command: |
| 100 |
command: lease4-add |
| 101 |
arguments: |
| 102 |
ip-address: "{{ ipaddr }}" |
| 103 |
hw-address: "{{ hwaddr }}" |
| 104 |
rv_changed: [0] |
| 105 |
""" |
| 106 |
|
| 107 |
RETURN = r""" |
| 108 |
response: |
| 109 |
description: The server JSON response. |
| 110 |
returned: when available |
| 111 |
type: dict |
| 112 |
""" |
| 113 |
|
| 114 |
import json |
| 115 |
import os |
| 116 |
import socket |
| 117 |
import traceback |
| 118 |
|
| 119 |
from ansible.module_utils.basic import AnsibleModule |
| 120 |
|
| 121 |
# default buffer size for socket I/O |
| 122 |
BUFSIZ = 8192 |
| 123 |
|
| 124 |
|
| 125 |
def _parse_constant(s): |
| 126 |
raise ValueError(f'Invalid JSON: "{s}"') |
| 127 |
|
| 128 |
|
| 129 |
def main(): |
| 130 |
module = AnsibleModule( |
| 131 |
argument_spec=dict( |
| 132 |
command=dict(type="str", required=True), |
| 133 |
arguments=dict(type="dict"), |
| 134 |
rv_unchanged=dict(type="list", elements="int", default=[]), |
| 135 |
rv_changed=dict(type="list", elements="int", default=[]), |
| 136 |
socket=dict(type="path", default="/run/kea/kea4-ctrl-socket"), |
| 137 |
), |
| 138 |
) |
| 139 |
|
| 140 |
cmd = {} |
| 141 |
cmd["command"] = module.params["command"] |
| 142 |
if module.params["arguments"] is not None: |
| 143 |
cmd["arguments"] = module.params["arguments"] |
| 144 |
cmdstr = json.dumps(cmd, ensure_ascii=True, allow_nan=False, indent=None, separators=(",", ":"), sort_keys=True) |
| 145 |
rvok = module.params["rv_unchanged"] |
| 146 |
rvch = module.params["rv_changed"] |
| 147 |
sockfn = module.params["socket"] |
| 148 |
|
| 149 |
r = {"changed": False} |
| 150 |
rsp = b"" |
| 151 |
|
| 152 |
if not os.path.exists(sockfn): |
| 153 |
r["msg"] = f"socket ({sockfn}) does not exist" |
| 154 |
module.fail_json(**r) |
| 155 |
|
| 156 |
phase = "opening" |
| 157 |
try: |
| 158 |
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock: |
| 159 |
phase = "connecting" |
| 160 |
sock.connect(sockfn) |
| 161 |
# better safe in case anything fails… |
| 162 |
r["changed"] = True |
| 163 |
phase = "writing" |
| 164 |
sock.sendall(cmdstr.encode("ASCII")) |
| 165 |
phase = "reading" |
| 166 |
while True: |
| 167 |
rspnew = sock.recv(BUFSIZ) |
| 168 |
if len(rspnew) == 0: |
| 169 |
break |
| 170 |
rsp += rspnew |
| 171 |
phase = "closing" |
| 172 |
except OSError as ex: |
| 173 |
r["msg"] = f"error {phase} socket ({sockfn}): {ex}" |
| 174 |
r["exception"] = traceback.format_exc() |
| 175 |
module.fail_json(**r) |
| 176 |
|
| 177 |
# 15 is the length of the minimum response {"response":0} as formatted by KEA |
| 178 |
if len(rsp) < 15: |
| 179 |
r["msg"] = f"unrealistically short response {rsp!r}" |
| 180 |
module.fail_json(**r) |
| 181 |
|
| 182 |
try: |
| 183 |
r["response"] = json.loads(rsp, parse_constant=_parse_constant) |
| 184 |
except ValueError as ex: |
| 185 |
r["msg"] = f"error parsing JSON response: {ex}" |
| 186 |
r["exception"] = traceback.format_exc() |
| 187 |
module.fail_json(**r) |
| 188 |
if not isinstance(r["response"], dict): |
| 189 |
r["msg"] = "bogus JSON response (JSONObject expected)" |
| 190 |
module.fail_json(**r) |
| 191 |
if "result" not in r["response"]: |
| 192 |
r["msg"] = "bogus JSON response (missing result)" |
| 193 |
module.fail_json(**r) |
| 194 |
res = r["response"]["result"] |
| 195 |
if not isinstance(res, int): |
| 196 |
r["msg"] = "bogus JSON response (non-integer result)" |
| 197 |
module.fail_json(**r) |
| 198 |
|
| 199 |
if res in rvok: |
| 200 |
r["changed"] = False |
| 201 |
elif res not in rvch: |
| 202 |
r["msg"] = f"failure result (code {res})" |
| 203 |
module.fail_json(**r) |
| 204 |
|
| 205 |
module.exit_json(**r) |
| 206 |
|
| 207 |
|
| 208 |
if __name__ == "__main__": |
| 209 |
main() |