Reputation: 139
Hello I have multiple yaml files which I need to updated, and to each file I need to add one or two lines after imagename line. I want use yq, but I am able only to add new value at end of yaml file
My file
containers:
container1:
name: service
org: "company:"
imagename: thirdparty-ingestion-service
tagprefix: "-"
tag: 772x0-69
ports:
- port: 6066
name: debug
- port: 11100
How it should look
containers:
container1:
name: service
org: "company:"
imagename: thirdparty-ingestion-service
imageprefix: ""
tagprefix: "-"
tag: 72x0-69
ports:
- port: 6066
name: debug
- port: 11100
How I added so far
yq .containers.container1.imageprefix="" values.yaml
Upvotes: 0
Views: 1679
Reputation: 36131
While items of an array do have an order, fields within objects do not, so inserting a new field at the desired position may very well not be respected by the next application processing the resulting data. That said, for the time being, collecting the fields (and their values) into an array, modifying their order, then re-assembling the object, will do the job. However, different trickery is needed depending on which implementation of yq you want to use.
Using kislyuk/yq:
.containers.container1 |= with_entries(
., (select(.key == "imagename") | {key: "imageprefix", value: ""})
)
Using mikefarah/yq:
.containers.container1 |= (
to_entries
| (.[] | select(.key == "imagename") | key + 1) as $i
| .[:$i] + [{"key": "imageprefix", "value": ""}] + .[$i:]
| from_entries
)
Upvotes: 1