I’m trying to pass string parameter(s) to a Terraform map variable, but receiving error "Invalid number literal". It’s not quite clear how to access keys and values in Terraform maps when passing a Jenkins parameter via terraform apply -var ...
Jenkinsfile:
pipeline {
agent any
parameters {
string(name: 'IP1', defaultValue: '', description: 'Enter first IP address')
}
stages {
stage('Git Checkout') {
steps {
git branch: 'branch1', credentialsId: '', url: 'http://<redacted>.git'
}
}
stage('Deploy Terraform') {
steps {
script {
dir('Linux') {
sh """
terraform init
terraform plan
terraform apply -var 'vms=${params.IP1}'
--auto-approve
"""
}
}
}
}
}
}
}
variables.tf
variable "vm_map" {
type = map(object({
name = string
ip = string
}))
default = {
"first" = {
name = "ubuntu-jenkins1"
ip = "172.30.100.160"
}
"second" = {
name = "ubuntu-jenkins2"
ip = "172.30.100.161"
}
"third" = {
name = "ubuntu-jenkins3"
ip = "172.30.100.162"
}
}
}
2
Answers
I figured it out! You can substitute 'vm1' for any value that identifies the first map object.
If you also wanted to add a name string parameter for the vm name, it would look like
Thank you all for the help! I will leave this in case someone else runs into this problem.
Here’s the terraform apply for multiple vm’s