I try to have entries by searching for a boolean with yq - yaml

Good evening!
I am trying to get with yq the entries looking for a boelan value inside a yml.
e.g:
yq '.applications | to_entries | .[] | select( .persistence == "true")' manifest.yml
But this does not return anything... this would be the yml where I look for:
applications:
a:
enabled: true
project: ../../../aproj
version: v1.0.1
persistence: true
b:
enabled: false
project: ../../bproj
version: v1.0.1
c:
enabled: true
project: ../../../cproj
version: v1.0.1
persistence: true
the output should be the following entries: a,c

Related

yq update yaml array using other yaml file's array

I dont have any detail knowledge on yq.
template1.yaml
spec:
template:
temp:
vars:
- name: first
env: []
template2.yaml
env:
-name: "first"
value: 1
-name: "two"
value: 2
I want to add env array of template2.yaml to template1.yaml's env array using yq. How can we do this ??
Which tool called yq are you using?
Using mikefarah/yq (tested with v4.20.2):
yq '
.spec.template.temp.vars[].env += load("template2.yaml").env
' template1.yaml
Using kislyuk/yq (tested with v3.0.2):
yq -y '
.spec.template.temp.vars[].env += input.env
' template1.yaml template2.yaml
Output:
spec:
template:
temp:
vars:
- name: first
env:
- name: "first"
value: 1
- name: "two"
value: 2
Note: This assumes, your template2.yaml looks more like this:
env:
- name: "first"
value: 1
- name: "two"
value: 2

YQ - replace values of subfields of list based on list of values

