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

element function on list of rendered templates fails with incorrect data #5828

Closed
gottwald opened this issue Mar 24, 2016 · 7 comments
Closed

Comments

@gottwald
Copy link

I am experimenting with terraform and found something that looks like a bug when using templates in combination with count and the element function.

I've got a var definition like this:

variable "mesos-master-hosts" {
  default = {
    "0" = "mesos-master1"
    "1" = "mesos-master2"
    "2" = "mesos-master3"
  }
}

And I render cloud instance start scripts as templates like this:

resource "template_file" "mesos_master_init" {
  count = "${length(keys(var.mesos-master-hosts))}"
  template = "${file("mesos-master-init.sh.tpl")}"
  vars {
    count_index = "${count.index}"
    zk_id = "${count.index + 1}"
    mesos_hostname = "${lookup(var.mesos-master-hosts, count.index)}"
    mesos_master_hosts = "${values(var.mesos-master-hosts)}"
    mesos_zk = "${format("zk://%s/mesos", join(",", formatlist("%s:2181", values(var.mesos-master-hosts))))}"
    marathon_zk = "${format("zk://%s/marathon", join(",", formatlist("%s:2181", values(var.mesos-master-hosts))))}"
    chronos_zk_hosts = "${join(",", formatlist("%s:2181", values(var.mesos-master-hosts)))}"
  }
}

And with that, I create GCE instances like so:

resource "google_compute_instance" "mesos-master" {
  count = "${length(values(var.mesos-master-hosts))}"
  name = "${lookup(var.mesos-master-hosts, count.index)}"

  # skipping a lot of other unimportant attributes like
  # machine_type, zone, tags, disk, network, etc

  metadata_startup_script = "${element(template_file.mesos_master_init.*.rendered, count.index)}"
}

Somehow the metadata_startup_script attribute is set to the value mesos-master1 here instead of the rendered template.
If I supply a rendered template by directly accessing the index of the list like metadata_startup_script = "${template_file.mesos_master_init.0.rendered}", then I get a rendered script like I intended to.

Is this a bug or am I doing something wrong?

Tested with Terraform v0.6.14
There is another Issue #5736 regarding count and templates that might be related.

@gottwald
Copy link
Author

Found the error:

mesos_master_hosts = "${values(var.mesos-master-hosts)}"

I accidently assigned a list to a template var instead of a string.
Everything works if I change it to something like this:

mesos_master_hosts = "${join(",", values(var.mesos-master-hosts))}"

Seems like handing a list to the template var completely throws off the template rendering.
I would have expected an error message instead of this behaviour.
Will leave this issue open for you to judge whether this needs to be changed or not.

@phinze
Copy link
Contributor

phinze commented Mar 25, 2016

Hi @gottwald - thanks for the report, and sorry for the confusing behavior here.

I believe the impending first-class list support will make this a proper error message in the near future.

Is that correct @jen20? If so, I think we can close this since we know it will be fixed up soon!

@panda87
Copy link

panda87 commented Apr 28, 2016

@gottwald Can you pls create a gist or something like that and present what 'mesos-master-init.sh.tpl' file contains?

Thanks

@gottwald
Copy link
Author

gottwald commented May 2, 2016

Don't have the original any more but something like this would do:

#!/bin/bash

echo "Other content.... foo bar"

echo "count_index: ${count_index}"
echo "zk_id: ${zk_id}"
echo "mesos_hostname: ${mesos_hostname}"
echo "mesos_master_hosts: ${mesos_master_hosts}"
echo "mesos_zk: ${mesos_zk}"
echo "marathon_zk: ${marathon_zk}"
echo "chronos_zk_hosts: ${chronos_zk_hosts}"

Was just referencing the vars, nothing fancy.

@panda87
Copy link

panda87 commented May 2, 2016

Thanks @gottwald

@mitchellh
Copy link
Contributor

This is a proper error message and handled with 0.7 now.

@ghost
Copy link

ghost commented Apr 20, 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 unassigned jen20 Apr 20, 2020
@ghost ghost locked and limited conversation to collaborators Apr 20, 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

5 participants