简体   繁体   English

导航到 Terraform 在 Azure 创建的文件共享

[英]Navigate to file share created by Terraform in Azure

What specific syntax should we use to navigate to the file share directory created by the Terraform code below when we putty in to a RHEL virtual machine in the same resource group?当我们放入同一资源组中的 RHEL 虚拟机时,我们应该使用什么特定语法来导航到由下面的 Terraform 代码创建的文件共享目录?

Form Of Answer Requested:要求的答复形式:

This OP is asking for an answer with a few lines of code in a form that looks something like:该 OP 要求通过几行代码以类似于以下形式的形式提供答案:

ls -al sharename/example
mkdir sharename/example/newdirectory
cd sharename/example/newdirectory
ls -al  

Also, we are asking if any additional resources need to be created in order for there to be storage that can be used by a VM that has permissions to use the storage share directory.此外,我们询问是否需要创建任何其他资源,以便有存储空间可供有权使用存储共享目录的 VM 使用。

Terraform Code That Creates The Storage: Terraform 创建存储的代码:

resource "azurerm_storage_account" "example" {
  name                     = "azureteststorage"
  resource_group_name      = azurerm_resource_group.my-resources.name
  location                 = azurerm_resource_group.my-resources.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_share" "example" {
  name                 = "sharename"
  storage_account_name = azurerm_storage_account.example.name
  quota                = 50
}

resource "azurerm_storage_share_directory" "example" {
  name                 = "example"
  share_name           = azurerm_storage_share.example.name
  storage_account_name = azurerm_storage_account.example.name
}

There are multiple ways to configure the usage of File Share from a Azure VM.有多种方法可以从 Azure VM 配置文件共享的使用。

Scenario 1: You can create share and VM at the same time and mount the Share using remote_exec on the VM like below:场景 1:您可以同时创建共享和 VM,并使用remote_exec在 VM 上挂载共享,如下所示:

provider "azurerm" {
  features{}
}

data "azurerm_resource_group" "example" {
  name     = "ansumantest"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.0.0.0/16"]
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "internal"
  resource_group_name  = data.azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}
resource "azurerm_public_ip" "aks-nfs-public-ip" {
  name                = "aks-nfs-public-ip"
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
  allocation_method   = "Static"

}
resource "azurerm_network_security_group" "example" {
  name                = "ansuman-nsg"
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name

  security_rule {
    name                       = "test123"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "*"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

resource "azurerm_subnet_network_security_group_association" "example" {
  subnet_id                 = azurerm_subnet.example.id
  network_security_group_id = azurerm_network_security_group.example.id
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    public_ip_address_id = azurerm_public_ip.aks-nfs-public-ip.id
    private_ip_address_allocation = "Dynamic"
  }
  depends_on = [
    azurerm_subnet_network_security_group_association.example
  ]
}


resource "azurerm_storage_account" "example" {
  name                     = "ansuazureteststorage1"
  resource_group_name      = data.azurerm_resource_group.example.name
  location                 = data.azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_share" "example" {
  name                 = "ansushare"
  storage_account_name = azurerm_storage_account.example.name
  quota                = 50
}

resource "azurerm_storage_share_directory" "example" {
  name                 = "example"
  share_name           = azurerm_storage_share.example.name
  storage_account_name = azurerm_storage_account.example.name
}
resource "azurerm_ssh_public_key" "example" {
  name                = "ansuman-sshkey"
  resource_group_name = data.azurerm_resource_group.example.name
  location            = data.azurerm_resource_group.example.location
  public_key          = file("~/.ssh/id_rsa.pub")
}

resource "azurerm_linux_virtual_machine" "example" {
  name                = "example-machine"
  resource_group_name = data.azurerm_resource_group.example.name
  location            = data.azurerm_resource_group.example.location
  size                = "Standard_D4s_v4"
  admin_username      = "adminuser"
  admin_password      = "Password@1234"
  disable_password_authentication = false
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]
  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }
    admin_ssh_key {
    username   = "adminuser"
    public_key = azurerm_ssh_public_key.example.public_key
  }

  source_image_reference {
    publisher = "RedHat"
    offer     = "RHEL"
    sku       = "82gen2"
    version   = "latest"
  }
 
    connection {
      type        = "ssh"
      host        = azurerm_public_ip.aks-nfs-public-ip.ip_address
      user        = "adminuser"
      password    = "Password@1234"
    }
    provisioner "remote-exec" {
    inline = [
        "sudo yum install cifs-utils -y",
        "sudo mkdir -p /mnt/${azurerm_storage_account.example.name}/${azurerm_storage_share.example.name}",
        "sudo mount -t cifs //${azurerm_storage_account.example.name}.file.core.windows.net/${azurerm_storage_share.example.name} /mnt/${azurerm_storage_account.example.name}/${azurerm_storage_share.example.name} -o vers=3.0,dir_mode=0777,file_mode=0777,serverino,username=${azurerm_storage_account.example.name},password=${azurerm_storage_account.example.primary_access_key}",
    ]
  }
}

