Iam trying to return a negative return code from a PowerShell script (.ps1) to Ansible. However the -ve codes that the script returns get converted into a large +ve integer for some reason. The script works fine if the return codes are +ve integers.
BTW The large +ve integer can be calculated like so:
4294967197 = 4294967296-99 (where -99 is the return code from PowerShell and 4294967296 = 4 * 1024**3 )
Appreciate any help. Thanks!
Details:
- ansible version: 2.9.6
- PSVersion : 5.1.14393.3383
- Windows : Server 2016 version 1607 (OS Build 14393.3383)
- python : 2.7.5
Also if i try this from the Windows Command Prompt it works !:
C:\> powershell -NoProfile %USERPROFILE%\Documents\ps_file_create.ps1
C:\> echo %errorlevel%
-99
This is what I have tried .. everything commented in the ansible/ps scripts below is what i have tried( I have also tried win_shell and get same results )
PowerShell Script (ps_file_create.ps1 ):
$return_code = -99 ## createfile $FileName
#return $return_code
#exit $return_code
$host.SetShouldExit($return_code)
exit
Ansible code:
- name: Execute the PowerShell Script that returns a -ve return code
#win_command: powershell -NonInteractive -NoProfile Invoke-Expression -Command ".\ps_file_create.ps1"
#win_command: powershell -NonInteractive -NoProfile .\ps_file_create.ps1
#win_command: powershell -NonInteractive -NoProfile -file ".\ps_file_create.ps1"
#win_command: powershell -NonInteractive -NoProfile -Command ".\ps_file_create.ps1"
win_command: powershell .\ps_file_create.ps1
args:
chdir: '%USERPROFILE%\Documents'
register: win_ps_out
ignore_errors: true
- name: Print Results of tasks
debug:
msg:
- "Powershell Out put: {{ win_ps_out | to_nice_json}}"
Output:
TASK [Print Results of tasks]*****************************************************************
ok: [myserver.xyz.abc.com] =>
msg:
- |-
Powershell Out put: {
"changed": true,
"cmd": "powershell .\\ps_file_create.ps1",
"delta": "0:00:00.532040",
"end": "2020-04-20 01:32:30.515580",
"failed": true,
"msg": "non-zero return code",
"rc": 4294967197, ## Expected -99 ##
"start": "2020-04-20 01:32:29.983539",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}