Automation 运行terraform init、terraform plan或apply时出现无效字符错误
我使用VScode editor运行Terraform,该编辑器使用PowerShell作为默认shell,当我尝试验证它或通过VScode、外部PowerShell或CMD运行Terraform init/plan/apply时,会收到相同的错误 在我添加虚拟机创建代码之前,代码一直在运行,没有任何问题。我已经收集了variables.tf、terraform.tfvars和下面的主要terraform代码 terraform.tfvarsAutomation 运行terraform init、terraform plan或apply时出现无效字符错误,automation,terraform,devops,terraform-provider-azure,Automation,Terraform,Devops,Terraform Provider Azure,我使用VScode editor运行Terraform,该编辑器使用PowerShell作为默认shell,当我尝试验证它或通过VScode、外部PowerShell或CMD运行Terraform init/plan/apply时,会收到相同的错误 在我添加虚拟机创建代码之前,代码一直在运行,没有任何问题。我已经收集了variables.tf、terraform.tfvars和下面的主要terraform代码 terraform.tfvars web_server_location
web_server_location = "West US 2"
resource_prefix = "web-server"
web_server_address_space = "1.0.0.0/22"
web_server_address_prefix = "1.0.1.0/24"
Environment = "Test"
变量。tf
variable "web_server_location" {
type = string
}
variable "resource_prefix" {
type = string
}
variable "web_server_address_space" {
type = string
}
#variable for network range
variable "web_server_address_prefix" {
type = string
}
#variable for Environment
variable "Environment" {
type = string
}
# Configure the Azure Provider
provider "azurerm" {
# whilst the `version` attribute is optional, we recommend pinning to a given version of the Provider
version = "=2.0.0"
features {}
}
# Create a resource group
resource "azurerm_resource_group" "example_rg" {
name = "${var.resource_prefix}-RG"
location = var.web_server_location
}
# Create a virtual network within the resource group
resource "azurerm_virtual_network" "example_vnet" {
name = "${var.resource_prefix}-vnet"
resource_group_name = azurerm_resource_group.example_rg.name
location = var.web_server_location
address_space = [var.web_server_address_space]
}
# Create a subnet within the virtual network
resource "azurerm_subnet" "example_subnet" {
name = "${var.resource_prefix}-subnet"
resource_group_name = azurerm_resource_group.example_rg.name
virtual_network_name = azurerm_virtual_network.example_vnet.name
address_prefix = var.web_server_address_prefix
}
# Create a Network Interface
resource "azurerm_network_interface" "example_nic" {
name = "${var.resource_prefix}-NIC"
location = azurerm_resource_group.example_rg.location
resource_group_name = azurerm_resource_group.example_rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example_subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.example_public_ip.id
}
}
# Create a Public IP
resource "azurerm_public_ip" "example_public_ip" {
name = "${var.resource_prefix}-PublicIP"
location = azurerm_resource_group.example_rg.location
resource_group_name = azurerm_resource_group.example_rg.name
allocation_method = var.Environment == "Test" ? "Static" : "Dynamic"
tags = {
environment = "Test"
}
}
# Creating resource NSG
resource "azurerm_network_security_group" "example_nsg" {
name = "${var.resource_prefix}-NSG"
location = azurerm_resource_group.example_rg.location
resource_group_name = azurerm_resource_group.example_rg.name
# Security rule can also be defined with resource azurerm_network_security_rule, here just defining it inline.
security_rule {
name = "RDPInbound"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
environment = "Test"
}
}
# NIC and NSG association
resource "azurerm_network_interface_security_group_association" "example_nsg_association" {
network_interface_id = azurerm_network_interface.example_nic.id
network_security_group_id = azurerm_network_security_group.example_nsg.id
}
# Creating Windows Virtual Machine
resource "azurerm_virtual_machine" "example_windows_vm" {
name = "${var.resource_prefix}-VM"
location = azurerm_resource_group.example_rg.location
resource_group_name = azurerm_resource_group.example_rg.name
network_interface_ids = [azurerm_network_interface.example_nic.id]
vm_size = "Standard_B1s"
delete_os_disk_on_termination = true
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServerSemiAnnual"
sku = "Datacenter-Core-1709-smalldisk"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
storage_account_type = "Standard_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "adminuser"
admin_password = "Password1234!"
}
os_profile_windows_config {
disable_password_authentication = false
}
tags = {
environment = "Test"
}
}
terraform\u示例.tf
variable "web_server_location" {
type = string
}
variable "resource_prefix" {
type = string
}
variable "web_server_address_space" {
type = string
}
#variable for network range
variable "web_server_address_prefix" {
type = string
}
#variable for Environment
variable "Environment" {
type = string
}
# Configure the Azure Provider
provider "azurerm" {
# whilst the `version` attribute is optional, we recommend pinning to a given version of the Provider
version = "=2.0.0"
features {}
}
# Create a resource group
resource "azurerm_resource_group" "example_rg" {
name = "${var.resource_prefix}-RG"
location = var.web_server_location
}
# Create a virtual network within the resource group
resource "azurerm_virtual_network" "example_vnet" {
name = "${var.resource_prefix}-vnet"
resource_group_name = azurerm_resource_group.example_rg.name
location = var.web_server_location
address_space = [var.web_server_address_space]
}
# Create a subnet within the virtual network
resource "azurerm_subnet" "example_subnet" {
name = "${var.resource_prefix}-subnet"
resource_group_name = azurerm_resource_group.example_rg.name
virtual_network_name = azurerm_virtual_network.example_vnet.name
address_prefix = var.web_server_address_prefix
}
# Create a Network Interface
resource "azurerm_network_interface" "example_nic" {
name = "${var.resource_prefix}-NIC"
location = azurerm_resource_group.example_rg.location
resource_group_name = azurerm_resource_group.example_rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example_subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.example_public_ip.id
}
}
# Create a Public IP
resource "azurerm_public_ip" "example_public_ip" {
name = "${var.resource_prefix}-PublicIP"
location = azurerm_resource_group.example_rg.location
resource_group_name = azurerm_resource_group.example_rg.name
allocation_method = var.Environment == "Test" ? "Static" : "Dynamic"
tags = {
environment = "Test"
}
}
# Creating resource NSG
resource "azurerm_network_security_group" "example_nsg" {
name = "${var.resource_prefix}-NSG"
location = azurerm_resource_group.example_rg.location
resource_group_name = azurerm_resource_group.example_rg.name
# Security rule can also be defined with resource azurerm_network_security_rule, here just defining it inline.
security_rule {
name = "RDPInbound"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
environment = "Test"
}
}
# NIC and NSG association
resource "azurerm_network_interface_security_group_association" "example_nsg_association" {
network_interface_id = azurerm_network_interface.example_nic.id
network_security_group_id = azurerm_network_security_group.example_nsg.id
}
# Creating Windows Virtual Machine
resource "azurerm_virtual_machine" "example_windows_vm" {
name = "${var.resource_prefix}-VM"
location = azurerm_resource_group.example_rg.location
resource_group_name = azurerm_resource_group.example_rg.name
network_interface_ids = [azurerm_network_interface.example_nic.id]
vm_size = "Standard_B1s"
delete_os_disk_on_termination = true
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServerSemiAnnual"
sku = "Datacenter-Core-1709-smalldisk"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
storage_account_type = "Standard_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "adminuser"
admin_password = "Password1234!"
}
os_profile_windows_config {
disable_password_authentication = false
}
tags = {
environment = "Test"
}
}
错误:
我自己在几个不同的环境中遇到过这个问题,它确实有一个共同的解决方案,一点也不有趣:手动将代码输入回 此资源块似乎是它遇到问题的地方:
resource "azurerm_virtual_machine" "example_windows_vm" {
name = "${var.resource_prefix}-VM"
location = azurerm_resource_group.example_rg.location
resource_group_name = azurerm_resource_group.example_rg.name
network_interface_ids = [azurerm_network_interface.example_nic.id]
vm_size = "Standard_B1s"
delete_os_disk_on_termination = true
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServerSemiAnnual"
sku = "Datacenter-Core-1709-smalldisk"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
storage_account_type = "Standard_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "adminuser"
admin_password = "Password1234!"
}
os_profile_windows_config {
disable_password_authentication = false
}
tags = {
environment = "Test"
}
}
尝试按原样将其复制回编辑器中。我看不到任何有问题的字符,具有讽刺意味的是,StackOverflow可能已经帮了你一把,把它们过滤掉了。按字面意思将其复制/粘贴到现有块上可能会纠正这种情况
我在网上看到过很多带有时髦双引号的Terraform示例(这不是ASCII双引号,也不起作用)。这可能就是你所看到的
除此之外,您还需要将代码推送到GitHub或类似的地方,这样我就可以自己查看原始字节。我自己在几个不同的上下文中遇到过这个问题,它确实有一个共同的解决方案,一点也不好玩:手动将代码输入回 此资源块似乎是它遇到问题的地方:
resource "azurerm_virtual_machine" "example_windows_vm" {
name = "${var.resource_prefix}-VM"
location = azurerm_resource_group.example_rg.location
resource_group_name = azurerm_resource_group.example_rg.name
network_interface_ids = [azurerm_network_interface.example_nic.id]
vm_size = "Standard_B1s"
delete_os_disk_on_termination = true
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServerSemiAnnual"
sku = "Datacenter-Core-1709-smalldisk"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
storage_account_type = "Standard_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "adminuser"
admin_password = "Password1234!"
}
os_profile_windows_config {
disable_password_authentication = false
}
tags = {
environment = "Test"
}
}
尝试按原样将其复制回编辑器中。我看不到任何有问题的字符,具有讽刺意味的是,StackOverflow可能已经帮了你一把,把它们过滤掉了。按字面意思将其复制/粘贴到现有块上可能会纠正这种情况
我在网上看到过很多带有时髦双引号的Terraform示例(这不是ASCII双引号,也不起作用)。这可能就是你所看到的
除此之外,您还需要将代码推送到GitHub或类似的地方,以便我自己查看原始字节。如果有人遇到此错误并在Google上遇到此错误,我想我会发布我的情况以及我是如何修复的 我有一个旧的演示Terraform基础设施,几个月后我重新访问了它,长话短说,我在两天前发布了这个命令,但忘记了它:
terraform plan -out=plan.tf
这将创建计划的zip存档。两天后回来运行terraform init时,我的终端滚动垃圾并“这个字符在语言中不被使用”大约7秒钟。由于.tf的扩展,terraform正在查看zip数据,并迅速将其裤子拉屎
通过将单个tf文件移动到临时目录并使用terraform init检查其有效性,我找到了罪魁祸首,将其删除,并恢复了功能
各位,导出计划文件时要小心 如果有人遇到这个错误并在谷歌上遇到它,我想我会发布我的情况以及我是如何解决的 我有一个旧的演示Terraform基础设施,几个月后我重新访问了它,长话短说,我在两天前发布了这个命令,但忘记了它:
terraform plan -out=plan.tf
这将创建计划的zip存档。两天后回来运行terraform init时,我的终端滚动垃圾并“这个字符在语言中不被使用”大约7秒钟。由于.tf的扩展,terraform正在查看zip数据,并迅速将其裤子拉屎
通过将单个tf文件移动到临时目录并使用terraform init检查其有效性,我找到了罪魁祸首,将其删除,并恢复了功能
各位,导出计划文件时要小心 @dmkvl-请看我在哪里创建了RGI如果你从互联网的某个地方复制粘贴了片段,那么可能会出现一些特殊字符。如果不是这样,那么请忽略我的评论。这个问题怎么样?下面的答案解决了你的问题吗?如果没有,请告诉我们您是否需要进一步的帮助。@Joy:谢谢。。但是我已经解决了这个问题。@dmkvl-请查看我在哪里创建了RGI如果您从internet的某个地方复制粘贴了代码片段,那么可能会出现一些特殊字符。如果不是这样,那么请忽略我的评论。这个问题怎么样?下面的答案解决了你的问题吗?如果没有,请告诉我们您是否需要进一步的帮助。@Joy:谢谢。。但我已经解决了这个问题,这帮了我很大的忙。以前是作为plan.tf导出的,但现在当我作为tf.plan导出时,它可以工作:)。这帮了我很多忙。以前是作为plan.tf导出的,但现在当我作为tf.plan导出时,它可以工作:)。