YQ 4.7.0 - 如何遍历每个数组元素并更新值

YQ 4.7.0 - How to traverse each array elements and update value

我有一个 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

感谢任何指点。

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

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