Ensure exit_json returns failed = False

This is required for modules that may return a non-zero `rc` value for a
successful run, similar to #24865 for Windows fixing **win_chocolatey**.

We also disable the dependency on `rc` value only, even if `failed` was
set.

Adapted unit and integration tests to the new scheme.
Updated raw, shell, script, expect to take `rc` into account.
This commit is contained in:
Dag Wieers
2017-05-21 00:25:57 +02:00
committed by Toshio Kuratomi
parent 1f78715848
commit 0e160d5c7e
17 changed files with 68 additions and 56 deletions

View File

@@ -570,10 +570,13 @@ class TaskExecutor:
vars_copy.update(result['ansible_facts'])
vars_copy.update({'ansible_facts': result['ansible_facts']})
# set the failed property if the result has a non-zero rc. This will be
# overridden below if the failed_when property is set
if result.get('rc', 0) != 0:
result['failed'] = True
# set the failed property if it was missing.
if 'failed' not in result:
result['failed'] = False
# set the changed property if it was missing.
if 'changed' not in result:
result['changed'] = False
# if we didn't skip this task, use the helpers to evaluate the changed/
# failed_when properties

View File

@@ -2023,6 +2023,9 @@ class AnsibleModule(object):
assert 'msg' in kwargs, "implementation error -- msg to explain the error is required"
kwargs['failed'] = True
if not 'changed' in kwargs:
kwargs['changed'] = False
self.do_cleanup_files()
self._return_formatted(kwargs)
sys.exit(1)

View File

@@ -62,6 +62,10 @@ Function Exit-Json($obj)
$obj = @{ }
}
if (-not $obj.ContainsKey('changed')) {
Set-Attr $obj "changed" $false
}
echo $obj | ConvertTo-Json -Compress -Depth 99
Exit
}
@@ -88,6 +92,10 @@ Function Fail-Json($obj, $message = $null)
Set-Attr $obj "msg" $message
Set-Attr $obj "failed" $true
if (-not $obj.ContainsKey('changed')) {
Set-Attr $obj "changed" $false
}
echo $obj | ConvertTo-Json -Compress -Depth 99
Exit 1
}

View File

@@ -204,7 +204,7 @@ def main():
if err is None:
err = b('')
module.exit_json(
result = dict(
cmd = args,
stdout = out.rstrip(b("\r\n")),
stderr = err.rstrip(b("\r\n")),
@@ -216,5 +216,11 @@ def main():
warnings = warnings
)
if rc != 0:
module.fail_json(msg='non-zero return code', **result)
module.exit_json(**result)
if __name__ == '__main__':
main()

View File

@@ -221,7 +221,7 @@ def main():
if out is None:
out = ''
ret = dict(
result = dict(
cmd=args,
stdout=out.rstrip('\r\n'),
rc=rc,
@@ -231,11 +231,12 @@ def main():
changed=True,
)
if rc is not None:
module.exit_json(**ret)
else:
ret['msg'] = 'command exceeded timeout'
module.fail_json(**ret)
if rc is None:
module.fail_json(msg='command exceeded timeout', **result)
elif rc != 0:
module.fail_json(msg='non-zero return code', **result)
module.exit_json(**result)
if __name__ == '__main__':

View File

@@ -42,4 +42,8 @@ class ActionModule(ActionBase):
result['changed'] = True
if 'rc' in result and result['rc'] != 0:
result['failed'] = True
result['msg'] = 'non-zero return code'
return result

View File

@@ -93,4 +93,8 @@ class ActionModule(ActionBase):
result['changed'] = True
if 'rc' in result and result['rc'] != 0:
result['failed'] = True
result['msg'] = 'non-zero return code'
return result