<p>I figured it out! You can substitute 'vm1' for any value that identifies the first map object.</p>
<pre><code>terraform apply -var vms='''{vm1: {name: &quot;ubuntu&quot;,ip: &quot;${params.IP1}&quot;}}''' --auto-approve
</code></pre>
<p>If you also wanted to add a name string parameter for the vm name, it would look like</p>
<pre><code>terraform apply -var vms='''{vm1: {name: &quot;${params.VM_NAME1}&quot;,ip: &quot;${params.IP1}&quot;}}''' --auto-approve
</code></pre>
<p>I will leave this in case someone else runs into this problem.</p>

CC BY-SA 4.0
2023-08-07T13:27:40.413Z
2023-08-07T13:35:51.583Z
2023-08-07T13:35:51.583Z
Mark Matlock
<p>Here's the terraform apply for multiple vm's</p>
<pre><code>terraform apply -var 'vm_map={&quot;first&quot;: {&quot;name&quot;: &quot;ubuntu-jenkins1&quot;, &quot;ip&quot;: &quot;172.30.100.160&quot;}, \
                                                          &quot;second&quot;: {&quot;name&quot;: &quot;ubuntu-jenkins2&quot;, &quot;ip&quot;: &quot;172.30.100.161&quot;}, \
                                                          &quot;third&quot;: {&quot;name&quot;: &quot;ubuntu-jenkins3&quot;, &quot;ip&quot;: &quot;172.30.100.162&quot;}}' --auto-approve
</code></pre>

CC BY-SA 4.0
2023-08-16T17:21:23.323Z
2023-08-16T17:21:23.323Z
Mark Matlock
<p>Edit (8/16/2023): I figured it out!</p>
<p>I'm trying to pass string parameter(s) to a Terraform map variable, but receiving error &quot;Invalid number literal&quot;.</p>
<p>Jenkinsfile:</p>
<pre><code>pipeline {
    agent any

    parameters {
        string(name: 'IP1', defaultValue: '', description: 'Enter first IP address')
    }
    
    stages {
        stage('Git Checkout') {
            steps {
                git branch: 'branch1', credentialsId: '', url: 'http://&lt;redacted&gt;.git'
            }
        }
        stage('Deploy Terraform') {
            steps {
                script {
                        dir('Linux') {
                                sh &quot;&quot;&quot;
                                terraform init
                                terraform plan
                                terraform apply -var 'vms=${params.IP1}' \
                                --auto-approve
                                &quot;&quot;&quot;
                            }
                    } 
                }
            }
        }
    }
}
</code></pre>
<p>variables.tf</p>
<pre><code>variable &quot;vm_map&quot; {
  type = map(object({
    name = string
    ip   = string
  }))
  default = {
    &quot;first&quot; = {
      name = &quot;ubuntu-jenkins1&quot;
      ip   = &quot;172.30.100.160&quot;
    }
    &quot;second&quot; = {
      name = &quot;ubuntu-jenkins2&quot;
      ip   = &quot;172.30.100.161&quot;
    }
    &quot;third&quot; = {
      name = &quot;ubuntu-jenkins3&quot;
      ip   = &quot;172.30.100.162&quot;
    }
  }
}
</code></pre>

<jenkins><terraform><jenkins-pipeline><jenkinsfile><infrastructure-as-code>
How To Convert Jenkins String Parameter To Terraform Map Variable? (Invalid number literal)
Mark Matlock
# How To Convert Jenkins String Parameter To Terraform Map Variable? (Invalid number literal)

(Asked by: username_0 on 2023-08-04)

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://.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"
    }
  }
}

**Tags:** jenkins, terraform, jenkins-pipeline, jenkinsfile, infrastructure-as-code


---

## 2 answers

### Answer by username_0 on 2023-08-07. Score: 1 | Accepted answer

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.


---

### Answer by username_0 on 2023-08-16. Score: 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


---

