terraform 中的正则表达式替换函数取消注释 YAML 文件中的多行
regex in terraform replace function to uncomment multiple lines in a YAML file
我在 YAML 文件中有以下三行:
# upstream_dns_servers:
# - 8.8.8.8
# - 8.8.4.4
我想从每行的开头删除哈希符号,同时保留哈希符号后面的所有内容,即得到:
upstream_dns_servers:
- 8.8.8.8
- 8.8.4.4
这应该可以通过 HCL 替换函数和它支持的正则表达式方言实现,我尝试使用 (?:x) 语法搜索字符串(非捕获子模式 - 根据 Terraform 文档)成功。
作为一个虚拟示例,给定以下 test.yml
文件
some_key: toto
# upstream_dns_servers:
# - 8.8.8.8
# - 8.8.4.4
other_key: titi
main.tf
中的以下条目完美地完成了工作:
data "local_file" "config" {
filename = "test.yml"
}
resource "local_file" "config_changed" {
content = join("\n", [for line in split("\n", data.local_file.config.content) : replace(line, "/^# (.*)$/", "")] )
filename = "test-copy.yml"
}
结果:
$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
+ create
Terraform will perform the following actions:
# local_file.config_changed will be created
+ resource "local_file" "config_changed" {
+ content = <<-EOT
some_key: toto
upstream_dns_servers:
- 8.8.8.8
- 8.8.4.4
other_key: titi
EOT
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "test-copy.yml"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
local_file.config_changed: Creating...
local_file.config_changed: Creation complete after 0s [id=cbf2811f5394ed4e55e0fa986432ae077790a6c8]
$ cat test-copy.yml
some_key: toto
upstream_dns_servers:
- 8.8.8.8
- 8.8.4.4
other_key: titi
我在 YAML 文件中有以下三行:
# upstream_dns_servers:
# - 8.8.8.8
# - 8.8.4.4
我想从每行的开头删除哈希符号,同时保留哈希符号后面的所有内容,即得到:
upstream_dns_servers:
- 8.8.8.8
- 8.8.4.4
这应该可以通过 HCL 替换函数和它支持的正则表达式方言实现,我尝试使用 (?:x) 语法搜索字符串(非捕获子模式 - 根据 Terraform 文档)成功。
作为一个虚拟示例,给定以下 test.yml
文件
some_key: toto
# upstream_dns_servers:
# - 8.8.8.8
# - 8.8.4.4
other_key: titi
main.tf
中的以下条目完美地完成了工作:
data "local_file" "config" {
filename = "test.yml"
}
resource "local_file" "config_changed" {
content = join("\n", [for line in split("\n", data.local_file.config.content) : replace(line, "/^# (.*)$/", "")] )
filename = "test-copy.yml"
}
结果:
$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
+ create
Terraform will perform the following actions:
# local_file.config_changed will be created
+ resource "local_file" "config_changed" {
+ content = <<-EOT
some_key: toto
upstream_dns_servers:
- 8.8.8.8
- 8.8.4.4
other_key: titi
EOT
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "test-copy.yml"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
local_file.config_changed: Creating...
local_file.config_changed: Creation complete after 0s [id=cbf2811f5394ed4e55e0fa986432ae077790a6c8]
$ cat test-copy.yml
some_key: toto
upstream_dns_servers:
- 8.8.8.8
- 8.8.4.4
other_key: titi