I am trying to append new entries to the following yaml:
destinations:
- namespace: ns1
server: https://kubernetes.default.svc
What I expect is:
destinations:
- namespace: ns1
server: https://kubernetes.default.svc
- namespace: ns2
server: https://kubernetes.default.svc-
- namespace: ns3
server: https://kubernetes.default.svc
and I can get is, if I do like this
yq -i '.spec.destinations += [{"namespace": "zi-test-customer", "server": "https://kubernetes.default.svc"},{"namespace": "zi-test-customer2", "server": "https://kubernetes.default.svc"}]' file.yaml
But if I take the list from env varible I get wrong list
What I am getting is:
destinations:
- namespace: ns1
server: https://kubernetes.default.svc
- {"namespace": "ns2", "server": "https://kubernetes.default.svc"}
- {"namespace": "ns3", "server": "https://kubernetes.default.svc"}
If I do this
NS=[{"namespace": "ns2", "server": "https://kubernetes.default.svc"},{"namespace": "ns3", "server": "https://kubernetes.default.svc"}]
yq -i '.spec.destinations += env(NS)' file.yaml
How extract values from env varible properly?
What you see as "wrong result" is nothing but a different representation. You're importing a list in JSON format, and its "style" is retained. But the actual data stored is the same in both your expected and your "wrong" result.
If you want to remove the style, mikefarah/yq provides the style
operator. Remove it by setting it to an empty string:
NS='[{"namespace": "ns2", "server": "https://kubernetes.default.svc"},{"namespace": "ns3", "server": "https://kubernetes.default.svc"}]'
NS="$NS" yq '.spec.destinations += env(NS) | ... style=""' file.yaml
spec:
destinations:
- namespace: ns1
server: https://kubernetes.default.svc
- namespace: ns2
server: https://kubernetes.default.svc
- namespace: ns3
server: https://kubernetes.default.svc