Hi @HugoSecteur4!
The result you’re seeing here is confusing to me. The first log line reports this:
Executing: ["cmd" "/C" "echo \"Hello World\""]
The backslashes we see here are a result of Terraform presenting the argument in quotes itself and thus escaping the ones in the string for display only. So the literal value of that argument as seen by the command interpreter should be echo "Hello World"
, as you wanted.
However, internally then Terraform must call the CreateProcess
Windows API function to launch the command, because you seem to be using Windows. The interface of that function requires adding the quotes back, because on Windows command arguments are passed as a string, so the effective call to the Windows API would then be as follows:
-
lpApplicationName
as cmd
-
lpCommandLine
as cmd /C "echo \"Hello World\""
The escaping backslashes must be re-introduced here because the second argument to cmd
contains spaces and must therefore be quoted itself.
What normally happens next on Windows is that the command interpreter (cmd.exe
) parses the third argument and itself calls CreateProcess
to run the given program, and then dealing with the quotes in the remaining arguments is up to that final program. However, echo
is a special command built in to the command interpreter, so in this case the command interpreter would interpret the command line directly itself.
My suspicion is that the strange result you see here is due to the fact that echo
is a command interpreter builtin and so it doesn’t process its arguments in the standard way: it just takes verbatim what it is given as a single string, rather than tokenizing it into several separate arguments as would normally happen when running an external program.
With all of that said, I think this odd behavior is likely unique to the echo
command in particular, and that if you were running a standard Windows command line application you’d see the behavior you expected.
However, I don’t have a Windows system handy to test, so it’s equally likely that I’m misreading the situation. I’d suggest trying this out with whatever actual program you need to run to see if you get a different result. Unfortunately on Windows not all commands/applications comply with the standard convention for command line processing, and in that case those programs may not be compatible with Terraform’s local-exec
provisioner unless you write a wrapper program (which could e.g. be just a .cmd
script, or anything else) that can then pass the arguments in the unique way that program is expecting.