Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update yaml array using yq

Tags:

yaml

yq

I have the following yaml which I want to replace the command field

Have

apiVersion: batch/v1
kind: Job
metadata:
  name: test004
spec:
  template:
    spec:
      containers:
        - name: "eee"
          image: "test"
          command: ["a", "b", "c"]

want

apiVersion: batch/v1
kind: Job
metadata:
  name: test004
spec:
  template:
    spec:
      containers:
        - name: "eee"
          image: "test"
          command: ["a", "b", "c", "d"]
yq  -i n --style  myfile.yaml 'spec.template.spec.containers[0].command' "["a", "b", "c","d"]"

is there a way to achive this with yq, I try with style without success, If I change it to simple string it works, but not when I want to pass full array, any idea ?

like image 751
Rayn D Avatar asked Apr 07 '26 00:04

Rayn D


1 Answers

You can use the field [+] notation to append fields in yq v3. So your command results in

yq --style double w -i myfile.yaml 'spec.template.spec.containers[0].command[+]' "c"

The above command appends the entry "c" to the array.

Or if you want to update the whole array command you could do in v4. Note that this creates the array entries in separate lines than what is shown in the OP. See YAML Multi-Line Arrays

yq e '.spec.template.spec.containers[0].command |= ["a", "b", "c","d"] | ..style="double"' yaml

Note that yq v4 is already out there and supports much powerful path expressions and operations. See Upgrading from v3 for instructions

like image 180
Inian Avatar answered Apr 09 '26 23:04

Inian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!