Windows become fails on tasks that ignore/catch any errors #30468
Labels
affects_2.4
This issue/PR affects Ansible v2.4
bug
This issue/PR relates to a bug.
support:core
This issue/PR relates to code supported by the Ansible Engineering Team.
windows
Windows community
Projects
Milestone
ISSUE TYPE
COMPONENT NAME
win_become
ANSIBLE VERSION
CONFIGURATION
Default
OS / ENVIRONMENT
Ansible Controller - MacOS
Ansible Remote - Windows (Server 2016 PSv5 and 2012 R2 PSv4 confirmed with the issue)
SUMMARY
When running with become on Windows and the module being called throws any error that is caught or ignored with
-ErrorAction SilentlyContinue
it will still fail with an RC value of 1.This seems to be due to the
$ps.HadErrors
at https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/shell/powershell.py#L138 is being set to true if any errors at all occurred in the execution.According to https://msdn.microsoft.com/en-us/library/system.management.automation.powershell.haderrors(v=vs.85).aspx
HasErrors
means Gets a value that indicates whether an error occurred while executing the pipeline. It seems like this also include errors that are caught intry {} catch {}
or cmdlet calls with-ErrorAction SilentlyContinue
when I would have assumed they would ignore them.In the example below I am using the setup module as it contains the following block where
Get-Command
will throw the exception but is ultimately caught will setHadErrors
totrue
.You can also verify it manually by running the following code through powershell on a Windows box.
Lastly you can simply a test module to produce this situation with the following module code
It seems like a return code of 1 is being set for normal (non-become) situations but the return code value is not used for error detection here unless it cannot parse the output as a JSON. The become wrapper actually checks the return code from the process and throws an exception if it doesn't equal 0.
STEPS TO REPRODUCE
Run the following command and target a WIndows host
ansible windows -m setup --become -e ansible_become_method=runas -e ansible_become_user=Administrator -e ansible_become_password=Password01
EXPECTED RESULTS
A successful run without any errors
ACTUAL RESULTS
The module will fail with the error
Note: the stdout value is the actual output json containing all the facts, has been omitted for brevity here.
PROPOSED SOLUTION
Use the following diff to only set the error code if the error steam contains at least 1 entry. I haven't had too much time to determine if this would have any other negative impacts but from an initial look it seems fine.
The text was updated successfully, but these errors were encountered: