如何使用Terraform的文件配置器从本地计算机复制到VM?

时间:2018-02-01 16:51:25

标签: terraform

我是Terraform的新手,到目前为止已经设法在Azure上启动并运行基本VM(加上资源管理器修剪)。我想到的下一个任务是让Terraform将本地计算机中的文件复制到新创建的实例中。理想情况下,我是在每次运行apply命令后都会复制文件的解决方案之后。

我觉得我非常接近,但到目前为止,我只是变得无穷无尽了#34;仍在创造......"我申请后的陈述(文件是0kb,所以在几分钟后放弃感觉是安全的。)

到目前为止,这是我所得到的(基于此代码):https://stackoverflow.com/a/37866044/4941009

网络

resource "azurerm_public_ip" "pub-ip" {
    name = "PublicIp"
    location = "${var.location}"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    public_ip_address_allocation = "Dynamic"
    domain_name_label = "${var.hostname}"
}

VM

resource "azurerm_virtual_machine" "vm" {
  name                  = "${var.hostname}"
  location              = "${var.location}"
  resource_group_name   = "${azurerm_resource_group.rg.name}"
  vm_size               = "${var.vm_size}"
  network_interface_ids = ["${azurerm_network_interface.nic.id}"]

  storage_image_reference {
    publisher = "${var.image_publisher}"
    offer     = "${var.image_offer}"
    sku       = "${var.image_sku}"
    version   = "${var.image_version}"
  }

  storage_os_disk {
    name          = "${var.hostname}osdisk1"
    vhd_uri       = "${azurerm_storage_account.stor.primary_blob_endpoint}${azurerm_storage_container.cont.name}/${var.hostname}osdisk.vhd"
    os_type       = "${var.os_type}"
    caching       = "ReadWrite"
    create_option = "FromImage"
  }

  os_profile {
    computer_name  = "${var.hostname}"
    admin_username = "${var.admin_username}"
    admin_password = "${var.admin_password}"
  }

  os_profile_windows_config {
    provision_vm_agent = true
  }

  boot_diagnostics {
    enabled     = true
    storage_uri = "${azurerm_storage_account.stor.primary_blob_endpoint}"
  }

  tags {
      environment = "${var.environment}"
  }
}

文件供应商

resource "null_resource" "copy-test-file" {

  connection {
    type     = "ssh"
    host     = "${azurerm_virtual_machine.vm.ip_address}"
    user     = "${var.admin_username}"
    password = "${var.admin_password}"
  }

  provisioner "file" {
    source      = "test.txt"
    destination = "/tmp/test.txt"
  }

}

顺便说一句,如果我将错误的登录详细信息传递给配置程序(即在创建VM后重新运行此命令并向配置程序提供不同的密码),行为是相同的。任何人都可以建议我出错的地方吗?

2 个答案:

答案 0 :(得分:1)

我最终得到了这个工作。老实说,我忘记了这个问题,所以我不记得我的确切问题是什么,下面的例子虽然似乎适用于我的实例:

resource "null_resource" remoteExecProvisionerWFolder {

  provisioner "file" {
    source      = "test.txt"
    destination = "/tmp/test.txt"
  }

  connection {
    host     = "${azurerm_virtual_machine.vm.ip_address}"
    type     = "ssh"
    user     = "${var.admin_username}"
    password = "${var.admin_password}"
    agent    = "false"
  }
}

所以看起来这里唯一的区别就是添加了agent = "false"。这是有道理的,因为只有一个用于Windows的SSH身份验证代理,并且很可能我以前没有明确指定使用该代理。然而,很可能我最终在配置中的其他地方改变了一些东西。对未来的人们抱歉没有太多的帮助。

答案 1 :(得分:1)

仅供参考,您可以通过winrm类型连接Windows实例

resource "null_resource" "provision_web" {

  connection {
    host     = "${azurerm_virtual_machine.vm.ip_address}"
    type     = "winrm"
    user     = "alex"
    password = "alexiscool1!"
  }

  provisioner "file" {
    source      = "path/to/folder"
    destination = "C:/path/to/destination"
  }

}