Output: Output:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述


Scenario-2: If you have an existing VM then you can just create the storage resources and then using custom script extension you can mount them:场景 2:如果您有现有的 VM,那么您可以只创建存储资源,然后使用custom script extension来挂载它们:

terraform code: terraform 代码:

resource "azurerm_storage_account" "example" {
  name                     = "ansuazureteststorage1"
  resource_group_name      = data.azurerm_resource_group.example.name
  location                 = data.azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_share" "example" {
  name                 = "ansushare"
  storage_account_name = azurerm_storage_account.example.name
  quota                = 50
}

resource "azurerm_storage_share_directory" "example" {
  name                 = "example"
  share_name           = azurerm_storage_share.example.name
  storage_account_name = azurerm_storage_account.example.name
}

data "azurerm_virtual_machine" "example" {
  name = "example-machine"
  resource_group_name = "ansumantest"
}

resource "azurerm_virtual_machine_extension" "test" {
  name                 = "MountShare"
 virtual_machine_id    = data.azurerm_virtual_machine.example.id
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScript"
  type_handler_version = "2.0"

  settings = <<SETTINGS
    {
        "script": "${base64encode(templatefile("customdata.sh", {
          Storage_account_name="${azurerm_storage_account.example.name}", 
          File_share_name="${azurerm_storage_share.example.name}", 
          Storage_account_key = "${azurerm_storage_account.example.primary_access_key}"
        }))}"
    }
SETTINGS
}

customdata.sh:自定义数据.sh:

#!/bin/sh
sudo yum install cifs-utils -y
sudo mkdir -p "/mnt/${Storage_account_name}/${File_share_name}"
sudo mount -t cifs "//${Storage_account_name}.file.core.windows.net/${File_share_name}" "/mnt/${Storage_account_name}/${File_share_name}" -o "vers=3.0,dir_mode=0777,file_mode=0777,serverino,username=${Storage_account_name},password=${Storage_account_key}"

Output: Output:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述


Scenario - 3: If you want to use putty to ssh and then mount you can just go to>>Portal>>share that you created from terraform >> Connect >> Linux and copy the script provided there and run in the VM after doing putty :场景 - 3:如果你想使用 putty 到 ssh 然后挂载,你只需go to>>Portal>>share that you created from terraform >> Connect >> Linux and copy the script provided there and run in the VM after doing putty

在此处输入图像描述

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

相关问题 Creating a csv file on an Azure File share from a list that is created in a C# Azure Function - Creating a csv file on an Azure File share from a list that was created in a C# Azure Function 如何与组织内的利益相关者共享 Azure 文件共享数据? - How to share Azure File Share data with the stakeholder within the organization? azure 文件共享 map 到基于客户端的文件夹? - azure file share map to folder based on clients? 与 Azure Blob 相比,我们什么时候应该在 azure 中使用文件共享? - When should we use file share in azure as compared to Azure Blobs? Terraform:模块的output变量到azure-pipelines.yml文件 - Terraform: Module's output variable to azure-pipelines.yml file Azure DevOps 管道无法找到可执行文件:“terraform” - Azure DevOps Pipeline unable to locate executable file: 'terraform' Terraform 和 Azure 数据浏览器 - Terraform and Azure Data Explorer 使用 Terraform 创建 Azure 策略 - Create Azure policy with Terraform SSIS 作业无法访问 Azure 文件共享路径 - SSIS job is not able to access the Azure File share path 为 Azure 文件共享存储帐户部署模板时面临的问题 - Facing Issues on Deploying template for Azure File Share Storage Account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM