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

26.3k views Asked by At

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?

1

There are 1 answers

3
jpseng On

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