win_Shell or win_command executing a variable taken from win_reg_stat

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

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

OK so I go the basic command working with…

win_command: ‘“{{ uninstall}}”’

Now working on the extra arguments

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

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

Appears the vendor is not storing productID in the expected path :frowning:

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 :frowning:

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

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.

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