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

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

How to convert Jenkins string parameter to Terraform map variable?

问题

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

Jenkinsfile:

  1. pipeline {
  2. agent any
  3. parameters {
  4. string(name: 'IP1', defaultValue: '', description: 'Enter first IP address')
  5. }
  6. stages {
  7. stage('Git Checkout') {
  8. steps {
  9. git branch: 'branch1', credentialsId: '', url: 'http://<redacted>.git'
  10. }
  11. }
  12. stage('Deploy Terraform') {
  13. steps {
  14. script {
  15. dir('Linux') {
  16. sh """
  17. terraform init
  18. terraform plan
  19. terraform apply -var 'vms=${params.IP1}' --auto-approve
  20. """
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }

variables.tf:

  1. variable "vm_map" {
  2. type = map(object({
  3. name = string
  4. ip = string
  5. }))
  6. default = {
  7. "first" = {
  8. name = "ubuntu-jenkins1"
  9. ip = "172.30.100.160"
  10. }
  11. "second" = {
  12. name = "ubuntu-jenkins2"
  13. ip = "172.30.100.161"
  14. }
  15. "third" = {
  16. name = "ubuntu-jenkins3"
  17. ip = "172.30.100.162"
  18. }
  19. }
  20. }
英文:

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:

  1. pipeline {
  2. agent any
  3. parameters {
  4. string(name: &#39;IP1&#39;, defaultValue: &#39;&#39;, description: &#39;Enter first IP address&#39;)
  5. }
  6. stages {
  7. stage(&#39;Git Checkout&#39;) {
  8. steps {
  9. git branch: &#39;branch1&#39;, credentialsId: &#39;&#39;, url: &#39;http://&lt;redacted&gt;.git&#39;
  10. }
  11. }
  12. stage(&#39;Deploy Terraform&#39;) {
  13. steps {
  14. script {
  15. dir(&#39;Linux&#39;) {
  16. sh &quot;&quot;&quot;
  17. terraform init
  18. terraform plan
  19. terraform apply -var &#39;vms=${params.IP1}&#39; \
  20. --auto-approve
  21. &quot;&quot;&quot;
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }
  28. }

variables.tf

  1. variable &quot;vm_map&quot; {
  2. type = map(object({
  3. name = string
  4. ip = string
  5. }))
  6. default = {
  7. &quot;first&quot; = {
  8. name = &quot;ubuntu-jenkins1&quot;
  9. ip = &quot;172.30.100.160&quot;
  10. }
  11. &quot;second&quot; = {
  12. name = &quot;ubuntu-jenkins2&quot;
  13. ip = &quot;172.30.100.161&quot;
  14. }
  15. &quot;third&quot; = {
  16. name = &quot;ubuntu-jenkins3&quot;
  17. ip = &quot;172.30.100.162&quot;
  18. }
  19. }
  20. }

答案1

得分: 1

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

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

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

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

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

  1. terraform apply -var 'vm_map={"first": {"name": "ubuntu-jenkins1", "ip": "172.30.100.160"}, \
  2. "second": {"name": "ubuntu-jenkins2", "ip": "172.30.100.161"}, \
  3. "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.

  1. 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

  1. 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

  1. 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;}, \
  2. &quot;second&quot;: {&quot;name&quot;: &quot;ubuntu-jenkins2&quot;, &quot;ip&quot;: &quot;172.30.100.161&quot;}, \
  3. &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:

确定