2

I am trying to escape a single quote in my string:

${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]}"))}"

I have tried with different combinations (\' or \\' or '' or ' ), but I received an illegal char escape or it doesn't print the single quote. my need is print the line

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

the double quote and percentage character are well interpreted

Henry TheKing
  • 69
  • 2
  • 3
  • 8

1 Answers1

0

No escaping is necessary, the following works for me:

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

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

Returns:

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"'
]

Using a template_file also works:

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}"
}

Returns:

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"'

Single quotes are still present.

This is with Terraform 0.11.6.

bodgit
  • 4,661
  • 13
  • 26
  • Maybe the problem is that I am using a template to print the content in a file and it doesn't work. `data "template_file" "inventory" { template = "${file("${path.module}/inventory.tpl")}" vars { connection_strings_master = "${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]}"))}" } }` – Henry TheKing Jan 17 '19 at 16:01
  • Maybe sounds crazy, but I had similiar problems although not with Terraform. Can you try putting single quote inside single quotes? Like: ''' – titus Jan 17 '19 at 16:05
  • Using a `template_file` resource still works fine for me, no escaping necessary. – bodgit Jan 17 '19 at 16:13