0

Given yaml config file that looks like this:

key1:
   key11:value1
   key12:value2
key2:
   key21:value3

How can I convert it in a bash script (preferable with yq) to env vars prefixed with a string? Desired output for env:

TF_VAR_key11=value1
TF_VAR_key12=value2
TF_VAR_key21=value3
Liza Shakury
  • 670
  • 9
  • 20
  • That YAML is invalid. There must be spaces between the dictionary keys to values. – Xiddoc Nov 28 '22 at 07:14
  • Do you want yq to set environment variables that way (and have no output), or do you want yq to output text formatted that way (e.g. to be interpreted by the shell)? – pmf Nov 28 '22 at 07:30
  • I want yq to output it in a formatted way, will do the export outside – Liza Shakury Nov 28 '22 at 07:31

4 Answers4

0

You'd first load the YAML configuration into memory.

from yaml import loads

with open("config.yaml") as f:
    config = loads(f.read())

Then, iterate over the dictionary values, which appear to also be dictionaries. For each of these dictionaries, write the key=val pair to the new file.

env_str = ""
for inner_dict in config.values():
    for key, val in inner_dict.items():
        env_str = f"TF_VAR_{key}={val}\n"
Xiddoc
  • 3,369
  • 3
  • 11
  • 37
0

Using python as suggested is easy and readable, if you need to do everything in bash, you can check this thread which has various solutions:
How can I parse a YAML file from a Linux shell script?

Uri Loya
  • 1,181
  • 2
  • 13
  • 34
0

Assuming the missing spaces in the input's subelements between key and value are intentional, so we are just dealing with an array of string values containing :, and separated by whitespace.

yq '.[] | split(" ") | .[] | split(":") | "TF_VAR_" + .[0] + "=" + .[1]' file.yaml

Which implementation of yq are you using? This works for mikefarah/yq. To be used with kislyuk/yq, add the -r option.

pmf
  • 24,478
  • 2
  • 22
  • 31
0

If you are scripting in Bash, you can use yq and jq along with the export command:

#!/bin/bash

# Read the YAML file and convert it to JSON using 'yq' and 'jq' tools
json=$(yq eval '. | tojson' input.yaml)

# Set each key-value pair as an environment variable
while IFS== read -r key value; do
  export "$key"="$value"
done < <(echo "$json" | jq -r 'to_entries[] | "\(.key)=\(.value)"')

# To check the env vars
printenv