我正在尝试在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 master和slave的脚本是:
#!/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
当我运行此脚本时,我在slave shell脚本中得到插值错误:
{aws_instance.puppet_master.private_ip}:错误插值
我想用主私有Ip代替这个插值字符串,以便在puppet中设置主从属设备我需要主IP。
有没有解决这个问题?
答案 0 :(得分:0)
您不能直接在shell文件中使用terraform vars。他们的方法是在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}")}