问题描述
我是 Terraform 的新手,到目前为止,我已经成功地在 Azure 上启动并运行了一个基本的 VM(加上资源管理器).我想到的下一个任务是让 Terraform 将文件从我的本地机器复制到新创建的实例中.理想情况下,我追求的是每次运行应用命令时都会复制文件的解决方案.
I'm new to Terraform and have so far managed to get a basic VM (plus Resource Manager trimmings) up and running on Azure. The next task I have in mind is to have Terraform copy a file from my local machine into the newly created instance. Ideally I'm after a solution where the file will be copied each time the apply command is run.
我觉得我已经很接近了,但到目前为止,一旦我申请,我就会得到无尽的仍在创建..."语句(文件是 0kb,所以几分钟后放弃是安全的).
I feel like I'm pretty close but so far I just get endless "Still creating..." statements once I apply (the file is 0kb so after a couple of mins it feels safe to give up).
到目前为止,这就是我所拥有的(基于此代码):https://stackoverflow.com/a/37866044/4941009
So far, this is what I've got (based on this code): 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}"
}
虚拟机
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"
}
}
顺便说一句,如果我将不正确的登录详细信息传递给供应商(即在虚拟机创建后重新运行并向供应商提供不同的密码),行为是相同的.谁能建议我哪里出错了?
As an aside, if I pass incorrect login details to the provisioner (ie rerun this after the VM has already been created and supply a different password to the provisioner) the behaviour is the same. Can anyone suggest where I'm going wrong?
推荐答案
我最终得到了这个工作.老实说,我有点忘记了这个问题,所以我不记得我的确切问题是什么,但下面的示例似乎适用于我的实例:
I eventually got this working. Honestly, I kind of forgot about this question so I can't remember what my exact issue was, the example below though seems to work on my instance:
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 身份验证代理,而且我之前可能没有明确指定使用该代理.然而,很可能我最终在配置的其他地方进行了更改.很抱歉未来的人在这方面没有太多帮助.
So it looks like the only difference here is the addition of agent = "false"
. This would make some sense as there's only one SSH authentication agent for windows and it's probable I hadn't explicitly specified to use that agent before. However it could well be that I ultimately changed something elsewhere in the configuration. Sorry future people for not being much help on this one.
这篇关于如何使用 Terraform 的文件配置器从本地计算机复制到 VM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!