Terraform - 函数

Functions函数

数值函数

- max()  获取最大值
- min()  获取最小值
- parseint()  解析字符串数字为对应进制的整数

字符串函数

- format()  格式化
- split()  字符串分割
- join()  字符串拼接
- replace()  替换字符串

Collection集合函数

- alltrue()  判断元素中都是true
- anytrue()  判断元素中存在true
- chunklist(list, size)  按照指定size拆分list
- compact(list)  去除list中的空元素
- concat(list1, list2)  合并两个list
- contains(list, value)  判断元素是否存在list中
- distinct(list)  去除list中的重复元素
- element(list, index)  根据index获取list中的元素
- list[index]  根据index获取list中的元素
- flatten(list,list,list)  将多层list转换成单list
- index(list, value)  返回value元素的index
- length(list)  计算list的长度
- lookup(map,key,default)  检索map的key,不存在返口default
- merge(map1,map2,map3)  合并map,相同key会被最后覆盖
- keys(map)  创建map中key的list
- values(map)  创建map中value的list

示例

output.tf


//内置数值函数
output "my_func1" {
  value = {
    f1 = [max(1, 2, 3), min(7, 8, 9), parseint("111", 2)],
    f2 = [max([1, 2, 3]...), min([7, 8, 9]...), parseint("111", 2)]
  }
}

//内置字符串函数
output "my_func2" {
  value = {
    f1 = format("Hello %s!", "anliven"),
    f2 = join(":", ["name", "age", "title"]),
    f3 = replace("a=b=c", "=", "–"),
    f4 = split(",", "name,age,title")
  }
}

locals {
  list1 = [1, 2, 3]
  list2 = [7, 8, 9, null]
  list3 = ["a", "b", "c", "a", "b", "c"]
}
//内置Collection集合函数
output "my_func3" {
  value = {
    p1 = alltrue([true, false]) //判断元素中都是true
    p2 = anytrue([])            //判断元素中存在true

    p3  = chunklist(["a", "b", "c"], 2)      //按照指定size拆分list
    p4  = compact(local.list2)               //去除list中的空元素
    p5  = concat(local.list1, local.list2)   //合并两个list
    p6  = contains(local.list2, 9)           //判断元素是否存在list中
    p7  = distinct(local.list3)              //去除list中的重复元素
    p8  = element(local.list3, 0)            //根据index获取list中的元素
    p9  = flatten([[["a", "b"], []], ["c"]]) //将多层list转换成单list
    p10 = index(local.list3, "c")            //返回value元素的index, 返回第一个匹配的索引

    p11 = length({ "a" = 1, "b" = 2 })                      //计算string、list、map的长度
    p12 = lookup({ a = "111", b = "222" }, "c", "what?")    //检索map的key,不存在返口default
    p13 = merge({ a = "b", c = "d" }, { e = "f", c = "z" }) //合并map,相同key会被最后覆盖
    p14 = keys({ a = 1, c = 2, d = 3 })                     //创建map中key的list
    p15 = values({ a = 3, c = 2, d = 1 })                   //创建map中value的list

  }
}

输出结果

liven@livenan MINGW64 /d/Testing/tf-files/tf-learn-functions
$ terraform plan

Changes to Outputs:
  + my_func1 = {
      + f1 = [  
          + 3,  
          + 7,  
          + 7,  
        ]       
      + f2 = [  
          + 3,  
          + 7,
          + 7,
        ]
    }
  + my_func2 = {
      + f1 = "Hello anliven!"
      + f2 = "name:age:title"
      + f3 = "a–b–c"
      + f4 = [
          + "name",
          + "age",
          + "title",
        ]
    }
  + my_func3 = {
      + p1  = false
      + p10 = 2
      + p11 = 2
      + p12 = "what?"
      + p13 = {
          + a = "b"
          + c = "z"
          + e = "f"
        }
      + p14 = [
          + "a",
          + "c",
          + "d",
        ]
      + p15 = [
          + 3,
          + 2,
          + 1,
        ]
      + p2  = false
      + p3  = [
          + [
              + "a",
              + "b",
            ],
          + [
              + "c",
            ],
        ]
      + p4  = [
          + "7",
          + "8",
          + "9",
        ]
      + p5  = [
          + 1,
          + 2,
          + 3,
          + 7,
          + 8,
          + 9,
          + null,
        ]
      + p6  = true
      + p7  = [
          + "a",
          + "b",
          + "c",
        ]
      + p8  = "a"
      + p9  = [
          + "a",
          + "b",
          + "c",
        ]
    }

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

liven@livenan MINGW64 /d/Testing/tf-files/tf-learn-functions
$
posted @ 2024-08-23 11:02  Anliven  阅读(12)  评论(0编辑  收藏  举报