1

Edit (8/16/2023): I figured it out!

I'm trying to pass string parameter(s) to a Terraform map variable, but receiving error "Invalid number literal".

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 2

1

I figured it out! You can substitute 'vm1' for any value that identifies the first map object.

terraform apply -var vms='''{vm1: {name: "ubuntu",ip: "${params.IP1}"}}''' --auto-approve

If you also wanted to add a name string parameter for the vm name, it would look like

terraform apply -var vms='''{vm1: {name: "${params.VM_NAME1}",ip: "${params.IP1}"}}''' --auto-approve

I will leave this in case someone else runs into this problem.

0

Here's the terraform apply for multiple vm's

terraform apply -var 'vm_map={"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"}}' --auto-approve

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.