Skip to content

Commit

Permalink
clickhouse_cfg_info: add file-related error handling (ansible-collect…
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersson007 authored Nov 1, 2024
1 parent 5e5df44 commit 9e297b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
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')

0 comments on commit 9e297b5

Please sign in to comment.