-1

I have a 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

The yaml can have multiple "name:" elements in the array and I want to loop all "name" elements and change the value for key "username:" to "xyz-321". Resultant YAML should be as follows:

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

I tried to use following yq command, but it did not changed the desired key's value:

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

Any pointers are appreciated.

Himanshu Singh
  • 199
  • 3
  • 15
  • Why is `internal_config:` empty? Your input isn't even valid YAML. Consider validating it first in http://www.yamllint.com/ – Inian Aug 09 '21 at 18:25

1 Answers1

1

If you change the indexing to look like this:

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

Then the flowing will work:

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