template_file data source that covers multiple templates

1,480 views
Skip to first unread message

John Parfitt

unread,
Feb 22, 2017, 5:55:58 PM2/22/17
to Terraform
i have about 10 different, unique userdata files, for separate instances. However I want to pass a common variable to all of them without creating 10 "template_file" resources. Something like this, which doesn't work, because it just sets "template" to whatever the last one is:

data "template_file" "userdata" {
    template = "${file("${path.module}/userdata_olympus.conf")}"
    template = "${file("${path.module}/userdata_vpn.conf")}"
    template = "${file("${path.module}/userdata_nat.conf")}"
    vars {
        domain_suffix = "${var.domain_suffix}"
    }
}

data "template_file" "userdata_puppetdb" {
    template = "${file("${path.module}/userdata_puppetdb.conf")}"
    vars {
        domain_suffix = "${var.domain_suffix}"
        puppetmaster_ip = "${aws_instance.puppet_server.private_ip}"
    }
}



Where the first block uses the common "domain_suffix" variable, and then any other resource would be for instances that require unique variables.

Tom Davidson

unread,
Feb 26, 2017, 11:49:35 AM2/26/17
to Terraform
would this do what your after, if not, can you explain more?:

template = "{join(",", file("${path.module}/userdata_olympus.conf"), file("${path.module}/userdata_vpn.conf"), file("${path.module}/userdata_nat.conf")}"

John Parfitt

unread,
Feb 27, 2017, 12:01:53 PM2/27/17
to Terraform
I'm just looking for some sort of regex, like: 

data "template_file" "userdata" {
    template = "${file("${path.module}/userdata_*.conf")}"
    vars {
        domain_suffix = "${var.domain_suffix}"
    }
}

so that all userdata files that use the "domain_suffix" variable are included. i don't want to have to create a template_file resource for each userdata (the userdata files are all slightly different, but they use the same variable).

John Parfitt

unread,
Mar 24, 2017, 5:44:24 PM3/24/17
to Terraform
bump

The best I can explain is that I have 10 instances, each instance has a different userdata file, but each userdata file shares a single, common variable that I want to pass to it.

userdata_sql.conf
userdata_nginx.conf
userdata_app.conf

For each of these I want to pass a variable using a single "template_file" resource that covers all of those userdata files.

I want to do this:

data "template_file" "userdata_files" {
  template = "${file("${path.module}/userdata_*.conf")}"
  
  vars {
    domain_suffix = "${var.domain_suffix}"
  }
}

Instead of this:

data "template_file" "userdata_sql" {
  template = "${file("${path.module}/userdata_sql.conf")}"
  
  vars {
    domain_suffix = "${var.domain_suffix}"
  }
}


data "template_file" "userdata_nginx" {
  template = "${file("${path.module}/userdata_nginx.conf")}"
  
  vars {
    domain_suffix = "${var.domain_suffix}"
  }
}


data "template_file" "userdata_app" {
  template = "${file("${path.module}/userdata_app.conf")}"
  
  vars {
    domain_suffix = "${var.domain_suffix}"
  }
}


Does that make sense?

John Parfitt

unread,
Mar 26, 2017, 7:51:56 PM3/26/17
to Terraform
Tom - that syntax didn't work and I couldn't get it to NOT error. I tried using "format" instead of "join" and i was able to get terraform to run, but it ended up giving every instance the same userdata.

On Sunday, February 26, 2017 at 8:49:35 AM UTC-8, Tom Davidson wrote:
Reply all
Reply to author
Forward
0 new messages