Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2023-08-07T13:27:40.413Z
Answers: 2023-08-07T13:35:51.583Z
Answers: 2023-08-07T13:35:51.583Z
Answers: Mark Matlock
Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2023-08-16T17:21:23.323Z
Answers: 2023-08-16T17:21:23.323Z
Answers: Mark Matlock
Body: <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>
  
Tags: <jenkins><terraform><jenkins-pipeline><jenkinsfile><infrastructure-as-code>
Title: How To Convert Jenkins String Parameter To Terraform Map Variable? (Invalid number literal)
UserDisplayName: Mark Matlock
ThreadText: # 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://<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"
      }
    }
  }
  
  ```
  
  **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
  
  ```
  
  
  ---
  
  