分割逗号分隔的字符串

时间:2018-07-13 14:53:18

标签: azure terraform

我的输出是用逗号分隔的多值字符串。

input.tf

resource "azurerm_app_service" "testap" {
  name                = "MySuperCoolAppServer001"
  location            = "eastus"
  resource_group_name = "notshown"
  app_service_plan_id = "notshown"
}

output.tf

output "output_tf_testap_outbound_ip_addresses" {
  value = "${azurerm_app_service.testap.outbound_ip_addresses}"
}

我在控制台中得到了这个

  

output_tf_testap_outbound_ip_addresses =   1.2.3.4,1.2.3.5,1.2.3.6,1.2.3.7,1.2.3.8,1.2.3.9

如何获得列表的第一项?在这种情况下,我试图隔离该值:

  

1.2.3.4

当在运行时之前不知道项目总数时,是否有办法获取所有项目的“集合”? (上面的列表有6个项目。)

以下代码似乎无效:

output "first_ip" {
  value = ["${azurerm_app_service.testap.outbound_ip_addresses[0]}"]
}

===================== APPEND ===============

first_ip_no_index有效。 first_ip没有

output "first_ip_no_index" {
  value = ["${split(",", azurerm_app_service.tf_middle_tier_azurerm_app_service.outbound_ip_addresses)}"]
}

output "first_ip" {
  value = "${split(",", azurerm_app_service.tf_middle_tier_azurerm_app_service.outbound_ip_addresses)[0]}"
}

first_ip产生此错误:

  

读取输出first_ip的配置时出错:解析错误为1:91:   预期为“}”,但发现为“ [”

1 个答案:

答案 0 :(得分:1)

您可以使用split() function将字符串拆分为列表。

move "other_folder" %%~ni

之后,您可以使用element(list, index) syntax将其编入索引:

# assuming list is sorted:
pattern = ["ABCDE",
"ABCDEFG",
"ABCDEFGH",
"ABCDEFGHIJKLMNO",
"CEST",
"DBTSFDE",
"DBTSFDEO",
"EOEUDNBNUW",
"EAEUDNBNUW",
"FG",
"FGH"]

pattern = list(reversed(pattern))

def iterate_patterns():
    while pattern:
        i = pattern.pop()
        throw_it_away = False
        for p in pattern:
            if p.startswith(i):
                throw_it_away = True
                break
        if throw_it_away == False:
            yield i

print(list(iterate_patterns()))

您通常还应该能够像这样使用list\[index\] syntax

output "output_tf_testap_outbound_ip_addresses" {
  value = ["${split(",", azurerm_app_service.testap.outbound_ip_addresses)}"]
}

然而,Terraform 0.11中似乎存在一个错误,该错误阻止切片output "first_ip" { value = "${element(split(",", azurerm_app_service.testap.outbound_ip_addresses), 0}" } 函数的结果,并引发以下错误:

  

错误:加载/tmp/tf-split-test/main.tf时出错:读取配置时出错   对于输出foo:1:25解析错误:预期为“}”,但发现为“ [”

如果您希望在output "first_ip" { value = "${split(",", azurerm_app_service.testap.outbound_ip_addresses)[0]}" } 函数上使用此语法,则可以使用local拆分列表,然后对其进行切片以解决此问题。

split