简体   繁体   English

读取文件并将 output 保存到 local_file

[英]Read file and save output to local_file

I'm trying to read the content of a file on an azurerm_linux_virtual_machine and save it to a local_file so that an ansible playbook can reference it later.我正在尝试读取 azurerm_linux_virtual_machine 上的文件内容并将其保存到 local_file,以便 ansible 剧本稍后可以引用它。 Currently the.tf looks like this目前.tf 看起来像这样

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"
}

But when i run terraform plan i get the following error但是当我运行 terraform 计划时,我收到以下错误

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.

The output myoutput is fine and returns no errors, this only occurs when i add in the resource local_file. output myoutput 很好并且没有返回错误,这只发生在我添加资源 local_file 时。 Is there a way to get the output of a file to a local_file?有没有办法将文件的 output 获取到 local_file?

Copping remote files to local is not supported by TF. TF不支持将远程文件复制到本地。

The workaround is to use scp in local-exec as shown here .解决方法是在local-exec中使用scp ,如此处所示 For example:例如:

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 ."
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Azure Devops 管道运行代码时缺少 Terraform local_file - Terraform local_file missing when running code using Azure Devops pipeline 本地文件Blob保存 - Local file blob save Azure ServiceBus队列保存并读取文件字节[] - Azure ServiceBus Queue save and read File byte[] 在MAC上的vscode中使用cloud shell输出到本地文件 - Output to local file using cloud shell in vscode on a MAC 如何使用逻辑应用程序从二进制输出中将文件另存为pdf - how to save file as pdf from binary output using logic app 如何将 Azure 函数输出作为 csv 文件保存到 Blob 存储 - How to save a Azure Function output as csv file to Blob storage 我们可以将 cmd 中的 output 保存到 txt 文件或环境变量中吗 - Can we save the output in cmd into txt file or environment variables 有没有办法将数据工厂 Web 活动输出保存到文件或数据库表中? - Is there a way to save Data Factory web activity output to a file or database table? 将“设置变量”活动的输出保存到 csv 文件 [Azure 数据工厂] - save the output of 'Set Variable' activity into a csv file [Azure Data Factory] 从本地文件系统读取 Azure 审计日志 - Read Azure Audit Logs from Local File System
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM