simbo1905
simbo1905

Reputation: 6832

Extract yaml file from a variable within a yaml file

I have a simple yaml file:

# replicas: number of pods
replicas: 1
# test
hello: world

I am storing this inside a kubernetes config map:

apiVersion: v1
data:
  helm_values: |
    # replicas: number of pods
    replicas: 1
    # test
    hello: world
kind: ConfigMap
metadata:
  creationTimestamp: 2018-07-30T06:07:38Z

I now want to extract the inner yaml file from the configmap. Trying yq I get:

$ VAR=$(oc get configmap backoffice-deployer -o yaml | yq .data.helm_values)
$ echo $VAR
"# replicas: number of pods\nreplicas: 1\n# test\nhello: world\n"

If I try to echo or printf the VAR into a file it isn't nicely formatted like the original file.

Whats the simplest way in bash to get the extracted data back in the format of the original file in bash?

Upvotes: 1

Views: 5051

Answers (1)

Amadan
Amadan

Reputation: 198314

I can't replicate your code exactly, but here's what I'm getting (OSX, bash v4.4.23, yq v2.1.0):

var=$(cat test.yaml | yq r - data.helm_values)
echo "$var"

with output

# replicas: number of pods
replicas: 1
# test
hello: world

If I forget the quotes, (echo $var), then:

# replicas: number of pods replicas: 1 # test hello: world

(no line breaks, no \n).

I can even further process it:

echo "$var" | yq r - hello
# => world

With yq v2.6.0 the command arguments have changed:

var=$(cat test.yaml | yq -r .data.helm_values)
echo "$var"

Upvotes: 1

Related Questions