gpt4 book ai didi

terraform - 如何动态获取 EC 2 的私有(private) IP 并将其放入/etc/hosts

转载 作者:行者123 更新时间:2023-12-03 20:30:44 27 4
gpt4 key购买 nike

我想使用 Terraform 创建多个 EC2 实例并将实例的私有(private) IP 地址写入 /etc/hosts在每个实例上。

目前我正在尝试以下代码,但它不起作用:

resource "aws_instance" "ceph-cluster" {
count = "${var.ceph_cluster_count}"
ami = "${var.app_ami}"
instance_type = "t2.small"
key_name = "${var.ssh_key_name}"

vpc_security_group_ids = [
"${var.vpc_ssh_sg_ids}",
"${aws_security_group.ceph.id}",
]

subnet_id = "${element(split(",", var.subnet_ids), count.index)}"

associate_public_ip_address = "true"
// TODO 一時的にIAM固定
//iam_instance_profile = "${aws_iam_instance_profile.app_instance_profile.name}"
iam_instance_profile = "${var.iam_role_name}"

root_block_device {
delete_on_termination = "true"
volume_size = "30"
volume_type = "gp2"
}

connection {
user = "ubuntu"
private_key = "${file("${var.ssh_key}")}"
agent = "false"
}

provisioner "file" {
source = "../../../scripts"
destination = "/home/ubuntu/"
}

tags {
Name = "${var.infra_name}-ceph-cluster-${count.index}"
InfraName = "${var.infra_name}"
}

provisioner "remote-exec" {
inline = [
"cat /etc/hosts",
"cat ~/scripts/ceph/ceph_rsa.pub >> ~/.ssh/authorized_keys",
"cp -arp ~/scripts/ceph/ceph_rsa ~/.ssh/ceph_rsa",
"chmod 700 ~/.ssh/ceph_rsa",
"echo 'IdentityFile ~/.ssh/ceph_rsa' >> ~/.ssh/config",
"echo 'User ubuntu' >> ~/.ssh/config",
"echo '${aws_instance.ceph-cluster.0.private_ip} node01 ceph01' >> /etc/hosts ",
"echo '${aws_instance.ceph-cluster.1.private_ip} node02 ceph02' >> /etc/hosts "
]
}

}


aws_instance.ceph-cluster. *. private_ip

我想得到上面命令的结果并把它放在 /etc/hosts .

最佳答案

我对数据库集群有类似的需求(某种穷人的领事替代品),我最终使用了以下 Terraform 文件:

variable "cluster_member_count" {
description = "Number of members in the cluster"
default = "3"
}
variable "cluster_member_name_prefix" {
description = "Prefix to use when naming cluster members"
default = "cluster-node-"
}
variable "aws_keypair_privatekey_filepath" {
description = "Path to SSH private key to SSH-connect to instances"
default = "./secrets/aws.key"
}

# EC2 instances
resource "aws_instance" "cluster_member" {
count = "${var.cluster_member_count}"
# ...
}

# Bash command to populate /etc/hosts file on each instances
resource "null_resource" "provision_cluster_member_hosts_file" {
count = "${var.cluster_member_count}"

# Changes to any instance of the cluster requires re-provisioning
triggers {
cluster_instance_ids = "${join(",", aws_instance.cluster_member.*.id)}"
}
connection {
type = "ssh"
host = "${element(aws_instance.cluster_member.*.public_ip, count.index)}"
user = "ec2-user"
private_key = "${file(var.aws_keypair_privatekey_filepath)}"
}
provisioner "remote-exec" {
inline = [
# Adds all cluster members' IP addresses to /etc/hosts (on each member)
"echo '${join("\n", formatlist("%v", aws_instance.cluster_member.*.private_ip))}' | awk 'BEGIN{ print \"\\n\\n# Cluster members:\" }; { print $0 \" ${var.cluster_member_name_prefix}\" NR-1 }' | sudo tee -a /etc/hosts > /dev/null",
]
}
}

一条规则是每个集群成员都由 cluster_member_name_prefix 命名。 Terraform 变量后跟计数索引(从 0 开始):cluster-node-0、cluster-node-1 等。

这会将以下行添加到每个“aws_instance.cluster_member”资源的 /etc/hosts文件(每个成员的完全相同的行和相同的顺序):
# Cluster members:
10.0.1.245 cluster-node-0
10.0.1.198 cluster-node-1
10.0.1.153 cluster-node-2

就我而言, null_resource填充 /etc/hosts文件由 EBS 卷附件触发,但 "${join(",", aws_instance.cluster_member.*.id)}"触发器也应该可以正常工作。

另外,为了本地开发,我添加了 local-exec配置器在本地写下每个 IP 到 cluster_ips.txt文件:
resource "null_resource" "write_resource_cluster_member_ip_addresses" {
depends_on = ["aws_instance.cluster_member"]

provisioner "local-exec" {
command = "echo '${join("\n", formatlist("instance=%v ; private=%v ; public=%v", aws_instance.cluster_member.*.id, aws_instance.cluster_member.*.private_ip, aws_instance.cluster_member.*.public_ip))}' | awk '{print \"node=${var.cluster_member_name_prefix}\" NR-1 \" ; \" $0}' > \"${path.module}/cluster_ips.txt\""
# Outputs is:
# node=cluster-node-0 ; instance=i-03b1f460318c2a1c3 ; private=10.0.1.245 ; public=35.180.50.32
# node=cluster-node-1 ; instance=i-05606bc6be9639604 ; private=10.0.1.198 ; public=35.180.118.126
# node=cluster-node-2 ; instance=i-0931cbf386b89ca4e ; private=10.0.1.153 ; public=35.180.50.98
}
}

并且,使用以下 shell 命令,我可以将它们添加到我的本地 /etc/hosts文件:
awk -F'[;=]' '{ print $8 " " $2 " #" $4 }' cluster_ips.txt >> /etc/hosts

例子:
35.180.50.32 cluster-node-0 # i-03b1f460318c2a1c3
35.180.118.126 cluster-node-1 # i-05606bc6be9639604
35.180.50.98 cluster-node-2 # i-0931cbf386b89ca4e

关于terraform - 如何动态获取 EC 2 的私有(private) IP 并将其放入/etc/hosts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48184438/

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