【问题标题】:How to iterate a lists in Terraform如何在 Terraform 中迭代列表
【发布时间】:2022-01-01 19:29:45
【问题描述】:

我有密码

resource "oci_core_security_list" "db_security_list" {
  count          = var.deploy_database ? 1 : 0
  compartment_id = var.network_compartment_id
  vcn_id         = var.vcn_id
  freeform_tags  = { "Component" = "Database" }
  display_name   = "test-db-sl"
  ingress_security_rules {
    protocol = "6" # TCP
    source   = var.compute_subnet_cidr
    tcp_options {
      max = "1522"
      min = "1522"
    }
  }
}

目前compute_subnet_cidr 有一个子网 cidr 块

如果 compute_subnet_cidr 是一个列表,我该如何迭代。

compute_subnet_cidr  = ["10.10.10.0/24", "10.10.10.1/24", "10.10.10.2/24"]

如何更改上述代码?

【问题讨论】:

    标签: terraform terraform-provider-oci


    【解决方案1】:

    是的,你可以使用dynamic blocks

    resource "oci_core_security_list" "db_security_list" {
      count          = var.deploy_database ? 1 : 0
      compartment_id = var.network_compartment_id
      vcn_id         = var.vcn_id
      freeform_tags  = { "Component" = "Database" }
      display_name   = "test-db-sl"
    
      dynamic "ingress_security_rules" {    
          for_each   = var.compute_subnet_cidr
          content {
            protocol = "6" # TCP
            source   = ingress_security_rules.value
            tcp_options {
              max = "1522"
              min = "1522"
            }
         }
       }  
    
    }
    

    【讨论】:

    • 它给了我错误:│ │ var.compute_subnet_cidr is tuple with 2 elements │ │ Inappropriate value for attribute "source": string required. ╵
    • @cloudbud 你做了什么改变?为什么var.compute_subnet_cidr 是一个有 2 个元素的元组?在您的问题中,它具有不同的结构。
    • compute_subnet_cidr = ["10.10.10.0/24", "10.10.10.1/24"] 这是我的变量
    • @cloudbud 你确定你使用了我写的代码吗?在我看来,您正在使用它的旧版本或者您更改了某些内容?
    • 是的,我只使用你的代码
    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 2021-01-07
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多