【发布时间】:2021-07-20 04:16:47
【问题描述】:
我不太清楚如何将配置“remote-exec”部分添加到我的模块中,我希望它从项目目录中复制配置脚本并执行它们。但是当我添加这个模块时,我似乎无法让它以 VM 实例为目标,并且由于它有多个网卡,我只想以主卡为目标。
我已使用它通过 Terraform 在本地 vSphere 实例上部署 Linux VM。
provider "vsphere" {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_server
# If you have a self-signed cert
allow_unverified_ssl = true
}
这是概述网络部分的示例 Linux 部署脚本,它允许将多个网卡配置到 VM
resource "vsphere_virtual_machine" "Linux" {
count = var.is_windows_image ? 0 : var.instances
depends_on = [var.vm_depends_on]
name = "%{if var.vmnameliteral != ""}${var.vmnameliteral}%{else}${var.vmname}${count.index + 1}${var.vmnamesuffix}%{endif}"
........
dynamic "network_interface" {
for_each = keys(var.network) #data.vsphere_network.network[*].id #other option
content {
network_id = data.vsphere_network.network[network_interface.key].id
adapter_type = var.network_type != null ? var.network_type[network_interface.key] : data.vsphere_virtual_machine.template.network_interface_types[0]
}
}
........
//Copy the file to execute
provisioner "file" {
source = var.provisioner_file_source // eg ./scripts/*
destination = var.provisioner_file_destination // eg /tmp/filename
connection {
type = "ssh" // for Linux its SSH
user = var.provisioner_ssh_username
password = var.provisioner_ssh_password
host = self.vsphere_virtual_machine.Linux.*.guest_ip_address
}
}
//Run the script
provisioner "remote-exec" {
inline = [
"chmod +x ${var.provisioner_file_destination}",
"${var.provisioner_file_destination} args",
]
connection {
type = "ssh" // for Linux its SSH
user = var.provisioner_ssh_username
password = var.provisioner_ssh_password
host = self.vsphere_virtual_machine.Linux.*.guest_ip_address
}
}
}
} // end of resource "vsphere_virtual_machine" "Linux"
所以我尝试了self。参考,但到目前为止 self.vsphere_virtual_machine.Linux.*.guest_ip_address 这只是显示了整个访客 IP 数组?
谁能指出我正确的方向,甚至是关于 terraform 模块的好指南?
【问题讨论】:
标签: linux terraform vmware vsphere