[英]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 代码创建的文件共享目录?
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 使用。
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.