Using yq to edit yaml files (--in-place, -i flag)
Asked Answered
A

1

16

I'm having a big problem with the edit in place flag for yq version 2.12.0. We are trying to update a value for a variable in one of our .yaml scripts. The before looks like this...

authentication:
  anonymous:
    enabled: false

But we want this

authentication:
  anonymous:
    enabled: true

We have tried to run

sudo yq -y ".authentication.anonymous.enabled |= true" sample.yml

but it overwrites the entire file and just makes it blank :/ Our current workaround is to run

sudo yq -y ".authentication.anonymous.enabled |= true" sample.yml > newfile.yml
sudo cp newfile.yml sample.yml

So basically we create the correct output we want but just push it into a new file and then copy the new contents into the old file (I know it's a whole ordeal). There's gotta be a better way to accomplish this...Can someone show me how to edit the file using the yq --in-place flag properly?

Aestivation answered 26/4, 2021 at 19:20 Comment(2)
Maybe try upgrading since 2.12.0 is two major versions behind of current 4.7.1, which does have -iYawn
Are you using Python yq as opposed to the Go version mikefarah/yq ?Daloris
E
23

yq - written in Go

Using yq you can edit a file in place:

yq -i e '.authentication.anonymous.enabled |= true' sample.yml


yq - written in Python

yq does offer in place editing of yaml files :

From README.MD:

 With -y/-Y, files can be edited in place like with sed -i: yq -yi .foo=1 *.yml

yq -yi '.authentication.anonymous.enabled |= true' sample.yml

Ellene answered 7/7, 2021 at 16:54 Comment(3)
These are not "yq implementations" -- they're different programs that use the same name but have a slightly different syntax for both the expressions and their respective command lines. If you treat them as "implementations" you'll have to learn the fact the hard way. If only because the Python yq wraps jq with everything that entails.Gon
Apologies if I came off rude. But I believe it's best to avoid confusion caused by calling them merely "implementations". I'd use "the yq, written in Go by Mike Farah" and "a different yq written later in Python". For lack of better idea. I am nitpicking, admittedly, but people will otherwise think the two are compatible. They are not.Gon
Thanks for your legitimate comment. I corrected the misleading headlinesEllene

© 2022 - 2024 — McMap. All rights reserved.