{"action":"opened","issue":{"url":"https://api.github.com/repos/hashicorp/terraform/issues/1942","labels_url":"https://api.github.com/repos/hashicorp/terraform/issues/1942/labels{/name}","comments_url":"https://api.github.com/repos/hashicorp/terraform/issues/1942/comments","events_url":"https://api.github.com/repos/hashicorp/terraform/issues/1942/events","html_url":"https://github.com/hashicorp/terraform/issues/1942","id":75930674,"number":1942,"title":"Template variables and bash substitution/array","user":{"login":"smotti","id":7963281,"avatar_url":"https://avatars.githubusercontent.com/u/7963281?v=3","gravatar_id":"","url":"https://api.github.com/users/smotti","html_url":"https://github.com/smotti","followers_url":"https://api.github.com/users/smotti/followers","following_url":"https://api.github.com/users/smotti/following{/other_user}","gists_url":"https://api.github.com/users/smotti/gists{/gist_id}","starred_url":"https://api.github.com/users/smotti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smotti/subscriptions","organizations_url":"https://api.github.com/users/smotti/orgs","repos_url":"https://api.github.com/users/smotti/repos","events_url":"https://api.github.com/users/smotti/events{/privacy}","received_events_url":"https://api.github.com/users/smotti/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-05-13T10:13:32Z","updated_at":"2015-05-13T10:13:32Z","closed_at":null,"body":"Currently how template variables are defined in templates interfere with bash's substitution feature and also array access.\r\n\r\nFor example, we like to append the instance id to the hostname of a new instance during bootstrap, for this we use the user_data attribute provided by the aws_instance resource.\r\nThe user_data will use as value a rendered template_file, i.e.:\r\n\r\n```\r\nresource \"template_file\" \"user_data\" {\r\n  filename = \"user_data.txt\"\r\n}\r\n\r\nresource \"aws_instance\" \"worker\" {\r\n  ami = \"ami-ffeeddcc\"\r\n  instance_type = \"t1.micro\"\r\n  user_data = \"${template_file.user_data.rendered}\"\r\n}\r\n\r\n```\r\n\r\nWhen using AWS EC2 one can get the instance-id via a meta-data http api:\r\n\r\n```\r\ncurl -s http://169.254.169.254/latest/meta-data/instance-id\r\ni-ffeeddcc\r\n```\r\n\r\nBut we don't need the 'i-' thus we use the following bash substitution:\r\n\r\n```\r\n#!/bin/bash -xe\r\nID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)\r\necho worker-${ID%i-} > /etc/hostname\r\n/etc/init.d/hostname start\r\n```\r\n\r\nUnfortunately this results in a 'parse error: syntax error' when terraform wants to render the template.\r\n\r\nWe also tried to escape the '{' and '}' but this will result in the hostname: \r\n```\r\nworker-$\\{ID%i-\\}\r\n```\r\n"}}