Given this block of pseudo code from Terraform:
resource "null_resource" {
provisioner "local-exec" {
command = "echo hello"
interpreter = local.os == "Windows" ? ["PowerShell", "-Command"] : ["bash"]
}
How would I get this to run in Powershell on Windows and bash on Linux?
You can examine the path where where your terraform project is running. On Windows it will be something similar to
C:/Users/
whereas on linux, the standard path will be/home/xxx
.local-exec
usage inherently draws in details of the platform where you are running Terraform, which is one of the reasons why it should be treated as a last resort.Terraform does not include any built-in way for a module to detect the host operating system. A custom Terraform provider could potentially offer a data source which returns that information, although if one were writing a Terraform provider anyway it may be more appropriate to write one to do whatever operation this provisioner would've been running directly, and thus avoid the need for the Terraform configuration to branch based on operating system.