carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.68k stars 137 forks source link

How to iterate over values in array's #790

Closed EmaLinuxawy closed 1 year ago

EmaLinuxawy commented 1 year ago

Discussed in https://github.com/vmware-tanzu/carvel-ytt/discussions/789

Originally posted by **EmaLinuxawy** January 17, 2023 Hello Community I am using YTT to generate multiple pipeline templates, so i want to iterate over data values in an array to get the values based on it's name to the respective job. i would like to iterate over this job to get an output with 3 build jobs with the name of the job based on each one inside stages array, then iterate over the repo from it's array and just get the value name without the key, and the same thing with the params ``` #@ def build(name, repo, params): name: #@ name plan: - in_parallel: - get: #@ repo trigger: true - task: build file: path/to/file.yaml params: #@ params #@ end jobs: #@ for/end stage in [s for s in data.values.build.stages]: - #@ build() ``` and this is my data/values: ``` build: stages: ["dev", "stage", "prod"] repo: ["repo-dev", "repo-stage", "repo-prod"] params: ["dev", "stage", "prod"] ``` lastly, I have data-values-file with the following: ``` repo-dev: dev-repo-app repo-stage: dev-stage-app repo-prod dev-prod-app dev: key: value key2: value2 stage: key: value key2: value2 prod: key: value key2: value2 ```
vrabbi commented 1 year ago

one option can be found in this gist https://carvel.dev/ytt/#gist:https://gist.github.com/vrabbi/642414d8ea5a13c7f4cb070bc5d025c1

EmaLinuxawy commented 1 year ago

That was actually what i'm looking for, Thanks a lot @vrabbi