I use yq to add environment vars from yaml files.
example.yaml:
var0: string0
var1: string1
ary0: ['aryval0', 'aryval1', 'aryval2']
code:
while read -r key val; do
eval "export $key=${val}"
done < <(yq '.[] | key + " " + .' example.yaml)
error:
!!seq (ary0) cannot be added to a !!str ()
desired outcome:
The same yaml file gets read into bash, environment variables, ansible and php at different times for different purposes.
I need to catch array entries when reading the yaml file and convert those array entries into a string representation to export as an environment variable, but I don't know the syntax or methods to do this in yq.
array declaration and values; I don't know how to get the environment variable example to work:
ansible/yaml: ary0: ['aryval0', 'aryval1', 'aryval2']
bash: ary0=('aryval0', 'aryval1', 'aryval2')
php: $ary0 = array('aryval0', 'aryval1', 'aryval2');
environment variable: ary0="aryval0:aryval1:aryval2"
Thanks!
You can do it in a
yqscript file like this:bash.yq
Then run it with yq like:
To get:
Explanation:
.[]matches all top level elements,to join them together( select(kind == "scalar") | key + "='" + . + "'")jointhem with,:map("'" + . + "'") | join(",")Disclaimer: I wrote yq