Terraform版本= 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文件
#!/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地形图结果
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" {为什么我会出错?
发布于 2020-02-13 15:47:54
template数据源中的template_file参数作为Terraform模板语法处理。
在这个语法中,使用${...}有一个特殊的含义,即...部件将被传递到模板中的某个var注入。
Bash还允许这种语法,以便在您打算使用它时获取变量的值。
为了协调这一点,您需要转义$字符,以便terraform模板编译器保留它,这可以通过在所有情况下加倍字符:$${i}来实现。
https://www.terraform.io/docs/configuration/expressions.html#string-templates
https://stackoverflow.com/questions/60203230
复制相似问题