carrotcakeslayer
carrotcakeslayer

Reputation: 1008

yaml - Print key and value, if value meets consitions

Given the following yaml:

charts:
  # repository with Helm charts for creation namespaces
  path: ns
  pathMonitoringPrometheus: prom

namespaces:
  first:
    description: "Description of first"
    enabled: false
    branch: master
    bootstrapChart: bootstrap
    syncAccessGroups: []
    namespace:
      role: k8s-role-of-first
      istio: disabled
      public: view
    sources: []

  second:
    description: "Description of second"
    enabled: false
    branch: HEAD
    bootstrapChart: bootstrap
    namespace:
      role: k8s-role-of-second
      istio: 1-13-2
      labels:
        label: second
    sources:
      - http://url.of.second

How could we get a list of namespaces and their istio value if it is different to "disabled".

We are trying to use "yq" tool, but I guess any approach would be ok, although "yq" would be a preferred approach.

second, 1-13-2 

Upvotes: 2

Views: 3558

Answers (2)

ezer
ezer

Reputation: 1172

this will do:

cat  /path/tp/your.yaml |yq -r  '.namespaces | to_entries[] | "\(.key)  \(.value.namespace.istio)"'`

will result:

first  disabled
second  1-13-2

Upvotes: 1

pmf
pmf

Reputation: 36231

Using kislyuk/yq you can base your filter on jq.

  • to_entries splits up the object into an array of key-value pairs
  • select selects those items matching your criteria
  • String interpolation in combination with the -r option puts together your desired output
yq -r '
  .namespaces
  | to_entries[]
  | select(.value.namespace.istio != "disabled")
  | "\(.key), \(.value.namespace.istio)"
'
second, 1-13-2

Using mikefarah/yq the filter is quite similar.

  • to_entries[] has to be split up to_entries | .[]
  • String interpolation is replaced using join and an array
yq '
  .namespaces
  | to_entries | .[]
  | select(.value.namespace.istio != "disabled")
  | [.key, .value.namespace.istio] | join(", ")
'
second, 1-13-2

Upvotes: 2

Related Questions