我在 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 檔案)但沒有成功。
uj5u.com熱心網友回復:
就像一個虛擬的例子,給定以下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, "/^# (.*)$/", "$1")] )
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/420685.html
標籤:
下一篇:正則運算式掃描檔案以獲取特定內容
