gpt4 book ai didi

azure - 使用azure terraform在3个azure虚拟机中执行shell脚本

转载 作者:行者123 更新时间:2023-12-03 06:34:09 26 4
gpt4 key购买 nike

下面的代码创建了 3 个 linux ubuntu azure 虚拟机。同时我想在这 3 个虚拟机中执行 shell 脚本。为此,我使用下面的代码,但收到以下错误,基本上是使用 filebase64 来执行代码,但它的不工作。有人可以检查一下并让我知道问题

            # Resource-1: Azure Resource Group
resource "azurerm_resource_group" "myrg" {
name = "${var.resource_group}"
location = "${var.location}"
}

# Create Virtual Network
resource "azurerm_virtual_network" "myvnet" {
name = "myvnet-1"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name
}

# Create Subnet
resource "azurerm_subnet" "mysubnet" {

name = "mysubnet-1"
resource_group_name = azurerm_resource_group.myrg.name
virtual_network_name = azurerm_virtual_network.myvnet.name
address_prefixes = ["10.0.2.0/24"]
}


#Create Bastion host
resource "azurerm_subnet" "Azure_Bastion_Subnet" {
name = "AzureBastionSubnet"
resource_group_name = azurerm_resource_group.myrg.name
virtual_network_name = azurerm_virtual_network.myvnet.name
address_prefixes = ["10.0.3.0/24"]
}

#Create Azure Public IP Address
resource "azurerm_public_ip" "mypublicip" {

count = "${var.instance_count}"
name = "mypublicip-${count.index}"
resource_group_name = azurerm_resource_group.myrg.name
location = azurerm_resource_group.myrg.location
allocation_method = "Static"
sku = "Standard"
domain_name_label = "app1-vm-${count.index}-${random_string.myrandom.id}"
}


resource "azurerm_network_interface" "myvmnic" {
count = "${var.instance_count}"
name = "vmnic-${count.index}"
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name

ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.mysubnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = element(azurerm_public_ip.mypublicip[*].id, count.index)

}
}

