Hi There,
I’m working in the following Terraform definitions and making use try() for setting default values.
Everything works as expected for any variable set as string and/or list when, in cases of no values are declared, the variable assumes the try() default value (e.g. visibility_level = try(each.value.visibility_level, "private")
The problem lies on Boolean, which the true/false seems to be ignored by try(). If we explicitly declare the variable/value (e.g. allow_merge_on_skipped_pipeline = false
), it works. However, when we comment out or remove the variable, try() does not seem to set the default value set (e.g. try(each.value.allow_merge_on_skipped_pipeline, false)
.
Now I’m wondering… is try() applicable for Boolean values?
Thanks in advance for any clue on that.
projects.auto.tfvars
gitlab_projects = {
"contoso-server" = {
name = "contoso-server"
description = "contoso-server repo"
namespace = "contoso"
default Branch = "main"
mr_approvals_required = 0
...
# allow_merge_on_skipped_pipeline = false # optional
# remove_source_branch_after_merge = true # optional
variables.tf
variable "gitlab_projects" {
type = map(object({
name = string
description = string
namespace = string
default_branch = string
mr_approvals_required = number
...
allow_merge_on_skipped_pipeline = optional(bool)
container_registry_enabled = optional(bool)
...
}))
description = "List of GitLab Projects to be provisioned"
}
main.tf
resource "gitlab_project" "this" {
for_each = var.gitlab_projects
name = each.value.name
description = each.value.description
namespace_id = data.gitlab_group.this[each.key].id
path = try(each.value.path, each.value.name)
visibility_level = try(each.value.visibility_level, "private")
default Branch = each.value.default_branch
allow_merge_on_skipped_pipeline = try(each.value.allow_merge_on_skipped_pipeline, false)
container_registry_enabled = try(each.value.container_registry_enabled, true)
...
}