linux - 导航到 Azure 中由 Terraform 创建的文件共享

标签 linux azure terraform azure-storage terraform-provider-azure

当我们进入同一资源组中的 RHEL 虚拟机时,我们应该使用什么特定语法来导航到由下面的 Terraform 代码创建的文件共享目录?

要求的答复形式:

这个OP要求用几行代码来回答,其形式如下:

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

此外,我们还询问是否需要创建任何其他资源,以便有存储空间可供有权使用存储共享目录的虚拟机使用。

创建存储的 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
}

最佳答案

有多种方法可以配置 Azure VM 中文件共享的使用。

场景 1:您可以同时创建共享和虚拟机,并在虚拟机上使用 remote_exec 挂载共享,如下所示:

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

输出:

enter image description here enter image description here enter image description here

<小时/>

场景 2:如果您有现有的虚拟机,那么您只需创建存储资源,然后使用自定义脚本扩展即可挂载它们:

地形代码:

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:

#!/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}"

输出:

enter image description here enter image description here enter image description here enter image description here

<小时/>

场景 - 3:如果您想使用 putty 进行 ssh,然后挂载,您只需转到 >>Portal>>您从 terraform 创建的共享 >> Connect >> Linux 并复制那里提供的脚本,然后执行putty后在VM中运行:

enter image description here

关于linux - 导航到 Azure 中由 Terraform 创建的文件共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71074965/

相关文章:

azure - 为什么 WAIISHost 会锁定某些文件

azure - Terraform - 根据本地字符串列表验证传递的字符串列表

linux - drwxr -xr -x 2 2是什么意思

azure - 无法加载文件或程序集 'msshrtmi' - 发布 Windows Azure 网站时

azure - 需要使用动态 key 在Azure数据工厂中生成JSON文件

indexing - Terraform 列表元素超出范围?

terraform - 在 Terraform 0.12 中合并两张 map 以创建第三张 map

python - 如何使用 IP 地址 python 获取 MAC 地址

python - 使用带有 sudo 的 Python 模块

python : rcvfrom() doesn't work in Linux