-1

我有一个 yaml:

global:
  resolve_timeout: 5m
receivers:
- name: alerts-null
- name: default
  local_configs:
  - api_url: https://abx.com
    channel: '#abx'
    send_resolved: true      
    username: abc-123
- name: devops-alerts
  local_configs:
  - api_url: https://abx.com
    channel: '#abx'
    send_resolved: true      
    username: abc-123

yaml 可以在数组中有多个“name:”元素,我想循环所有“name”元素并将键“username:”的值更改为“xyz-321”。生成的 YAML 应如下所示:

global:
  resolve_timeout: 5m
receivers:
- name: alerts-null
- name: default
  local_configs:
  - api_url: https://abx.com
    channel: '#abx'
    send_resolved: true      
    username: xyz-321
- name: devops-alerts
  local_configs:
  - api_url: https://abx.com
    channel: '#abx'
    send_resolved: true      
    username: xyz-321

我尝试使用以下 yq 命令,但它没有更改所需键的值:

yq eval '(.receivers[] | select(.name.local_configs.username)) = "xyz-321"' source.yaml > manipulated.yaml

任何指针表示赞赏。

4

1 回答 1

1

如果您将索引更改为如下所示:

global:
  resolve_timeout: 5m
receivers:
- name: alerts-null
  internal_config:
    username: abc-123
- name: default
  internal_config:
    username: abc-123

然后流动将起作用:

yq e '(.receivers[].internal_config.username) |= "new_username"' myFile.yaml

于 2021-08-09T21:36:25.113 回答