如何将Jenkins字符串参数转换为Terraform地图变量?

huangapple go评论75阅读模式
英文:

How to convert Jenkins string parameter to Terraform map variable?

问题

我试图将字符串参数传递给 Terraform 的映射变量,但收到错误消息 "Invalid number literal"。在通过 terraform apply -var ... 传递 Jenkins 参数时,如何访问 Terraform 映射中的键和值并不太清楚。

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"
    }
  }
}
英文:

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: &#39;IP1&#39;, defaultValue: &#39;&#39;, description: &#39;Enter first IP address&#39;)
    }
    
    stages {
        stage(&#39;Git Checkout&#39;) {
            steps {
                git branch: &#39;branch1&#39;, credentialsId: &#39;&#39;, url: &#39;http://&lt;redacted&gt;.git&#39;
            }
        }
        stage(&#39;Deploy Terraform&#39;) {
            steps {
                script {
                        dir(&#39;Linux&#39;) {
                                sh &quot;&quot;&quot;
                                terraform init
                                terraform plan
                                terraform apply -var &#39;vms=${params.IP1}&#39; \
                                --auto-approve
                                &quot;&quot;&quot;
                            }
                    } 
                }
            }
        }
    }
}

variables.tf

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;
    }
  }
}

答案1

得分: 1

我弄清楚了!你可以将'vm1'替换为标识第一个地图对象的任何值。

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

如果你还想为虚拟机名称添加一个名字字符串参数,它将如下所示:

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

这是用于多个虚拟机的terraform apply命令:

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
英文:

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

terraform apply -var vms=&#39;&#39;&#39;{vm1: {name: &quot;ubuntu&quot;,ip: &quot;${params.IP1}&quot;}}&#39;&#39;&#39; --auto-approve

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

terraform apply -var vms=&#39;&#39;&#39;{vm1: {name: &quot;${params.VM_NAME1}&quot;,ip: &quot;${params.IP1}&quot;}}&#39;&#39;&#39; --auto-approve

Here's the terraform apply for multiple vm's

terraform apply -var &#39;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;}}&#39; --auto-approve

huangapple
  • 本文由 发表于 2023年8月5日 11:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定