Reputation: 2860
I have a "create_vm" module which is creating VM plus storage account, NIC etc.
My requirement is I want to pass IP address list from root module to create_vm module so anyone can restrict in bound connections to VM as per their requirement. Correct me I'm not using correct terminology for modules.
Directory structure looks like this:
subscription_1 subscription_2 Modules README.md
./subscription_1:
./subscription_2:
main.tf sqlvm.tf.bak terraform.tfstate terraform.tfstate.backup variables.tf
./Modules:
create_vm
./Modules/create_vm:
main.tf variable.tf
cat ./Modules/create_vm/main.tf
resource "azurerm_network_security_rule" "tf-nsr-5986" {
...
source_address_prefixes = "${var.allowed_source_ips}"
...
}
cat ./Modules/create_vm/variable.tf
variable "allowed_source_ips" {
description = "List of ips from which inbound connection to VMs is allowed"
type = "list"
}
Utilizing this in root module cat ./subscription_2/main.tf
module "vm_app" {
...
allowed_source_ips = "${var.ip_list}"
...
}
cat ./subscription_2/variable.tf
variable "ip_list" {
description = "List of ips from which inbound connection to VMs is allowed"
type = "list"
}
Now, I am running terraform from my local vm, by passing arguments, exactly the way I would on Azure DevOps pipeline
terraform plan -var "resource_group_name=nxt-grp-prd-manage-rgp-au-se" -var "virtual_network_name=virtual_network_1" -var "sql_subnet_name=subnet_1" -var "app_subnet_name=subnet_2" -var "application_nsg=test_nsg" -var "count_vm=2" -var "sql_host_basename=sqlvms" -var "app_host_basename=appvms" -var "storage_account_suffix=sta" -var "virtual_machine_size=Standard_B1ms" -var "virtual_machine_image_publisher=MicrosoftWindowsServer" -var "virtual_machine_image_offer=WindowsServer" -var "virtual_machine_image_sku=2012-R2-Datacenter" -var "virtual_machine_image_version=latest" -var "username=devopsadmin" -var "password=Angular12#$%" -var "ip_list="a.b.c.d","p.q.r.s","x.y.z.l""
Unfortunately, I'm getting the error message as like below:
Error: Invalid number literal
on <value for var.ip_list> line 1:
(source code not available)
Failed to recognize the value of this number literal.
Error: Extra characters after expression
on <value for var.ip_list> line 1:
(source code not available)
An expression was successfully parsed, but extra characters were found after
it.
Has anyone solved this type of challenge before? Not really sure why it is complaining, I'm giving the same way I have given before in create_vm module.
Any help would be much appreciated.
Upvotes: 2
Views: 10415
Reputation: 125
Look for type mismatches between your input variable and what you are actually sending in.
This is a generalized error that tends to surface when the type for an input is a map or list and a string was given. I believe it has something to do with the way Terraform tries to convert inputs to what is set in the input variable.
Example:
Given the following in a variables.tf
in a module named test
variable "foo" {
type = map
}
If you instantiate your module like this:
module "example" {
source = "./test"
foo = <<-EOT
bar
EOT
}
This errors will surface.
In most cases Terraform tries to prevent this, but some things slip through (such as heredoc syntax).
Upvotes: 1
Reputation: 2605
You should assing a list to the ip_list variable, not a string. You're expecting a list but you're assigning a string (that's technically fine, it gets parsed into a list with single string in it. ) and then a comma, which is the extra character.
Instead you should pass a proper list as a parameter, notice the 'array' / [] (square brackets) notation.
... -var "ip_list=["a.b.c.d","p.q.r.s","x.y.z.l"] ...
I'd also strongly encourage you to use a variable file instead. You'll then get some linting/terraform language support. That will allow you to spot mistakes easier before running tf apply in ci/cd.
You would create a file variables.tfvars
:
...
variable "ip_list" {
description = "List of ips ..."
default = ["a.b.c.d","p.q.r.s","x.y.z.l"]
}
...
You can then pass the whole file as a paramter to terraform apply -var-file="./variables.tfvars"
.
Upvotes: 2