Terraform 无法逃脱单引号

Terraform 无法逃脱单引号

我试图转义字符串中的单引号:

${join("\n",formatlist("%s ansible_host=%s ansible_ssh_common_args='-o ProxyCommand=\"ssh -W %%h:%%p -q cloud-user@%s\"'","${module.compute.ops_master_names}","${module.compute.ops_master_priv_ips}","${module.ips.bastion_fips[0]}"))}"

我尝试了不同的组合(\' 或 \\' 或 '' 或 ' ),但收到了非法字符转义,或者没有打印单引号。我需要打印该行

ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q cloud-user@%s"'

双引号和百分号字符被很好地解释

答案1

不需要转义,以下对我有用:

locals {
  test = ["foo", "bar"]
}

output "test" {
  value = "${formatlist("ansible_ssh_common_args='-o ProxyCommand=\"ssh -W %%h:%%p -q cloud-user@%s\"'", local.test)}"
}

返回:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

test = [
    ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q cloud-user@foo"',
    ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q cloud-user@bar"'
]

使用template_file也有效:

locals {
  test = ["foo", "bar"]
}

data "template_file" "inventory" {
  template = <<-EOT
    $${test}
  EOT

  vars {
    test = "${join("\n", formatlist("ansible_ssh_common_args='-o ProxyCommand=\"ssh -W %%h:%%p -q cloud-user@%s\"'", local.test))}"
  }
}

output "test" {
  value = "${data.template_file.inventory.rendered}"
}

返回:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

test =   ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q cloud-user@foo"'
ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q cloud-user@bar"'

单引号仍然存在。

这是 Terraform 0.11.6。

相关内容