Given a list in YAML, I want to dynamically replace all values of source.targetRevison's based on a input list to an input variable:
replace_list=[argocd,argocd-projects]
replace_to_value=v1.0.0
yq version 4.2.x+
Input YAML:
server:
additionalApplications:
- name: argocd
path: argocd/argocd-install
source:
targetRevision: feature/3-dns
- name: argocd-projects
source:
path: argocd/argocd-projects
targetRevision: feature/3-dns
syncPolicy:
automated:
selfHeal: true
prune: true
- name: argocd-tools-aks-apps
namespace: argocd
destination:
namespace: argocd
server: https://kubernetes.default.svc
project: argocd
source:
targetRevision: HEAD
syncPolicy:
automated:
selfHeal: true
prune: true
Expected output:
server:
additionalApplications:
- name: argocd
path: argocd/argocd-install
source:
targetRevision: v1.0.0
- name: argocd-projects
source:
path: argocd/argocd-projects
targetRevision: v1.0.0
syncPolicy:
automated:
selfHeal: true
prune: true
- name: argocd-tools-aks-apps
namespace: argocd
destination:
namespace: argocd
server: https://kubernetes.default.svc
project: argocd
source:
targetRevision: HEAD
syncPolicy:
automated:
selfHeal: true
prune: true
Current progress:
yq '.server.additionalApplications.[] | (.source.targetRevision)' argocd/argocd-install/values-override.yaml
gives me:
feature/3-dns
feature/3-dns
HEAD
you can do this:
yq '( .server.additionalApplications[] |
select(.name == ("argocd", "argocd-projects")) |
.source.targetRevision )
|= "v1.0.0"' examples/data1.yaml
Explanation:
You want to update a selection of the additionalApplication entries.
First you navigate to those entries and expand them .server.additionalApplications[]
Next, we filter them by the name: select(.name == ("argocd", "argocd-projects")
Now we can update their .source.targetRevision property.
Importantly, note that the whole LHS expression is in brackets, as that's what we want to pass to the 'update' (|=) operator. If you don't put it in brackets, then you will see that it seems to discard everything else (because it filters first, then updates separately).
Hope that makes sense!
Disclaimer: I wrote yq.

Remove certain fields from a YAML map object using yq

I have some tricky thing that I need to do for a yaml file, this is how it looks before
Before
apiVersion: v1
items:
- apiVersion: core.k8s.com/v1alpha1
kind: Test
metadata:
creationTimestamp: '2022-02-097T19:511:11Z'
finalizers:
- cuv.ssf.com
generation: 1
name: bar
namespace: foo
resourceVersion: '12236'
uid: 0117657e8
spec:
certificateIssuer:
acme:
email: myemail
provider:
credentials: dst
type: foo
domain: vst
type: bar
status:
conditions:
- lastTransitionTime: '2022-02-09T19:50:12Z'
message: test
observedGeneration: 1
reason: Ready
status: 'True'
type: Ready
lastOperation:
description: test
state: Succeeded
type: Reconcile
https://codebeautify.org/yaml-validator/y22fe4943
I need to remove all the fields under section metadata and the tricky part is to keep only the name and namespace
in addition to removing the status section at all, it should look like following
After
apiVersion: v1
items:
- apiVersion: core.k8s.com/v1alpha1
kind: Test
metadata:
name: bar
namespace: foo
spec:
certificateIssuer:
acme:
email: myemail
provider:
credentials: dst
type: foo
domain: vst
type: bar
After link
https://codebeautify.org/yaml-validator/y220531ef
Using yq (https://github.com/mikefarah/yq/) version 4.19.1
Using mikefarah/yq (aka Go yq), you could do something like below. Using del for deleting an entry and with_entries for selecting known fields
yq '.items[] |= (del(.status) | .metadata |= with_entries(select(.key == "name" or .key == "namespace")))' yaml
Starting v4.18.1, the eval flag is the default action, so the e flag can be avoided
This should work using yq in the implementaion of https://github.com/kislyuk/yq (not https://github.com/mikefarah/yq), and the -y (or -Y) flag:
yq -y '.items[] |= (del(.status) | .metadata |= {name, namespace})'
apiVersion: v1
items:
- apiVersion: core.k8s.com/v1alpha1
kind: Test
metadata:
name: bar
namespace: foo
spec:
certificateIssuer:
acme:
email: myemail
provider:
credentials: dst
type: foo
domain: vst
type: bar

How to access value from a Object using a dynamic key in Yaml

I have a yaml configuration as follows:
parameters:
group: '$(group)'
acl:
certificateFile: AclCertificates.p12
provisioningProfileFile: AmericashDisProfile.mobileprovision
keystore: 'acl.jks'
sail:
certificateFile: AclCertificates.p12
provisioningProfileFile: AmericashDisProfile.mobileprovision
keystore: 'acl.jks'
steps:
- bash: |
echo ${{ parameters[$(group)]['certificateFile'] }}
I want to access the object value using the dynamic key. Here group: '$(group)' is a dynamic value which is coming from another var file.
I have tried a way of access the object value like ${{ parameters[$(group)]['certificateFile'] }} But its not working. I'm not able to figure out that how should i pass the parameter group in the echo ${{ parameters[$(group)]['certificateFile'] }} in order to get specific object's value.
For example, you have a YAML pipeline A:
parameters:
- name: test
type: object
default:
- name: Name1
path: Path1
- name: Name2
path: Path2
variables:
sth: ${{ join(';',parameters.test.*.name) }}
And then you can use YAML pipeline B to get the object value:
variables:
- template: azure-pipelines-2.yml # Template reference
steps:
- task: CmdLine#2
inputs:
script: 'echo "${{variables.sth}}"'

YQ 4.7.0 - How to traverse each array elements and update value

I have a yaml:
global:
resolve_timeout: 5m
receivers:
- name: alerts-null
- name: default
local_configs:
- api_url: https://abx.com
channel: '#abx'
send_resolved: true
username: abc-123
- name: devops-alerts
local_configs:
- api_url: https://abx.com
channel: '#abx'
send_resolved: true
username: abc-123
The yaml can have multiple "name:" elements in the array and I want to loop all "name" elements and change the value for key "username:" to "xyz-321". Resultant YAML should be as follows:
global:
resolve_timeout: 5m
receivers:
- name: alerts-null
- name: default
local_configs:
- api_url: https://abx.com
channel: '#abx'
send_resolved: true
username: xyz-321
- name: devops-alerts
local_configs:
- api_url: https://abx.com
channel: '#abx'
send_resolved: true
username: xyz-321
I tried to use following yq command, but it did not changed the desired key's value:
yq eval '(.receivers[] | select(.name.local_configs.username)) = "xyz-321"' source.yaml > manipulated.yaml
Any pointers are appreciated.
If you change the indexing to look like this:
global:
resolve_timeout: 5m
receivers:
- name: alerts-null
internal_config:
username: abc-123
- name: default
internal_config:
username: abc-123
Then the flowing will work:
yq e '(.receivers[].internal_config.username) |= "new_username"' myFile.yaml

Resources