Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indent function in template file failed to render #16775

Closed
sebastien-prudhomme opened this issue Nov 28, 2017 · 4 comments
Closed

Indent function in template file failed to render #16775

sebastien-prudhomme opened this issue Nov 28, 2017 · 4 comments

Comments

@sebastien-prudhomme
Copy link

sebastien-prudhomme commented Nov 28, 2017

Terraform Version

Terraform v0.11.0 and Terraform v.0.10.8

Terraform Configuration Files

resource "tls_private_key" "ca" {
  algorithm = "RSA"
  rsa_bits  = "2048"
}

data "template_file" "config-yaml" {
  template = "${file("${path.module}/config.yaml.tpl")}"

  vars {
    ca_rsa_private = "${tls_private_key.ca.private_key_pem}"
  }
}

config.yaml.tpl:

ca_rsa_private: |
  ${indent(2, ca_rsa_private)}

Expected Behavior

No error

Actual Behavior

Got this error:

failed to render : 2:5: unknown function called: indent

Important Factoids

If i use the indent function directly in the TF file, it works fine.

data "template_file" "config-yaml" {
  template = "${file("${path.module}/config.yaml.tpl")}"

  vars {
    ca_rsa_private = "${indent(2, tls_private_key.ca.private_key_pem)}"
  }
}

config.yaml.tpl:

ca_rsa_private: |
  ${ca_rsa_private}
@apparentlymart
Copy link
Contributor

Hi @sebastien-prudhomme,

Unfortunately this is the expected behavior today: the template_file interpolation context doesn't include Terraform's interpolation functions, since it's an entirely separate system.

We have plans in future to make templates a more "first-class" idea in Terraform, rather than a separate provider, and that would allow them to integrate more deeply with Terraform's value scope and interpolation functions. Unfortunately there's a fair amount of work we need to get done before we can do that, since it'll interact with the new version of the configuration language we're integrating currently.

In the mean time I think the best way to do this is to inline the "template" inside your config as a local value definition. This is not ideal because it doesn't allow your editor to see the result as YAML, but it allows the use of interpolation functions and avoids the need for defining a separate set of variables:

locals {
  config_yaml = <<EOT
ca_rsa_private: |
  ${indent(2, tls_private_key.ca.private_key_pem)}
EOT
}

Alternatively, you could exploit the fact that YAML accepts JSON syntax and instead assign this YAML attribute in JSON, at the expense of readability of the resulting YAML file:

ca_rsa_private: ${ca_rsa_private_json}
data "template_file" "config-yaml" {
  template = "${file("${path.module}/config.yaml.tpl")}"

  vars {
    ca_rsa_private_json = "${jsonencode(tls_private_key.ca.private_key_pem)}"
  }
}

...though indeed this isn't really much better than your example of passing in the already-indented string, except that it at least doesn't need to hard-code a specific indentation level and thus decouples the YAML file layout from the Terraform config.

@hashibot
Copy link
Contributor

hashibot commented Dec 5, 2017

This issue has been automatically migrated to hashicorp/terraform-provider-template#23 because it looks like an issue with that provider. If you believe this is not an issue with the provider, please reply to this issue and let us know.

@hashibot hashibot closed this as completed Dec 5, 2017
@sebastien-prudhomme
Copy link
Author

Thanks for the explanation @apparentlymart , but perhaps the doc should be corrected: https://www.terraform.io/docs/configuration/interpolation.html

You may use any of the built-in functions in your template

@ghost
Copy link

ghost commented Apr 5, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants