Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clickhouse_cfg_info: add file-related error handling #85

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions plugins/modules/clickhouse_cfg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/targets/clickhouse_cfg_info/tasks/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Loading