前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Terrafrom函数解析

Terrafrom函数解析

原创
作者头像
oliverpei
发布2020-03-17 23:47:26
2.4K0
发布2020-03-17 23:47:26
举报
文章被收录于专栏:资源编排资源编排

Terraform支持很多内建的函数可以用来处理字符串、数值计算、加密等。调用方式如下:

代码语言:txt
复制
<FUNCTION NAME>(<ARGUMENT 1>, <ARGUMENT 2>)

本文主要对Terraform支持的函数进行总结,简要说明其用处及用法。

数值计算函数

函数名

描述

使用示例

示例结果

abs

计算绝对值

abs(-123.4)

123.4

ceil

向上取整

ceil(5.1)

6

floor

向下取整

floor(4.9)

4

log

计算对数

log(16, 2)

4

max

取最大值

max(12, 54, 3)

54

min

取最小值

min(12, 54, 3...)

3

parseint

将字符串转换为整数

parseint("FF", 16)

255

pow

计算x的y次幂

pow(3, 2)

9

signum

计算数字的正负,正数返回1,负数返回-1,0返回0

signum(-13)

-1

字符串处理函数

函数名

描述

使用示例

示例结果

chomp

移除字符串末尾的换行符

chomp("hello\r\n")

hello

format

格式化字符串

format("Hello, %s!", "world")

Hello, world!

formatlist

格式化字符串,返回字符串列表

format("Hello, %s", "world", "tencent")

"Hello, world", "Hello, tencent"

indent

在多行字符串的每一行前面添加指定数量的空格(统一缩进)

indent(2, "test\nnew")

test\n new

join

把数组中的所有元素放入一个字符串,元素通过指定的分隔符分隔

join (", ", "foo", "bar", "baz")

foo, bar, baz

lower

将字符串中的字母转换为小写

lower("HELLO")

hello

uper

将字符串中的字符转换为大写

upper("hello")

HELLO

regex

正则匹配函数

regex("a-z+", "53453453.345345aaabbbccc23454")

aaabbbccc

regexall

正则匹配函数,返回所有的匹配的字串列表

regexall("a-z+", "1234abcd5678efgh9")

"abcd","efgh"

replace

字符替换函数,支持正则匹配

replace("hello world", "/w.*d/", "everybody")

hello everybody

split

分割字符串

split(",", "foo,bar,baz")

"foo","bar","baz"

strrev

翻转字符串

strrev("hello")

olleh

substr

在字符串中抽取从start下标开始的指定数目的字符

substr("hello world", 1, 4)

ello

title

将给定字符串的每一行首字母大写

title("hello world")

Hello World

trim

移除字符串开头和结尾的指定字符

trim("?!hello?!", "!?")

hello

trimprefix

移除字符串开头的指定字符

trimprefix("helloworld", "hello")

world

trimsuffix

移除字符串结尾的指定字符

trimsuffix("helloworld", "world")

hello

trimspace

移除字符串开头和结尾的空白字符

trimspace(" hello\n\n")

hello

集合函数

函数名

描述

使用示例

示例结果

chunklist

将单个list分割为多个固定大小的list

chunklist(list, chunk_size)

coalesce

返回第一个非空的字符串

coalesce("", "b")

b

coalescelist

返回第一个非空的list

coalescelist([], "c", "d")

"c","d"

compact

移除list中的空字符串

compact("a", "", "b", "c")

"a","b","c"

concat

将多个list合并为一个list

concat("a", "", "b", "c")

"a", "", "b", "c"

contains

判断list中是否包含给定的值

contains("a", "b", "c", "a")

true

distinct

移除list中的重复元素

distinct("a", "b", "a", "c", "d", "b")

"a", "b", "c", "d"

element

返回list指定位置的元素

element("a", "b", "c", 1)

b

flatten

将所有list中的元素合并到一个list

flatten(["a", "b", [], "c"])

"a", "b", "c"

index

找到给定值在list中的位置

index("a", "b", "c", "b")

1

keys

返回map中的所有key

keys({a=1, c=2, d=3})

"a","c","d"

length

返回list, map或string的长度

length("hello")

5

list

将所有参数组成list

list("a", "b", "c")

"a","b","c"

lookup

查找map中的值,如果不存在,返回default

lookup({a="ay", b="bee"}, "a", "what?")

ay

map

将所有参数组成map

map("a", "b", "c", "d")

{"a" = "b", "c" = "d"}

merge

将多个map合并为一个map,并移除重复的key

merge({"a"="b", "c"="d"}, {"e"="f", "c"="z"})

{"a" = "b", "c" = "z", "e" = "f"}

range

根据start, limit, step生成一个list

range(start, limit, step)

reverse

翻转list

reverse(1, 2, 3)

3,2,1

setintersection

返回多个集合中都有的元素集合

setintersection("a", "b", "b", "c", "b", "d")

"b"

setproduct

返回集合的笛卡尔积

setproduct("a", "b")

["a","b"]

setsubtract

返回第一个集合中有的但第二个集合中没有的元素集合

setsubtract("a", "b", "c", "a", "c")

"b"

setunion

返回多个集合的并集

setunion("a", "b", "b", "c", "d")

"d","b","c","a"

slice

从list中取连续的子list

slice("a", "b", "c", "d", 1, 3)

"b","c"

sort

对list按升序排序

sort("e", "d", "a", "x")

"a","d","e","x"

transpose

交换map的key和value

transpose({"a" = "1", "2", "b" = "2", "3"})

{"1" = "a","2" = "a","b","3" = "b"}}

values

返回map的所有value

values({a=3, c=2, d=1})

3,2,1

zipmap

将两个list组合成map

zipmap("a", "b", 1, 2)

