# win\_Shell or win\_command executing a variable taken from win\_reg\_stat

**URL:** <https://forum.ansible.com/t/win-shell-or-win-command-executing-a-variable-taken-from-win-reg-stat/26276>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [December 13, 2017, 9:32am UTC](https://forum.ansible.com/t/win-shell-or-win-command-executing-a-variable-taken-from-win-reg-stat/26276 "2017-12-13T09:32:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gareth\_Stockdale](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Gareth\_Stockdale](https://forum.ansible.com/u/Gareth_Stockdale)\
**Post date:** [December 13, 2017, 9:32am UTC](https://forum.ansible.com/t/win-shell-or-win-command-executing-a-variable-taken-from-win-reg-stat/26276/1 "2017-12-13T09:32:14Z")

</div>

I have some vendor software where I am trying to uninstall (following their procedure) by obtaining the uninstall command and install location from the windows registry. I do this by querying win\_reg\_stat and storing result in two facts. I am then trying to execute the variable via win\_shell and win\_command in various guises but with no success. Even stripping it back to the bare uninstall command with no parameters is proving difficult eg

win\_shell: “{{ uninstall }}”  
args:  
executable: cmd

or

win\_command: “{{ uninstall}}”

Worth noting, the registry variable contain spaces in the directory path(eg C:\Program Files x86\blah\blah\uninstall).

I have tried various combinations of ", ’ characters but seem unsuccessful.

Anyone got a working example of executing a DOS command when defined as a variable with spaces in its directory path?

Thanks  
Gareth

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [December 13, 2017, 9:47am UTC](https://forum.ansible.com/t/win-shell-or-win-command-executing-a-variable-taken-from-win-reg-stat/26276/2 "2017-12-13T09:47:45Z")

</div>

We would probably need some more information and not just a blank variable called uninstall. You can omit names but keep the structure and arguments the same so we can see what exactly is the value from the registry key. Are you also able to share the registry path you are querying as you might be able to use win\_package for this instead.

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Gareth\_Stockdale](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Gareth\_Stockdale](https://forum.ansible.com/u/Gareth_Stockdale)\
**Post date:** [December 13, 2017, 9:52am UTC](https://forum.ansible.com/t/win-shell-or-win-command-executing-a-variable-taken-from-win-reg-stat/26276/3 "2017-12-13T09:52:29Z")

</div>

OK so I go the basic command working with…

win\_command: ‘“{{ uninstall}}”’

Now working on the extra arguments

---

<div class="post-metadata">

**Author:** ![Gareth\_Stockdale](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Gareth\_Stockdale](https://forum.ansible.com/u/Gareth_Stockdale)\
**Post date:** [December 13, 2017, 10:30am UTC](https://forum.ansible.com/t/win-shell-or-win-command-executing-a-variable-taken-from-win-reg-stat/26276/4 "2017-12-13T10:30:02Z")

</div>

SO the problem I have now is if I try and add args eg

win\_shell: ‘“{{uninstall}}” /S \_?=“{{installloc}}”’

It fails to run it with “unable to exec C:\Program.exe” - ie the single quote i had around the “{{uninstall}” previously have moved around the entire command, but it now has issue with whitepsace in {{ uninstall }} variable path.

If I put the single qoute back around the {{uninstall}} with parameters after…

win\_shell: ‘“{{uninstall}}”’ /S \_?=“{{installloc}}”

I get a syntax error on the /S (even with qouting attempt on this parameters).

Any ideas anyone?

Thanks

---

<div class="post-metadata">

**Author:** ![Gareth\_Stockdale](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Gareth\_Stockdale](https://forum.ansible.com/u/Gareth_Stockdale)\
**Post date:** [December 13, 2017, 10:32am UTC](https://forum.ansible.com/t/win-shell-or-win-command-executing-a-variable-taken-from-win-reg-stat/26276/5 "2017-12-13T10:32:57Z")

</div>

HI Jordan,

Example uninstall path:

C:\Program Files (x86)\My Software\It Is Mine\myagent\uninstall.exe

The full command woudl be…

C:\Program Files (x86)\My Software\It Is Mine\myagent\uninstall.exe /S \_?=C:\Program Files (x86)\My Software\It Is Mine\myagent

I will take a look at win\_package but initial reading suggest I also need to find the product ID associated, so will look into that.

Thanks  
Gareth

---

<div class="post-metadata">

**Author:** ![Gareth\_Stockdale](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Gareth\_Stockdale](https://forum.ansible.com/u/Gareth_Stockdale)\
**Post date:** [December 13, 2017, 11:37am UTC](https://forum.ansible.com/t/win-shell-or-win-command-executing-a-variable-taken-from-win-reg-stat/26276/6 "2017-12-13T11:37:00Z")

</div>

Appears the vendor is not storing productID in the expected path ☹

ON a different tact to avoid the extra single quoting on the executable, I tried obtaining the windows short path for the file. win\_stat does not provide it. So I ran a successful win\_shell cmd as follows:

win\_shell: cmd /c for %A in ("{{ uninstall }}") do @echo %~sA  
args:  
executable: cmd

…however in the stdout it is not returning the shorthand version like when I do it manually on a dos prompt ☹

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [December 17, 2017, 4:24pm UTC](https://forum.ansible.com/t/win-shell-or-win-command-executing-a-variable-taken-from-win-reg-stat/26276/7 "2017-12-17T16:24:11Z")

</div>

You can pass win\_shell a chdir in the ‘args’ section as well if that helps (may not if a full path is needed).

Feel free to go back to your vendor and ask them how their automated testing works if its such a pain to install/uninstall their product. The only way we will get better quality installers is if vendors get badgered into making them.

cheers,

Jon

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [December 17, 2017, 8:34pm UTC](https://forum.ansible.com/t/win-shell-or-win-command-executing-a-variable-taken-from-win-reg-stat/26276/8 "2017-12-17T20:34:28Z")

</div>

While you can use win\_shell I find it is better to use win\_command when it comes to running executables, this way you don’t fall prey to the shell specific escaping, e.g. when using win\_shell you usually have to do it like;

- win\_shell: &“C:\Program Files\someapp\app.exe”

compared to

- win\_command: ‘“C:\Program Files\someapp\app.exe”’

This is because win\_shell will automatically run the param in PowerShell as an encoded command and when a quoted value is run in powershell it will just print it to the screen, the & tells it to execute it. You read more about this here [http://docs.ansible.com/ansible/devel/windows\_usage.html#running-commands](http://docs.ansible.com/ansible/devel/windows_usage.html#running-commands).

For your specific situation I would try (note you would need some sort of when clause to skip this task if it is already uninstalled);

- name: uninstall application  
win\_command: ‘“C:\Program Files (x86)\My Software\It Is Mine\myagent\uninstall.exe” /S “\_?=C:\Program Files (x86)\My Software\It Is Mine\myagent”’

In this example, I have

- put the entire win\_command value inside single quotes that tells the Ansible yml parser this is all one value and ignore things like double quotes, backslashes and other escaping chars
- used double quotes to enclose the first argument which is the path to the executable as well as the 3rd argument which also has spaces in it
- I didn’t use jinja2 blocks but if you need to substitute the path, leave the quoting as is as it is very important they stay like that, e.g. make it like

- win\_command: ‘“{{ uninstall\_path }}” /S “\_?={{ unisntall\_path }}”’

In the end Windows will see this as

argv[0] = C:\Program Files (x86)\My Software\It Is Mine\myagent\uninstall.exe  
argv[1] = /S  
argv[2] = \_?=C:\Program Files (x86)\My Software\It Is Mine\myagent

Where argv[0] is the path to the executable that is used to start the process. This follows Microsoft standard rules for argument parsing and in the application entry point, the argv array above is what will be passed in so it should work. Hopefully this helps you with what you need to do.

Thanks

Jordan
