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: ":"
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.