[英]Why does terraform aws code fail to render?
Terraform version = 0.12地形版本 = 0.12
resource "aws_instance" "bespin-ec2-web" {
ami = "ami-0bea7fd38fabe821a"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.bespin-sg.id]
subnet_id = aws_subnet.bespin-subnet-public-a.id
associate_public_ip_address = true
tags = {
Name = "bespin-ec2-web-a"
}
user_data = data.template_file.user_data.rendered
}
data "template_file" "user_data" {
template = file("${path.module}/userdata.sh")
}
userdata.sh file用户数据.sh文件
#!/bin/bash
USERS="bespin"
GROUP="bespin"
for i in $USERS; do
/usr/sbin/adduser ${i};
/bin/echo ${i}:${i}1! | chpasswd;
done
cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config_old
sed -i 's/PasswordAuthentication no/#PasswordAuthentication no/' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
systemctl restart sshd
terraform plan result地形规划结果
Error: failed to render : <template_file>:5,24-25: Unknown variable; There is no variable named "i"., and 2 other di
agnostic(s)
on instance.tf line 13, in data "template_file" "user_data":
13: data "template_file" "user_data" {
Why am I getting an error?为什么我收到错误?
The template
argument in the template_file
data source is processed as Terraform template syntax. template_file
数据源中的template
参数被处理为 Terraform 模板语法。
In this syntax, using ${...}
has a special meaning, that the ...
part will be injected by some var that is passed into the template.在这个语法中,使用
${...}
有一个特殊的含义, ...
部分将被一些传递到模板中的 var 注入。
Bash also allows this syntax, for getting the values of variables as your intending to use it. Bash 也允许使用这种语法,以便在您打算使用它时获取变量的值。
To reconcile this, you'll need to escape the $
character so that the terraform template compiler will leave it be, which you can do by doubling up the character: $${i}
in all cases.为了解决这个问题,您需要对
$
字符进行转义,以便 terraform 模板编译器将其保留,您可以通过将字符加倍来实现: $${i}
在所有情况下。
https://www.terraform.io/docs/configuration/expressions.html#string-templates https://www.terraform.io/docs/configuration/expressions.html#string-templates
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.