为什么 terraform aws 代码无法呈现?
Why does terraform aws code fail to render?
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" {
为什么我会收到错误消息?
template_file
数据源中的 template
参数作为 Terraform 模板语法处理。
在此语法中,使用 ${...}
具有特殊含义,即 ...
部分将由传递到模板中的某些变量注入。
Bash 也允许使用此语法,以获取您打算使用的变量的值。
为了协调这一点,您需要转义 $
字符,以便 Terraform 模板编译器保留它,您可以通过将字符加倍来实现:$${i}
案例。
https://www.terraform.io/docs/configuration/expressions.html#string-templates
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" {
为什么我会收到错误消息?
template_file
数据源中的 template
参数作为 Terraform 模板语法处理。
在此语法中,使用 ${...}
具有特殊含义,即 ...
部分将由传递到模板中的某些变量注入。
Bash 也允许使用此语法,以获取您打算使用的变量的值。
为了协调这一点,您需要转义 $
字符,以便 Terraform 模板编译器保留它,您可以通过将字符加倍来实现:$${i}
案例。
https://www.terraform.io/docs/configuration/expressions.html#string-templates