-
I need to update in a yaml file parts of a value from values in a second yaml file. specifically using this representative test case: test.yaml:
trigger_payload.yaml:
I need to update each image-id in test.yaml from the image_id given in trigger_payload.yaml if their image names match the full_name and leave them unchanged if they do not match.
I was hoping to find a single yq expression to do this, but alas I can quite wrap my head aroud it. I do have a solution looping over the events in trigger_payload.yaml in a bash loop which I do not like too much. Any hints greately appreciated. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I made a slight change to the trigger_payload, to make it easier to update by looking up the ids given a key: some-backend:
image_id: 22aa
some-frontend:
image_id: 56ab Then we can update the original given the trigger by doing: # use ea to load up both files into memory
./yq ea '
# file[1] is the reference/trigger yaml, lets call that $trig
select(fi==1) as $trig |
# we only want to output the contents of the first file, so select that
select(fi==0) |
# now lets update the container entries
with(.spec.template.spec.containers[];
# here we find the image id in the trigger
$trig[.name].image_id as $newImageId |
# if the trigger exists, we update the image using regex
select($newImageId) | .image |= sub("(.*):.*", "${1}:" + $newImageId) )
' examples/data1.yaml examples/data2.yaml The key bit is here |
Beta Was this translation helpful? Give feedback.
I made a slight change to the trigger_payload, to make it easier to update by looking up the ids given a key:
Then we can update the original given the trigger by doing: