简体   繁体   中英

How to add yaml to file.yaml in a specific place?

I'm using yq and i'm struggling to figure out the below problem.

Given a file.yaml containing the below:

apiVersion: v1
clusters:
  - cluster:
      age: 50
      server: dev
    name: apple
  - cluster:
      age: 50
      server: prod
    name: orange

what is the yq command i need to replace file.yaml with a file that adds yaml for the cluster where name: orange only, so that file.yaml looks like this afterwards:

apiVersion: v1
clusters:
  - cluster:
      age: 50
      server: dev
    name: apple
  - cluster:
      age: 50
      server: prod
      extensions:
      - name: xx.io
        extension:
          subnet:
          - 1.2.3.4/32
    name: orange

Which implementation of yq do you mean? In any case, select can pick the right object, += will update.

Using mikefarah/yq :

yq '(.clusters[] | select(.name == "orange")).cluster += load("extension.yaml")' file.yaml

Using kislyuk/yq :

yq -y '(.clusters[] | select(.name == "orange")).cluster += input' file.yaml extension.yaml

Input file.yaml :

apiVersion: v1
clusters:
  - cluster:
      age: 50
      server: dev
    name: apple
  - cluster:
      age: 50
      server: prod
    name: orange

Input extension.yaml :

extensions:
- name: xx.io
  extension:
    subnet:
    - 1.2.3.4/32

Output:

apiVersion: v1
clusters:
  - cluster:
      age: 50
      server: dev
    name: apple
  - cluster:
      age: 50
      server: prod
      extensions:
        - name: xx.io
          extension:
            subnet:
              - 1.2.3.4/32
    name: orange

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM