From 81a04b844ea3d23fbbb81c55c605aaf5a8553420 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Wed, 30 Oct 2024 14:26:32 +0100 Subject: [PATCH] clickhouse_cfg_info: add file-related error handling --- plugins/modules/clickhouse_cfg_info.py | 13 +++++++++---- .../targets/clickhouse_cfg_info/tasks/initial.yml | 12 ++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/plugins/modules/clickhouse_cfg_info.py b/plugins/modules/clickhouse_cfg_info.py index 8458e62..c412900 100644 --- a/plugins/modules/clickhouse_cfg_info.py +++ b/plugins/modules/clickhouse_cfg_info.py @@ -66,10 +66,15 @@ ) -def load_from_yaml(path): - with open(path, 'r') as f: +def load_from_yaml(module, path): + try: + f = open(path, 'r') content = yaml.safe_load(f) - return content + except Exception as e: + module.fail_json(msg="Could not open/load YAML from the file %s: %s" % (path, e)) + else: + f.close() + return content def main(): @@ -87,7 +92,7 @@ def main(): if not HAS_PYYAML: module.fail_json(msg=missing_required_lib('pyyaml')) - cfg_content = load_from_yaml(module.params['path']) + cfg_content = load_from_yaml(module, module.params['path']) # Users will get this in JSON output after execution module.exit_json(changed=False, **cfg_content) diff --git a/tests/integration/targets/clickhouse_cfg_info/tasks/initial.yml b/tests/integration/targets/clickhouse_cfg_info/tasks/initial.yml index 9e31118..16e4c26 100644 --- a/tests/integration/targets/clickhouse_cfg_info/tasks/initial.yml +++ b/tests/integration/targets/clickhouse_cfg_info/tasks/initial.yml @@ -18,3 +18,15 @@ that: - result['http_port'] == 8123 - result['grpc']['compression_level'] == 'medium' + +- name: Try to retreive info from a non-existing file + register: result + ignore_errors: true + community.clickhouse.clickhouse_cfg_info: + path: /tmp/non-existing.yaml + +- name: Assert failure + ansible.builtin.assert: + that: + - result is failed + - result.msg is search('Could not open/load YAML from the file')