Apart from updating the Server OS to 2008 R2 the only way I know how to get 
the output of a batch script is to use win_shell while changing the active 
code page. An example of running a valid and failing command can be found

- name: create test batch file
>   win_copy:
>     dest: C:\temp\test.bat
>     content: |
>       @echo OFF
>       echo Hello World
>
 

- name: create bad batch file
>   win_copy:
>     dest: C:\temp\bad.bat
>     content: |
>       @echo OFF
>       echo stdout
>       echo stderr 1>&2
>       exit 1
>
 

- name: run test batch file
>   win_shell: chcp 437 | Out-Null; C:\temp\test.bat
>
 

- name: run bad test batch file
>   win_shell: chcp 437 | Out-Null; C:\temp\bad.bat


The output looks like the below 

PLAYBOOK: adhoc.yml 
> ******************************************************************************************************************************************************************************************************************************
> 1 plays in adhoc.yml
>
 

PLAY [adhoc stuff] 
> *******************************************************************************************************************************************************************************************************************************
> META: ran handlers
>
 

TASK [create test batch file] 
> ********************************************************************************************************************************************************************************************************************
> task path: /Users/jborean/dev/module-tester/adhoc.yml:7
> Friday 08 September 2017  09:20:59 +1000 (0:00:00.045)       0:00:00.045 
> ****** 
> ok: [Server2008] => {"changed": false, "checksum": 
> "994da01848ab043e362343ac6b95e0169215e510", "dest": "C:\\temp\\test.bat", 
> "failed": false, "operation": "file_copy", "original_basename": 
> "tmpivwi2x5e", "size": 27, "src": null }

 

TASK [create bad batch file] 
> *********************************************************************************************************************************************************************************************************************
> task path: /Users/jborean/dev/module-tester/adhoc.yml:14
> Friday 08 September 2017  09:21:00 +1000 (0:00:01.582)       0:00:01.628 
> ****** 
> ok: [Server2008] => {"changed": false, "checksum": 
> "a91d421034b3a977a02d1a229652fb7a542c16e5", "dest": "C:\\temp\\bad.bat", 
> "failed": false, "operation": "file_copy", "original_basename": 
> "tmp_5gq6hzc", "size": 27, "src": null}
>
 

TASK [run test batch file] 
> ***********************************************************************************************************************************************************************************************************************
> task path: /Users/jborean/dev/module-tester/adhoc.yml:22
> Friday 08 September 2017  09:21:02 +1000 (0:00:01.796)       0:00:03.424 
> ****** 
> changed: [Server2008] => {"changed": true, "cmd": "chcp 437| Out-Null; 
> C:\\temp\\test.bat", "delta": "0:00:00.528651", "end": "2017-09-07 
> 11:21:03.384382", "failed": false, "rc": 0, "start": "2017-09-07 
> 11:21:02.855730", "stderr": "", 

"stderr_lines": [], "stdout": "Hello World\r\n", "stdout_lines": ["Hello 
> World"]}
>
 

TASK [run bad test batch file] 
> *******************************************************************************************************************************************************************************************************************
> task path: /Users/jborean/dev/module-tester/adhoc.yml:25
> Friday 08 September 2017  09:21:04 +1000 (0:00:02.169)       0:00:05.594 
> ****** 
> fatal: [Server2008]: FAILED! => {"changed": true, "cmd": "chcp 437| 
> Out-Null; C:\\temp\\bad.bat", "delta": "0:00:00.372014", "end": "2017-09-07 
> 11:21:06.008062", "failed": true, "msg": "non-zero return code", "rc": 1, 
> "start": "2017-09-07 11:21:05.636047", "stderr": "stderr \r\n", 
> "stderr_lines": ["stderr "], "stdout": "Fail\r\n", "stdout_lines": ["Fail"]}
>
 

> PLAY RECAP 
> ***************************************************************************************************************************************************************************************************************************************
> Server2008                 : ok=3    changed=1    unreachable=0   
>  failed=1   
> Friday 08 September 2017  09:21:06 +1000 (0:00:02.098)       0:00:07.692 
> ****** 
>
 

=============================================================================== 
> run test batch file 
> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>  
> 2.17s
> /Users/jborean/dev/module-tester/adhoc.yml:22 
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> run bad test batch file 
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>  
> 2.10s
> /Users/jborean/dev/module-tester/adhoc.yml:25 
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> create bad batch file 
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>  
> 1.80s
> /Users/jborean/dev/module-tester/adhoc.yml:14 
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> create test batch file 
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>  
> 1.58s
> /Users/jborean/dev/module-tester/adhoc.yml:7 
> ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> Playbook run took 0 days, 0 hours, 0 minutes, 7 seconds


In the example you can see that it runs both a good and bad batch file and 
also get's the stdout and stderr streams. The reason why this works is that 
win_shell creates a new powershell process and before we run the batch 
script we change the codepage to 437 which is the US codepage so that we 
can retrieve the output.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/eaad4403-bc22-401a-8389-9005eef503e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to