From 28fa75e1deea11e19a969eea78c9fa2c32f196a9 Mon Sep 17 00:00:00 2001 From: Edoardo Putti Date: Thu, 13 Oct 2016 18:47:26 +0200 Subject: [PATCH] handle errors in read_file function --- library/container_file_read.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/library/container_file_read.py b/library/container_file_read.py index cca536b..f2e5015 100644 --- a/library/container_file_read.py +++ b/library/container_file_read.py @@ -22,11 +22,19 @@ options: - path of the file to check ''' -def read_file_in_container(path): - import json - with open(path, 'r') as lines: - print(json.dump({ key_data: lines.read()})) - return 0 +def read_file_in_container(args): + (path, module) = args + try: + with open(path, 'r') as lines: + module.exit_json( + path = path, + text = lines.read().strip('\n'), + ) + except IOError, e: + module.exit_json( + msg = e, + path = path, + ) def main(): module = AnsibleModule(