10

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?

iej94
  • 191
  • 1
  • 2
  • 8

1 Answers1

16

yq - Go implementation

Using yq you can edit a file in place:

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


yq - Python implementation

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

jpseng
  • 1,618
  • 6
  • 18