2

I'm trying to use yq https://github.com/mikefarah/yq v4.3.2 to add a yaml value in a CloudFormation template like so:

Mappings: 
  RegionMap: 
    us-east-1: 
      AMI: 'ami-YeahRight'

Instead, what I'm getting is:

Mappings: 
  RegionMap: 
    us-east-1: 
      AMI: ami-YeahRight

The style bits in the documentation and from this SO answer yq processing a string with quotation marks made me think that this portion of a bash script would work however the style portion is ignored.

region="us-east-1"
ami="ami-YeahRight"
echo Inserting $ami into $region
yq eval '.Mappings.RegionMap.'"$region"'.AMI='"$ami"' style="single"' -i temp.yaml

I've tried a whole bunch of similar bits but can't seem to crack this nut. Any help here would be greatly appreciated!

Inian
  • 80,270
  • 14
  • 142
  • 161
Patrick Steger
  • 270
  • 2
  • 12

1 Answers1

3

mikefaraq/yq is going through major leap of changes starting from v4 and I'm not surprised that things are breaking in-between.

On v4.4 I can make this work, but using env() function to look-up the variables and use the ..style attribute to set the quoting style

region="us-east-1" ami="ami-YeahRight" yq e '.Mappings.RegionMap.[env(region)] = env(ami) | ..style="single"' yaml
Inian
  • 80,270
  • 14
  • 142
  • 161
  • 1
    You're not kidding about the leap of changes! So I ended up keeping the beginning of the eval string the same for reasons but changing the end to ```| ..style="single"``` worked like a charm. Cheers! – Patrick Steger Jan 15 '21 at 14:21