Escaping backslash returns double backslash

Single backslash not returning

Resulting value should be a single backslash but that isn’t the case. Variable is passing double backslasshes.


terraform version

Terraform v1.1.7

on linux_amd64

+ provider registry.terraform.io/paultyng/sql v0.4.0


variable "username" {

  description = "username"

  type        = string

  default     = "domain\\username"

}

output "username" {

  value = var.username

}

Expected Output


terraform plan

Changes to Outputs:

  + username = "domain\username"

Actual Output


terraform plan

Changes to Outputs:

  + username = "domain\\username"

The output you show is correct, a quoted string is displayed and the backslash must be correctly escaped. The actual string only contains a single backslash.

That is not correct, the variable passed in the variable contains 2 backslashes instead of the one. If you escape a backslash two backslashes are passed. I did figure out a way around it with just templating the value to get it to return a single backslash.

Template file:
${domain_name}/${user_name}

data "template_file" "logon_template" {
  template = file("${path.module}/logon.key")
  vars = {
    user_name = local.username,
    domain_name   = local.domain
  }
}

output "username" {
  value = data.template_file.logon_template.rendered
}

I’m not sure exactly what you are referring to, taking the first example shown, the output is

% terraform output username
"domain\\username"

And viewing the raw data you can see a single backsalsh.

% tf output -raw username
domain\username

The state also contains "value": "domain\\username", with a single escaped backslash in the json data.

The string template in the new example is using a forward slash, so it will not need to be escaped at all.

Ah, the little things! Thanks for the insights into using the raw switch, the provider keeps indicating bad username string so I thought it was the backslashes.