resource "azurerm_network_security_group" "linux-nsg"{
name = "acceptanceTestSecurityGroup1"
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name

security_rule {
name = "Allowssh"
priority = 150
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}

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

resource "azurerm_subnet_network_security_group_association" "example" {
subnet_id = azurerm_subnet.mysubnet.id
network_security_group_id = azurerm_network_security_group.linux-nsg.id
}

data "template_file" "config"{
template = file("${path.module}/script.sh")
}

# Resource: Azure Linux Virtual Machine
resource "azurerm_linux_virtual_machine" "mylinuxvm" {
count = "${var.instance_count}"
name = "mylinuxvm-${count.index}"
computer_name = "zookeeper-${count.index}" # Hostname of the VM
resource_group_name = azurerm_resource_group.myrg.name
location = azurerm_resource_group.myrg.location
size = "Standard_DS1_v2"
admin_username = "useradmin"
#admin_password = "Solr@12345"
network_interface_ids = [ element(azurerm_network_interface.myvmnic[*].id, count.index)]
custom_data = filebase64(data.template_file.config.rendered)
disable_password_authentication = true
admin_ssh_key {
username = "useradmin"
public_key = file("${path.module}/ssh-keys/terraform-azure.pub")
}

os_disk {
name = "osdisk${count.index}"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
connection {
type = "ssh"
host = self.public_ip_address
user = self.admin_username
private_key = file("${path.module}/ssh-keys/terraform-azure.pem")
}

}
resource "azurerm_public_ip" "bastion_ip" {
name = "bastion_ip"
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name
allocation_method = "Static"
sku = "Standard"
}


resource "azurerm_bastion_host" "bastion_test" {
name = "bastion-test"
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name


ip_configuration {
name = "bastion-configuration"
subnet_id = azurerm_subnet.Azure_Bastion_Subnet.id
public_ip_address_id = azurerm_public_ip.bastion_ip.id
}
}



Error: Invalid function argument
on main.tf line 125, in resource "azurerm_linux_virtual_machine" "mylinuxvm":
125: custom_data = filebase64(data.template_file.config.rendered)
├────────────────
│ while calling filebase64(path)
│ data.template_file.config.rendered is "#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p ~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\"
]\nthen\nmkdir -p ~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p ~/zookeeper/log/zk2\necho \"2\" >
~/zookeeper/data/zk2/myid\n\nelif [ \"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir -p
~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" > ~/zookeeper/data/zk3/myid\nfi\n\n"
Invalid value for "path" parameter: no file exists at
"#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir
-p ~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p
~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p
~/zookeeper/log/zk2\necho \"2\" > ~/zookeeper/data/zk2/myid\n\nelif [
\"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir
-p ~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" >
~/zookeeper/data/zk3/myid\nfi\n\n"; 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.
Error: Invalid function argument
on main.tf line 125, in resource "azurerm_linux_virtual_machine" "mylinuxvm":
125: custom_data = filebase64(data.template_file.config.rendered)
├────────────────
│ while calling filebase64(path)
│ data.template_file.config.rendered is "#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p ~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\"
]\nthen\nmkdir -p ~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p ~/zookeeper/log/zk2\necho \"2\" >
~/zookeeper/data/zk2/myid\n\nelif [ \"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir -p
~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" > ~/zookeeper/data/zk3/myid\nfi\n\n"
Invalid value for "path" parameter: no file exists at
"#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir
-p ~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p
~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p
~/zookeeper/log/zk2\necho \"2\" > ~/zookeeper/data/zk2/myid\n\nelif [
\"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir
-p ~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" >
~/zookeeper/data/zk3/myid\nfi\n\n"; 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.
Error: Invalid function argument
on main.tf line 125, in resource "azurerm_linux_virtual_machine" "mylinuxvm":
125: custom_data = filebase64(data.template_file.config.rendered)
├────────────────
│ while calling filebase64(path)
│ data.template_file.config.rendered is "#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p ~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\"
]\nthen\nmkdir -p ~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p ~/zookeeper/log/zk2\necho \"2\" >
~/zookeeper/data/zk2/myid\n\nelif [ \"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir -p
~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" > ~/zookeeper/data/zk3/myid\nfi\n\n"
Invalid value for "path" parameter: no file exists at
"#!/bin/sh\nhost=$(hostname)\nif [ \"$host\" = \"zookeeper-0\" ]\nthen\nmkdir
-p ~/zookeeper/zk-server-1\nmkdir -p ~/zookeeper/data/zk1\nmkdir -p
~/zookeeper/log/zk1\n\nelif [ \"$host\" = \"zookeeper-1\" ]\nthen\nmkdir -p
~/zookeeper/zk-server-2\nmkdir -p ~/zookeeper/data/zk2\nmkdir -p
~/zookeeper/log/zk2\necho \"2\" > ~/zookeeper/data/zk2/myid\n\nelif [
\"$host\" = \"zookeeper-2\" ]\nthen\nmkdir -p ~/zookeeper/zk-server-3\nmkdir
-p ~/zookeeper/data/zk3\nmkdir -p ~/zookeeper/log/zk3\necho \"3\" >
~/zookeeper/data/zk3/myid\nfi\n\n"; 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.

最佳答案

删除该 block

data "template_file" "config" {
template = file("${path.module}/script.sh")
}

替换下面的代码

# Resource: Azure Linux Virtual Machine
resource "azurerm_linux_virtual_machine" "mylinuxvm" {
[...
custom_data = filebase64(data.template_file.config.rendered)
...]
}

# Resource: Azure Linux Virtual Machine
resource "azurerm_linux_virtual_machine" "mylinuxvm" {
[...
custom_data = filebase64("${path.module}/script.sh")
...]
}

作为引用,您的结果将是

  # azurerm_linux_virtual_machine.mylinuxvm[0] will be created
+ resource "azurerm_linux_virtual_machine" "mylinuxvm" {
+ admin_username = "useradmin"
+ allow_extension_operations = true
+ computer_name = "zookeeper-0"
+ custom_data = (sensitive value)
+ disable_password_authentication = true
+ extensions_time_budget = "PT1H30M"
+ id = (known after apply)
+ location = "westeurope"
+ max_bid_price = -1
+ name = "mylinuxvm-0"
+ network_interface_ids = (known after apply)
+ patch_assessment_mode = "ImageDefault"
+ patch_mode = "ImageDefault"
+ platform_fault_domain = -1
+ priority = "Regular"
+ private_ip_address = (known after apply)
+ private_ip_addresses = (known after apply)
+ provision_vm_agent = true
+ public_ip_address = (known after apply)
+ public_ip_addresses = (known after apply)
+ resource_group_name = "rg-kv-stackoverflow"
+ size = "Standard_DS1_v2"
+ virtual_machine_id = (known after apply)
[...]

已编辑

我用来验证的脚本

请检查您的脚本是否正常工作。您可以通过检查 /var/log/cloud-init-output.log 文件来验证这一点

#!/bin/bash

printf "Hello World"
mkdir -p /tmp/user-data

来自 cloud-init-output.log 的日志

Cloud-init v. 21.1-19-gbad84ad4-0ubuntu1~16.04.2 running 'modules:config' at Tue, 10 Jan 2023 10:59:16 +0000. Up 40.04 seconds.
Hello WorldCloud-init v. 21.1-19-gbad84ad4-0ubuntu1~16.04.2 running 'modules:final' at Tue, 10 Jan 2023 10:59:32 +0000. Up 56.18 seconds.

执行脚本

  • user-data 目录已创建。
azureadmin@zookeeper-0:~$ ls -l /tmp
total 8
drwx------ 3 root root 4096 Jan 10 10:58 systemd-private-1ff50958c212495f8f02a9d123bf4cac-systemd-timesyncd.service-Wcs89v
drwxr-xr-x 2 root root 4096 Jan 10 10:59 user-data

其他背景

自定义数据在首次启动或设置期间可供虚拟机使用,这称为配置。如果您的计算机已创建,并且您正在使用自定义数据,则在这种情况下您必须执行其他步骤。引用https://learn.microsoft.com/en-us/azure/virtual-machines/custom-data

关于azure - 使用azure terraform在3个azure虚拟机中执行shell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75061981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com