【问题标题】:Configure provisioning script for multiple instance VM vSphere deployment为多实例 VM vSphere 部署配置置备脚本
【发布时间】: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


    【解决方案1】:

    我注意到的第一个问题是vsphere_virtual_machine 资源没有属性guest_ip_address,它是guest_ip_addresses。这确实会返回一个列表,因此您需要弄清楚如何从列表中选择所需的 IP。我不确定 vSphere 中的顺序是否可预测。如果我记得,不是。

    在不知道您要完成什么的情况下,最简单的方法可能是使用default_ip_address,因为这会返回单个地址并为“最有可能”的情况选择 ip。

    您似乎还将主机设置为多宿主。这增加了额外的复杂性。如果default_ip_address 没有给你你想要的,你需要使用一些更复杂的表达式来找到你的IP。也许您可以使用sort 函数,这样排序就更可预测了。您还可以使用for 循环“找到”IP。

    关于构建模块,如果上面的代码在一个模块中,我建议的第一件事是避免使用count。其原因将在以下文本中解释。 Hashicorp 在他们的网站上有很多很好的文档。此外,gruntwork 的人们也有一个blog series,他们将其发展成一本书,名为Terraform Up and Running。我建议检查一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 2021-04-06
      相关资源
      最近更新 更多