using ${path.module} in a module, with a variable

1,289 views
Skip to first unread message

ja...@fpcomplete.com

unread,
Jun 5, 2016, 11:12:18 AM6/5/16
to Terraform
Let's say you are using `"${path.module}/init.tpl"` similar to the example in https://www.terraform.io/docs/providers/template/r/file.html, (though in my case I am using https://www.terraform.io/docs/provisioners/file.html), but you are doing this in a module, and you want that path to be a variable. You cannot use ${path.module} in a variable definition, you'll get something like: * Variable 'post_init_script_path': cannot contain interpolations

How should this be done? I'd like the module to default to a script that is included in the module, but I want the user/caller of the module to have the option to provide a path to their own script.

David Adams

unread,
Jun 5, 2016, 2:33:24 PM6/5/16
to terrafo...@googlegroups.com
This is a good case for `coalesce()`. I believe this pattern will do what you're looking for:

    variable "template-path" { default = "" }
    variable "name" {}

    resource "template_file" "the-template" {
      template = "${file(coalesce(var.template-path, "${path.module}/default.tpl"))}"
      vars = {
        name = "${var.name}"
      }
    }

    output "rendered-template" {
      value = "${template_file.the-template.rendered}"
    }

Then when calling the module, you can just skip specifying `template-file` to use the default.

-dave


On Sun, Jun 5, 2016 at 10:12 AM, <ja...@fpcomplete.com> wrote:
Let's say you are using `"${path.module}/init.tpl"` similar to the example in https://www.terraform.io/docs/providers/template/r/file.html, (though in my case I am using https://www.terraform.io/docs/provisioners/file.html), but you are doing this in a module, and you want that path to be a variable. You cannot use ${path.module} in a variable definition, you'll get something like: * Variable 'post_init_script_path': cannot contain interpolations

How should this be done? I'd like the module to default to a script that is included in the module, but I want the user/caller of the module to have the option to provide a path to their own script.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/a307c667-7428-4daf-b640-cca1c8cc3354%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ja...@fpcomplete.com

unread,
Jun 5, 2016, 8:39:36 PM6/5/16
to Terraform


On Sunday, June 5, 2016 at 2:33:24 PM UTC-4, David Adams wrote:
This is a good case for `coalesce()`. I believe this pattern will do what you're looking for:

Ah yes! I had not seen `coalesce()` until now, thanks for pointing me to that, it looks like it fits perfectly. I will give it a try.
Reply all
Reply to author
Forward
0 new messages