get windows-path and execute command

hi,
i want to use “get-command” to get the location of a program. in the next step i want to execute this program with a full qualified location.
this example is just an example, i know there are other modules for doing a ping.

i use
(get-command ping).source
to get the location. in the next step i want to execute this with a parameter:

  • name: get pingpath
    win_shell: “(get-command ping).source”)
    register: pingpath
  • name test ping
    win_shell: “{{ pingpath }} 9.9.9.9”

that throws an error because of an
unexpected token: ":"

any idea how to fix this?

regards,
andre

I’d start by adding a debug step to show what you actually registered:

- name: Show what got registered in pingpath
  ansible.builtin.debug:
    msg: "{{ pingpath }}"

Given that using it that way produced unexpected results, perhaps it’s because the registered data aren’t what you expect.

This will be because you are using the full result from win_shell which includes things like changed, rc, stdout, stderr, etc. You need to do `- win_shell: “{{ pingpath.stdout |trim }} 9.9.9.9” and say you want to use the stdout value of the result which contains the full path. The | trim is also needed to trim the newline the stdout result will have.

Putting that aside what is the purpose here, it’s inefficient because you are now using 2 tasks which involves setting up the connection and running a command when you can just achieve it normally in one task and rely on the normal PATH lookup. It also is more complicated because you might need to quote the value if the path contains a space in it.

  • win_command: ping.exe 9.9.9.9

perfect, that did the trick.
thx a lot!

regards,
andre