Add formatting to a YAML value with yq v4

Viewed 1342

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!

1 Answers

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
Related