From ef6741021c37f701b6aba5b90e19b8da381670a3 Mon Sep 17 00:00:00 2001 From: saltydk Date: Sat, 3 Feb 2024 13:41:48 +0100 Subject: [PATCH] ansible: load saltbox library folder --- defaults/ansible.cfg.default | 1 + library/find_open_port.py | 94 ----------------------------------- library/qbittorrent_passwd.py | 41 --------------- 3 files changed, 1 insertion(+), 135 deletions(-) delete mode 100644 library/find_open_port.py delete mode 100644 library/qbittorrent_passwd.py diff --git a/defaults/ansible.cfg.default b/defaults/ansible.cfg.default index 966f290830..c38f86dfb0 100644 --- a/defaults/ansible.cfg.default +++ b/defaults/ansible.cfg.default @@ -2,6 +2,7 @@ inventory = /srv/git/saltbox/inventories/local roles_path = roles:/srv/git/saltbox/roles:/srv/git/saltbox/resources/roles filter_plugins = /srv/git/saltbox/filter_plugins +library = /srv/git/saltbox/library log_path = ./sandbox.log callbacks_enabled = profile_tasks interpreter_python = /srv/ansible/venv/bin/python3 diff --git a/library/find_open_port.py b/library/find_open_port.py deleted file mode 100644 index df4072a333..0000000000 --- a/library/find_open_port.py +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/python - -from ansible.module_utils.basic import AnsibleModule -import subprocess -import json - -DOCUMENTATION = """ ---- -module: find_open_port -short_description: Find an available port in a given range -description: - - "This module finds an available port between a low and high bound." -options: - low_bound: - description: - - The lowest port to consider - required: true - high_bound: - description: - - The highest port to consider - required: true - protocol: - description: - - The protocol to consider: tcp, udp, or both - required: false - default: both -""" - -EXAMPLES = """ -- name: Find an available port - find_open_port: - low_bound: 5000 - high_bound: 6000 - protocol: tcp -""" - -def find_port(module, low_bound, high_bound, protocol): - try: - if high_bound <= low_bound: - module.fail_json(msg="High bound must be higher than low bound") - - # Generate sequence - seq = set(range(low_bound, high_bound + 1)) - - # Determine command based on protocol - if protocol == 'tcp': - cmd = "ss -Htan" - awk_cmd = "awk '{print $4}'" - elif protocol == 'udp': - cmd = "ss -Huan" - awk_cmd = "awk '{print $4}'" - else: # both - cmd = "ss -Htuan" - awk_cmd = "awk '{print $5}'" - - cmd += " | grep LISTEN | " + awk_cmd + " | grep -Eo '[0-9]+$' | sort -u" - - # Run command to get ports in use - ports_in_use = subprocess.check_output(cmd, shell=True) - ports_in_use = set(int(port) for port in ports_in_use.decode().split()) - - # Find available ports - available_ports = seq - ports_in_use - - # Check if there's at least one available port - if available_ports: - candidate = min(available_ports) - return False, {"port": candidate} - else: - return False, {"msg": "No available port found in the specified range"} - - except Exception as e: - module.fail_json(msg=str(e)) - -def main(): - module = AnsibleModule( - argument_spec=dict( - low_bound=dict(type='int', required=True), - high_bound=dict(type='int', required=True), - protocol=dict(type='str', default='both', choices=['tcp', 'udp', 'both']), - ), - supports_check_mode=True - ) - - is_error, result = find_port(module, module.params['low_bound'], module.params['high_bound'], module.params['protocol']) - - if not is_error: - module.exit_json(changed=False, meta=result) - else: - module.fail_json(msg="Error finding port", meta=result) - - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/library/qbittorrent_passwd.py b/library/qbittorrent_passwd.py deleted file mode 100644 index e583b9af5e..0000000000 --- a/library/qbittorrent_passwd.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/python -from ansible.module_utils.basic import AnsibleModule -import base64 -import hashlib -import os - -def qbittorrent_passwd(plain_passwd): - try: - ITERATIONS = 100_000 - SALT_SIZE = 16 - - salt = os.urandom(SALT_SIZE) - h = hashlib.pbkdf2_hmac("sha512", plain_passwd.encode(), salt, ITERATIONS) - return "@ByteArray({}:{})".format(base64.b64encode(salt).decode(), base64.b64encode(h).decode()) - except Exception as e: - raise ValueError(f"Error generating password hash: {str(e)}") - -def main(): - module_args = dict( - password=dict(type='str', required=True, no_log=True) - ) - - result = dict( - changed=False, - msg='' - ) - - module = AnsibleModule( - argument_spec=module_args, - supports_check_mode=True - ) - - try: - result['msg'] = qbittorrent_passwd(module.params['password']) - except ValueError as err: - module.fail_json(msg=str(err)) - - module.exit_json(**result) - -if __name__ == '__main__': - main()