gpt4 book ai didi

shell - Terraform 插值错误 : bad substitution

转载 作者:行者123 更新时间:2023-12-03 07:37:47 25 4
gpt4 key购买 nike

我正在尝试在 AWS 上使用 terraform 设置 puppet 主从设置。我有 2 个 EC2 实例,一个是主实例,第二个是从实例。

我已将脚本编写为 setup.tf

provider "aws"{
region="us-east-1"
access_key="XXXX"
secret_key="XXXX"
}

resource "aws_security_group" "default"{
name="terraform-sg"
description="Created by terraform"

ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress{
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

}

resource "aws_instance" "puppet_master"{

connection={
user="ubuntu"
key_file="/home/vaibhav/Downloads/puppet_elk.pem"
}

instance_type="t2.micro"

ami="ami-fce3c696"
key_name = "puppet_elk"
security_groups= ["${aws_security_group.default.name}"]
tags{
Name="master"
}

provisioner "file" {
source = "./scripts/puppet-master.sh"
destination = "~/puppet-master.sh"
}

provisioner "remote-exec" {
inline = [
"chmod +x ~/puppet-master.sh",
"~/puppet-master.sh puppet"
]
}
}

resource "aws_instance" "puppet_node"{

depends_on = ["aws_instance.puppet_master"]

connection={
user="ubuntu"
key_file="/home/vaibhav/Downloads/puppet_elk.pem"
}

instance_type="t2.micro"

ami="ami-fce3c696"
key_name = "puppet_elk"
security_groups= ["${aws_security_group.default.name}"]
tags{
Name="node"
}

provisioner "file" {
source = "./scripts/puppet-node.sh"
destination = "~/puppet-node.sh"
}

provisioner "remote-exec" {
inline = [
"chmod +x ~/puppet-node.sh",
"~/puppet-node.sh node1"
]
}

我用来安装 puppet 主控和从属的脚本是:

#!/bin/bash
if [ $# -eq 0 ]
then
echo "No hostname supplied"
exit
fi
env=$1
echo $env
hostname=`hostname`
myipaddress=$(ifconfig eth0 | awk '/inet addr/{print substr($2,6)}')
sudo apt-get update -y
sudo apt-get upgrade -y
sudo wget http://apt.puppetlabs.com/puppetlabs-release-trusty.deb
sudo dpkg -i puppetlabs-release-trusty.deb
sudo apt-get update -y
sudo apt-get -y install puppetmaster
sudo sed -i "s/$hostname/$env/g" /etc/hostname
sudo sed -i "s/no/yes/g" /etc/default/puppetmaster
sudo echo "127.0.0.1 puppet.example.net puppet" >> /etc/hosts
sudo echo "$myipaddress puppet.example.net puppet" >> /etc/hosts

sudo reboot

对于从站:

#!/bin/bash
if [ $# -eq 0 ]
then
echo "No hostname supplied"
exit
fi
env=$1
echo $env
hostname=`hostname`
sudo apt-get update -y
sudo apt-get upgrade -y
sudo wget http://apt.puppetlabs.com/puppetlabs-trusty.deb
sudo dpkg -i puppetlabs-release-trusty.deb
sudo apt-get update -y
sudo pt-get -y install puppet
sudo ed -i "s/$hostname/$env/g" /etc/hostname
sudo sed -i "s/no/yes/g" /etc/default/puppet
# replace IP with the variable

#masterip=${aws_instance.puppet-node.private_ip}
sudo echo "${aws_instance.puppet_master.private_ip} puppet.example.net puppet" >> /etc/hosts
sudo echo "${aws_instance.puppet_master.private_ip} puppet" >> /etc/hosts
sudo puppet agent --enable
sudo puppet agent --waitforcert 60
sudo reboot

当我运行此脚本时,我在从 shell 脚本中收到插值错误:

{aws_instance.puppet_master.private_ip}:插值错误

我想用主私有(private)IP来代替这个插值字符串,以便在puppet中设置主从,我需要主IP。

有什么解决办法吗?

最佳答案

您不能直接在 shell 文件中使用 terraform 变量。他们的方法是在 shell 代码中使用 %s,然后使用 $format() 直接传递 var:

例如:

!/bin/bash
if [ $# -eq 0 ]
then
echo "No hostname supplied"
exit
fi
env=$1
echo $env
hostname=`hostname`
sudo apt-get update -y
sudo apt-get upgrade -y
sudo wget http://apt.puppetlabs.com/puppetlabs-trusty.deb
sudo dpkg -i puppetlabs-release-trusty.deb
sudo apt-get update -y
sudo pt-get -y install puppet
sudo ed -i "s/$hostname/$env/g" /etc/hostname
sudo sed -i "s/no/yes/g" /etc/default/puppet
# replace IP with the variable

#masterip=${aws_instance.puppet-node.private_ip}
sudo echo "%s" >> /etc/hosts
sudo echo "%s puppet" >> /etc/hosts
sudo puppet agent --enable
sudo puppet agent --waitforcert 60
sudo reboot

然后传递值,如下所示:

${format(file("path/to/file.json"), "${aws_instance.puppet_master.private_ip}","${aws_instance.puppet_master.private_ip}")}

关于shell - Terraform 插值错误 : bad substitution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37795180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com