英文:
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: '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"
}
}
}
答案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='''{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
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论