首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >读取文件并将输出保存到local_file

读取文件并将输出保存到local_file
EN

Stack Overflow用户
提问于 2021-03-29 19:25:32
回答 1查看 123关注 0票数 0

我正在尝试读取azurerm_linux_virtual_machine上的文件内容,并将其保存到local_file中,以便ansible playbook以后可以引用它。目前,.tf如下所示

代码语言:javascript
运行
复制
resource "azurerm_linux_virtual_machine" "vm" {
name                  = myvm
location              = myzone
resource_group_name   = azurerm_resource_group.azureansibledemo.name
network_interface_ids = [azurerm_network_interface.myterraformnic.id]
size                  = "Standard_DS1_v2"

os_disk {
    name              = "storage"
    caching           = "ReadWrite"
    storage_account_type = "Premium_LRS"
}

source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
}

computer_name  = myvm
admin_username = "azureuser"
disable_password_authentication = true
custom_data = base64encode(file("telnet.sh"))

admin_ssh_key {
    username       = "azureuser"
    public_key     = tls_private_key.ansible_ssh_key.public_key_openssh
}

boot_diagnostics {
    storage_account_uri = azurerm_storage_account.mystorageaccount.primary_blob_endpoint
}


}

output "myoutput" {
    value = file("/tmp/output.yml")
}

resource "local_file" "testoutput" {
  content = <<-DOC
    ${file("/tmp/output.yml")}
    DOC
    filename = "test.yml"
}

但是当我运行terraform plan时,我得到以下错误

代码语言:javascript
运行
复制
Error: Invalid function argument

  on main.tf line 181, in resource "local_file" "testoutput":
 181:     ${file("/tmp/output.yml")}

Invalid value for "path" parameter: no file exists at /tmp/output.yml; this
function works only with files that are distributed as part of the
configuration source code, so if this file will be created by a resource in
this configuration you must instead obtain this result from an attribute of
that resource.

输出myoutput是正常的,并且不返回任何错误,这仅在我添加资源local_file时发生。有没有一种方法可以将文件的输出转换为local_file?

EN

Stack Overflow用户

发布于 2021-03-29 19:35:58

将远程文件拷贝到本地是TF的not supported操作。

解决方法是在local-exec中使用scp,如here所示。例如:

代码语言:javascript
运行
复制
provisioner "local-exec" {
  command = "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${var.openstack_keypair} ubuntu@${openstack_networking_floatingip_v2.wr_manager_fip.address}:~/client.token ."
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66853378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档