- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码创建了 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/
有什么方法可以将 Terraform 模板输出用于另一个 Terraform 模板的输入? 例如:我有一个创建 ELB 的 Terraform 模板,我有另一个 Terraform 模板,它将创建一个
我正在使用 Terraform 在 Azure 中设置虚拟网络。 我有几个 VNet,每个 VNet 都有自己的网络安全组 100% 在 Terraform 中管理,在运行 Terraform 之前不
resources and data sources在 terraform 文档中 link ,谁能解释一下它们的区别以及可以使用它们的示例场景 最佳答案 Data Sources :允许 Terra
terraform plan 等命令如何知道/决定使用哪些文件? -help 显示了一个 DIR-OR-PLAN 参数,但没有显示如何使用它: $ terraform -help plan Usage
我在尝试运行使用 terraform lock 的 terraform 脚本时收到以下错误消息。 *Acquiring state lock. This may take a few moments.
我想简化这样的构造 variable "google" { type = object({ project = string region = string
这是一个场景 - 您开发用于研发组织的 terraform 模块。它们已经被一两个微服务使用,转化为十几个 pod。您确定了重构机会,例如将某些功能提取到其自己的 terraform 模块中。很好,但
Terraform 是否支持条件属性?我只想根据变量的值使用属性。 例子: resource "aws_ebs_volume" "my_volume" { availability_zone =
我想将此作为功能请求发布,但我想在发布之前看看是否有其他人找到了一些聪明的方法。或者也许 Hashicorp 的某个人可以告诉我这将是 future 的一个功能 在运行 terraform apply
我在 terraform 的变量插值中遇到了麻烦。这是我的 terraform 配置的样子。即内置函数内的变量 variable "key" {} ssh_keys { pat
运行 terraform 并等待需要很长时间。 所以我想运行它来排除需要最长执行时间的 rds 或者我只想运行 ec2 资源。 有没有办法在 terraform 中做这样的事情? 最佳答案 您可以使用
terraform 是否提供这样的功能来覆盖变量值?假设我已经声明了下面给出的两个变量。 variable "foo" {} variable "bar" { default = "false"} f
我正在为 Terraform Associate Certification 做准备考试。我在 Udemy 上进行了一次练习考试,并收到了一个关于自动安装社区提供程序的问题。但是,根据实际 terra
我有很多使用 Terraform 的 gcp-provider 用 Terraform 0.11 编写的 Terraform 模块,并希望将其升级到 Terraform 0.12。 为此,我需要保留系
我的项目有 2 个存储库。静态网站和服务器。我希望网站由 cloudfront 和 s3 托管,服务器在 elasticbeanstalk 上。我知道这些资源至少需要了解 Route53 资源才能在同
我能有这样的资源吗 resource "foo" "bar.baz"{ ... } 或者以后 . 会把我搞砸吗?特别是,是否允许这样做: resource "foo" "other"{ ...
我能有这样的资源吗 resource "foo" "bar.baz"{ ... } 或者以后 . 会把我搞砸吗?特别是,是否允许这样做: resource "foo" "other"{ ...
运行时terraform init使用 Terraform 时 0.11.3我们收到以下错误: Initializing provider plugins... - Checking for avai
我正在尝试将项目的 CLI 工作区迁移到 Terraform Cloud。我正在使用 Terraform 版本 0.14.8 并遵循官方指南 here . $ terraform0.14.8 work
尝试在Azure Pipeline中将terraform init作为任务运行时,错误指出 spawn C:\hostedtoolcache\windows\terraform\0.12.7\x64\
我是一名优秀的程序员,十分优秀!