diff --git a/cadoles/eole/plugins/modules/instance.py b/cadoles/eole/plugins/modules/instance.py index 3002a9d..721c7c2 100644 --- a/cadoles/eole/plugins/modules/instance.py +++ b/cadoles/eole/plugins/modules/instance.py @@ -335,24 +335,30 @@ def run_module(): for expectation_name, response in module.params['variables'].items(): expectations.set_expectation_response_by_name(expectation_name, response) - instance_process = pexpect.spawn(cmd, encoding='utf-8', timeout=60) + try: + instance_process = pexpect.spawn(cmd, encoding='utf-8', timeout=60) - some_index = 0 - while some_index < nb_expectations: - p = instance_process.expect(patterns) - if p == 0: - break - if p == 1: - print(f'Some missing expectations for {instance_process.before}{instance_process.after}') - break - pattern = patterns[p] - for expectation in expectations.get_expectations_by_pattern(patterns[p]): - if expectation.is_the_one(instance_process.before): - expectation.answer(instance_process) + some_index = 0 + while some_index < nb_expectations: + p = instance_process.expect(patterns) + if p == 0: break - some_index += 1 - result['changed'] = True - result['msg'] += f"Module {result['module']} instanciated" + if p == 1: + print(f'Some missing expectations for {instance_process.before}{instance_process.after}') + break + pattern = patterns[p] + for expectation in expectations.get_expectations_by_pattern(patterns[p]): + if expectation.is_the_one(instance_process.before): + expectation.answer(instance_process) + break + some_index += 1 + result['changed'] = True + result['msg'] += f"Module {result['module']} instanciated" + module.exit_json(**result) + except Exception as err: + result['msg'] += err + result['changed'] = True + module.fail_json(**result) @@ -369,7 +375,6 @@ def run_module(): # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results - module.exit_json(**result) def main():