【问题标题】:Terraform module for storage management policy, selective actions用于存储管理策略、选择性操作的 Terraform 模块
【发布时间】:2020-07-10 17:10:02
【问题描述】:

我正在尝试通过模块的方式使用 terraform azurerm_storage_management_policy 资源,因此我不必在我的代码中重复自己。

我有一种情况需要在某些文件夹上设置以下所有内容: tier_to_cool_after_days_since_modification_greater_than = var.days_to_cool tier_to_archive_after_days_since_modification_greater_than = var.days_to_archive delete_after_days_since_modification_greater_than = var.days_to_delete

但在某些文件夹上我只需要设置delete_after_days_since_modification_greater_than

我想在我的主配置中不为此环境定义资源的情况下执行此操作,但我没有找到方法来执行此操作。我看到 github 请求 terraform 添加 -1,但被拒绝了。

当我有一个列出所有三个的模块时,有没有一种方法可以不必定义所有三个?

【问题讨论】:

    标签: azure terraform azure-storage terraform-provider-azure


    【解决方案1】:

    根据您的要求,在某些文件夹上设置以下所有内容,仅在其他一些文件夹上设置delete_after_days_since_modification_greater_than。您可以在两个分区中定义虚拟文件夹,并使用for 循环在每个规则中迭代虚拟文件夹。您可以使用prefix_match 过滤选择的文件夹。

    这是一个工作示例供您参考。

    locals {
      folderlist1= ["test1","test2","test3"] 
      folderlist2 = [ "foo1","foo2"]
    
      list1 = [
          for a in local.folderlist1: 
          "container1/${a}"
      ]
    
      list2 = [
          for b in local.folderlist2: 
          "container1/${b}"
      ]
    
    }
    
    
    output "result1" {
        value = local.list1
    }
    
    output "result2" {
      value = local.list2
    }
    
    
    resource "azurerm_storage_management_policy" "example" {
      storage_account_id = azurerm_storage_account.example.id
    
    
      rule {
        name    = "rule1"
        enabled = true
        filters {
          prefix_match = local.list1
          blob_types   = ["blockBlob"]
        }
        actions {
          base_blob {
            tier_to_cool_after_days_since_modification_greater_than    = 10
            tier_to_archive_after_days_since_modification_greater_than = 50
            delete_after_days_since_modification_greater_than          = 100
          }
          # snapshot {
          #   delete_after_days_since_creation_greater_than = 30
          # }
        }
      }
      
      rule {
        name    = "rule2"
        enabled = true
        filters {  
          prefix_match = local.list2 
          blob_types   = ["blockBlob"]
        }
        actions {
          base_blob {
            # tier_to_cool_after_days_since_modification_greater_than    = 11
            # tier_to_archive_after_days_since_modification_greater_than = 51
            delete_after_days_since_modification_greater_than          = 101
          }
          # snapshot {
          #   delete_after_days_since_creation_greater_than = 31
          # }
        }
      }
    }
    

    结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2020-03-31
      • 2022-12-22
      • 2020-03-29
      • 1970-01-01
      • 2021-04-04
      相关资源
      最近更新 更多