Re: Running exe on windows from a batch file

2,207 views
Skip to first unread message
Message has been deleted

Zimidar Boy

unread,
Nov 8, 2017, 1:49:50 PM11/8/17
to Ansible Project
Can you please run playbook by using -vvv verbose mode. It will give more clarity of error.

On Wednesday, November 8, 2017 at 1:23:57 PM UTC-5, lpesc...@google.com wrote:
HI folks,
I have copied a batch file from ansible to windows and want to run it. 
If i go to the windows machine and run it it works fine, but when I run it from playbook it fails. 

Here is the playbook:


---

  - name: test script module

    hosts: builders

    tasks:

      - name: run simple script

        win_command: C:\Users\pkmbuilder\Desktop\configure_builder.bat




And here is the batch:

::Install cygwin

setup-x86_64.exe --root C:\cygwin64 --quiet-mode --site http://cygwin.mirror.constant.com --packages "openssh,rsync,zip,vim,wget" || exit /b 1


When running via cli I get


root@Raspi_Ctrl:/ansible/playbook# ansible-playbook /ansible/playbook/batch.yml -i /ansible/hosts


PLAY [test script module] ******************************************************


TASK [Gathering Facts] *********************************************************

ok: [100.]


TASK [run simple script] *******************************************************

fatal: [100.6]: FAILED! => {"changed": true, "cmd": "C:\\Users\\pkmbuilder\\Desktop\\configure_builder.bat", "delta": "0:00:00.304048", "end": "2017-11-09 02:22:46.953264", "msg": "non-zero return code", "rc": 1, "start": "2017-11-09 02:22:46.649216", "stderr": "'setup-x86_64.exe' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n", "stderr_lines": ["'setup-x86_64.exe' is not recognized as an internal or external command,", "operable program or batch file."], "stdout": "\r\nC:\\Users\\pkmbuilder>setup-x86_64.exe --root C:\\cygwin64 --quiet-mode --site http://cygwin.mirror.constant.com --packages \"openssh,rsync,zip,vim,wget\"   || exit /b 1 \r\n", "stdout_lines": ["", "C:\\Users\\pkmbuilder>setup-x86_64.exe --root C:\\cygwin64 --quiet-mode --site http://cygwin.mirror.constant.com --packages \"openssh,rsync,zip,vim,wget\"   || exit /b 1 "]}

to retry, use: --limit @/ansible/playbook/batch.retry


PLAY RECAP *********************************************************************

100.            : ok=1    changed=0    unreachable=0    failed=1   


What variables need I set to get this running?


Thanks!




Zimidar Boy

unread,
Nov 8, 2017, 1:53:53 PM11/8/17
to Ansible Project
Also try this batch file. https://gist.github.com/wjrogers/1016065
Message has been deleted

Jordan Borean

unread,
Nov 8, 2017, 5:18:02 PM11/8/17
to Ansible Project
Where is the setup-x86_64.exe located? By default commands run from win_command and win_shell are run in the user's home directory which means it will be trying to execute C:\Users\pkmbuilder\setup-x86_64.exe. If the exe is located in another directory you will either have to reference the full/relative path from that user's home directory or change the working directory on the task. To change the working directory to another path (the example is C:\temp) you can set the task as;

- win_command: cmd.exe /c dir
  args
:
    chdir
: C:\temp

Thanks

Jordan
Message has been deleted
Message has been deleted

Jordan Borean

unread,
Nov 8, 2017, 7:06:05 PM11/8/17
to Ansible Project
Personally, I would bypass the batch file and just call the executable directly like

- name: install Cygwin
  win_command: C:\Users\pkmbuilder\setup-x86_64.exe --root C:\cygwin64 --quiet-mode --site http://cygwin.mirror.constant.com --packages "openssh,rsync,zip,vim,wget"

# Note if you are using an Ansible version before 2.4, change "openssh,rsync,zip,vim,wget" to \"openssh,rsync,zip,vim,wget\". If you are on 2.4 keep it as it is

This way you aren't reliant on a file being present on the server and are bypassing cmd which could obfuscate some results. You can also use win_package but you would need to determine the product id before hand but doing so would give you some idempotency.

Thanks

Jordan
Message has been deleted
Message has been deleted

J Hawkesworth

unread,
Nov 9, 2017, 2:54:50 PM11/9/17
to Ansible Project
Hey,

Not sure if either of these are the answer but a couple of things you can try..

Try using win_shell instead of win_command. I think it may handle args differently (because it is running inside a shell).

Another thing you could try is using with_items to pass each of the packages you are installing separately, thereby avoiding the comma in your command line. Probably a bit slower but if it works it might be good enough for you to move on to the next thing.

Hope this helps,

Jon

Jordan Borean

unread,
Nov 9, 2017, 3:15:46 PM11/9/17
to Ansible Project
I don't think win_shell is necessary, you are truly trying to run an executable and don't want to confuse issues that may be related to how the shell handles things and how a command handles it. In your case you will need to enclose the full command with a single quote ', you tried this but I think forgot to close the string with a ' hence Ansible telling you that there was inbalanced quotes. I just tried the following on a brand new server and it was successful in install Cygwin.

- hosts: '2012R2'
gather_facts: no
tasks:
- name: download Cygwin installer
win_get_url:
dest: C:\temp\setup-x86_64.exe

- name: install Cygwin
win_command: 'C:\temp\setup-x86_64.exe --root C:\cygwin64 --quiet-mode --site http://cygwin.mirror.constant.com --packages "openssh,rsync,zip,vim,wget,nano"'


As you can see, the whole string for win_command is enclosed with ' and it has allowed me to continue to use " and \ without escaping anything. Here you can see Cygwin is installed and up and running




Thanks


Jordan

Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages