首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么terraform aws代码不能呈现?

为什么terraform aws代码不能呈现?
EN

Stack Overflow用户
提问于 2020-02-13 08:15:34
回答 1查看 3.6K关注 0票数 4

Terraform版本= 0.12

代码语言:javascript
复制
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文件

代码语言:javascript
复制
 #!/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

地形图结果

代码语言:javascript
复制
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" {

为什么我会出错?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-13 15:47:54

template数据源中的template_file参数作为Terraform模板语法处理。

在这个语法中,使用${...}有一个特殊的含义,即...部件将被传递到模板中的某个var注入。

Bash还允许这种语法,以便在您打算使用它时获取变量的值。

为了协调这一点,您需要转义$字符,以便terraform模板编译器保留它,这可以通过在所有情况下加倍字符:$${i}来实现。

https://www.terraform.io/docs/configuration/expressions.html#string-templates

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60203230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档