{"a" = 1,"b" = 2}

编码函数

函数名

描述

使用示例

示例结果

base64decode

base64解码函数

base64decode("SGVsbG8gV29ybGQ=")

Hello World

base64encode

base64编码函数

base64encode("Hello World")

SGVsbG8gV29ybGQ=

base64gzip

gzip压缩字符串然后base64编码

base64gzip("AAAAAAAAAAAAAAAAAAAAAAAA")

H4sIAGUdX14AA3N0xA64ADb7j4sZAAAA

csvdecode

解码csv格式的字符串数据

csvdecode("a,b,c\n1,2,3")

{"a" = "1","b" = "2","c" = "3"}

jsondecode

json解码函数

jsondecode("{\"hello\": \"world\"}")

{"hello" = "world"}

jsonencode

json编码函数

jsonencode({"hello"="world"})

{"hello":"world"}

urlencode

url编码函数

urlencode("Hello World")

Hello%20World

yamldecode

yaml解码函数

yamldecode("{\"hello\": \"world\"}")

{"hello" = "world"}

yamlencode

yaml编码函数

yamlencode({"a":"b"})

"a": "b"

文件操作函数

函数名

描述

使用示例

示例结果

abspath

返回绝对路径

abspath(path.root)

/home/user/some/terraform/root

dirname

返回文件路径,移除最后的文件名

dirname("foo/bar/baz.txt")

foo/bar

pathexpand

展开用户的HOME目录

pathexpand("~/.ssh/id_rsa")

/home/steve/.ssh/id_rsa

basename

返回路径中的文件名

basename("foo/bar/baz.txt")

baz.txt

file

读取文件并返回文件内容

file("hello.txt")

Hello World

fileexists

判断给定的路径是否存在

fileexists("hello.txt")

true

fileset

正则匹配目录下的文件

fileset(path.module, "files/*.txt")

"files/hello.txt","files/world.txt"

filebase64

读取文件并返回文件内容的base64编码

filebase64("hello.txt")

SGVsbG8gV29ybGQ=

templatefile

读取模板文件并渲染

templatefile(path, vars)

日期时间函数

函数名

描述

使用示例

示例结果

formatdate

格式化日期时间格式,输入日期为RFC 3339格式

formatdate("MMM DD, YYYY", "2018-01-02T23:12:01Z")

Jan 02, 2018

timeadd

加一段时间到日期上,返回一个新日期

timeadd("2019-11-22T00:00:00Z", "10m")

2019-11-22T00:10:00Z

timestamp

返回当前时间

timestamp()

2019-05-13T07:44:12Z

哈希和加密函数

函数名

描述

使用示例

示例结果

base64sha256

计算字符串的SHA256哈希值,然后base64编码

base64sha256("hello world")

uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=

base64sha512

计算字符串的SHA512哈希值,然后base64编码

base64sha512("hello world")

MJ7MSJwS1utMxA9QyQLytNDtd+5RGnx6m808qG1M2G+Y...

bcrypt

计算Blowfish算法加密值

bcrypt("hello world")

$2a$10$D5grTTzcsqyvAeIAnY/mYOIqliCoG7eAMX0/oFcuD.iErkksEbcAa

filebase64sha256

计算文件的SHA256哈希值,然后base64编码

filebase64sha256(filepath)

filebase64sha512

计算文件的SHA512哈希值,然后base64编码

filebase64sha512(filepath)

filemd5

计算文件的md5值

filemd5(filepath)

filesha1

计算文件的sha1值

filesha1(filepath)

filesha256

计算文件的sha256值

filesha256(filepath)

filesha512

计算文件的sha512值

filesha512(filepath)

md5

计算字符串的md5值

md5("hello world")

5eb63bbbe01eeed093cb22bb8f5acdc3

rsadecrypt

解密RSA加密的密文

rsadecrypt(ciphertext, privatekey)

sha1

计算字符串的sha1值

sha1("hello world")

2aae6c35c94fcfb415dbe95f408b9ce91ee846ed

sha256

计算字符串的sha256值

sha256("hello world")

b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

sha512

计算字符串的sha512值

sha512("hello world")

309ecc489c12d6eb4cc40f50c902f2b4d0e...

uuid

随机生成唯一ID

uuid()

b5ee72a3-54dd-c4b8-551c-4bdc0204cedb

uuidv5

根据名称生成唯一ID

uuidv5("url", "https://www.terraform.io/")

9db6f67c-dd95-5ea0-aa5b-e70e5c5f7cf5

IP相关函数

函数名

描述

使用示例

示例结果

cidrhost

根据IP地址前缀和编号计算IP地址

cidrhost("10.12.127.0/20", 16)

10.12.112.16

cidrnetmask

计算子网掩码

cidrnetmask("172.16.0.0/12")

255.240.0.0

cidrsubnet

计算子网地址(具体规则看官方文档)

cidrsubnet("172.16.0.0/12", 4, 2)

172.18.0.0/16

类型转换函数

函数名

描述

使用示例

示例结果

can

表明表达式是否能正确计算出结果

can(local.foo.bar)

false

tobool

转换为布尔值

tobool("true")

true

tolist

转换为list

tolist("a", "b", 3)

"a","b","3"

tomap

转换为map

tomap({"a" = "foo", "b" = true})

{"a" = "foo", "b" = "true"}

tonumber

转换为数字

tonumber("1")

1

toset

转换为set

toset("a", "b", 3)

"a","b","3"

tostring

转换为字符串

tostring(1)

1

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 数值计算函数
  • 字符串处理函数
  • 集合函数
  • 编码函数
  • 文件操作函数
  • 日期时间函数
  • 哈希和加密函数
  • IP相关函数
  • 类型转换函数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档