To put it in simple way, you can loop a list of values simply attaching to loop in func call
- func: shell
do:
- echo "hello {{.loopitem}}"
loop:
- tom
- peter
- james
- func: shell
do:
- echo "name {{.loopitem.name}}"
- echo "age {{.loopitem.age}}"
loop:
- name: tom
age: 11
- name: peter
age: 45
- name: james
age: 23
Pleas refer to the usage of .countries
goal:
- add loop support
design: |
loop takes either a list directly
or it takes a static var/dvar name to reference to
or a templated string to be evaluated and eventually
pointing to a var/dvar name
consideration: |
do we need the while condition to stop the loop
maybe not
the reason is that you can always use dvar + cmd func
to prepare the list for iteration
vars:
countries:
- name: Australia
population: 20m
- name: British
population: 2000m
- name: China
population: 1.4b
- name: Danmark
population: 30m
tasks:
- name: task
task:
- func: shell
do:
- echo "hello {{.loopitem}}"
loop:
- tom
- peter
- james
- func: shell
do:
- echo "name {{.loopitem.name}}"
- echo "age {{.loopitem.age}}"
loop:
- name: tom
age: 11
- name: peter
age: 45
- name: james
age: 23
- func: shell
do:
- echo "hello {{.loopitem}}"
loop: [Australia, British, China, Danmark]
- func: shell
desc: |
the loop point to a iteratable var countries
do:
- echo "hello {{.loopitem.name}}"
- echo "hello {{.loopitem.population}}"
loop: "countries"
- func: shell
desc: |
the templated value will be eventually a var/dvar name
dvars:
- name: listname
value: "countries"
do:
- echo "hello {{.loopindex}}"
- echo "hello {{.loopindex1}}"
- echo "hello {{.loopitem.name}}"
- echo "hello {{.loopitem.population}}"
loop: "{{.listname}}"
loading [Config]: ./tests/functests/upconfig.yml
Main config:
Version -> 1.0.0
RefDir -> ./tests/functests
WorkDir -> cwd
AbsWorkDir -> /up_project/up
TaskFile -> c0056
Verbose -> vvv
ModuleName -> self
ShellType -> /bin/sh
MaxCallLayers -> 8
Timeout -> 3600000
MaxModuelCallLayers -> 256
EntryTask -> task
ModRepoUsernameRef ->
ModRepoPasswordRef ->
work dir: /up_project/up
-exec task: task
loading [Task]: ./tests/functests/c0056
module: [self], instance id: [dev], exec profile: []
profile - envVars:
(*core.Cache)({
})
Task1: [task ==> task: ]
-Step1:
self: final context exec vars:
(*core.Cache)({
"up_runtime_task_layer_number": 0,
"countries": {
{
"name": "Australia",
"population": "20m"
},
{
"name": "British",
"population": "2000m"
},
{
"population": "1.4b",
"name": "China"
},
{
"population": "30m",
"name": "Danmark"
}
}
})
cmd( 1):
echo "hello {{.loopitem}}"
-
hello tom
-
.. ok
cmd( 1):
echo "hello {{.loopitem}}"
-
hello peter
-
.. ok
cmd( 1):
echo "hello {{.loopitem}}"
-
hello james
-
.. ok
. ok
-Step2:
self: final context exec vars:
(*core.Cache)({
"countries": {
{
"population": "20m",
"name": "Australia"
},
{
"name": "British",
"population": "2000m"
},
{
"population": "1.4b",
"name": "China"
},
{
"population": "30m",
"name": "Danmark"
}
},
"last_result": (*utils.ExecResult)({
Cmd: "echo \"hello james\"",
Code: 0,
Output: "hello james",
ErrMsg: ""
}),
"up_runtime_task_layer_number": 0
})
cmd( 1):
echo "name {{.loopitem.name}}"
-
name tom
-
.. ok
cmd( 2):
echo "age {{.loopitem.age}}"
-
age 11
-
.. ok
cmd( 1):
echo "name {{.loopitem.name}}"
-
name peter
-
.. ok
cmd( 2):
echo "age {{.loopitem.age}}"
-
age 45
-
.. ok
cmd( 1):
echo "name {{.loopitem.name}}"
-
name james
-
.. ok
cmd( 2):
echo "age {{.loopitem.age}}"
-
age 23
-
.. ok
. ok
-Step3:
self: final context exec vars:
(*core.Cache)({
"countries": {
{
"population": "20m",
"name": "Australia"
},
{
"population": "2000m",
"name": "British"
},
{
"population": "1.4b",
"name": "China"
},
{
"population": "30m",
"name": "Danmark"
}
},
"last_result": (*utils.ExecResult)({
Cmd: "echo \"age 23\"",
Code: 0,
Output: "age 23",
ErrMsg: ""
}),
"up_runtime_task_layer_number": 0
})
cmd( 1):
echo "hello {{.loopitem}}"
-
hello Australia
-
.. ok
cmd( 1):
echo "hello {{.loopitem}}"
-
hello British
-
.. ok
cmd( 1):
echo "hello {{.loopitem}}"
-
hello China
-
.. ok
cmd( 1):
echo "hello {{.loopitem}}"
-
hello Danmark
-
.. ok
. ok
-Step4: [
the loop point to a iteratable var countries
]
self: final context exec vars:
(*core.Cache)({
"countries": {
{
"name": "Australia",
"population": "20m"
},
{
"name": "British",
"population": "2000m"
},
{
"name": "China",
"population": "1.4b"
},
{
"population": "30m",
"name": "Danmark"
}
},
"last_result": (*utils.ExecResult)({
Cmd: "echo \"hello Danmark\"",
Code: 0,
Output: "hello Danmark",
ErrMsg: ""
}),
"up_runtime_task_layer_number": 0
})
cmd( 1):
echo "hello {{.loopitem.name}}"
-
hello Australia
-
.. ok
cmd( 2):
echo "hello {{.loopitem.population}}"
-
hello 20m
-
.. ok
cmd( 1):
echo "hello {{.loopitem.name}}"
-
hello British
-
.. ok
cmd( 2):
echo "hello {{.loopitem.population}}"
-
hello 2000m
-
.. ok
cmd( 1):
echo "hello {{.loopitem.name}}"
-
hello China
-
.. ok
cmd( 2):
echo "hello {{.loopitem.population}}"
-
hello 1.4b
-
.. ok
cmd( 1):
echo "hello {{.loopitem.name}}"
-
hello Danmark
-
.. ok
cmd( 2):
echo "hello {{.loopitem.population}}"
-
hello 30m
-
.. ok
. ok
-Step5: [
the templated value will be eventually a var/dvar name
]
self: final context exec vars:
(*core.Cache)({
"up_runtime_task_layer_number": 0,
"listname": "countries",
"countries": {
{
"name": "Australia",
"population": "20m"
},
{
"name": "British",
"population": "2000m"
},
{
"population": "1.4b",
"name": "China"
},
{
"name": "Danmark",
"population": "30m"
}
},
"last_result": (*utils.ExecResult)({
Cmd: "echo \"hello 30m\"",
Code: 0,
Output: "hello 30m",
ErrMsg: ""
})
})
cmd( 1):
echo "hello {{.loopindex}}"
-
hello 0
-
.. ok
cmd( 2):
echo "hello {{.loopindex1}}"
-
hello 1
-
.. ok
cmd( 3):
echo "hello {{.loopitem.name}}"
-
hello Australia
-
.. ok
cmd( 4):
echo "hello {{.loopitem.population}}"
-
hello 20m
-
.. ok
cmd( 1):
echo "hello {{.loopindex}}"
-
hello 1
-
.. ok
cmd( 2):
echo "hello {{.loopindex1}}"
-
hello 2
-
.. ok
cmd( 3):
echo "hello {{.loopitem.name}}"
-
hello British
-
.. ok
cmd( 4):
echo "hello {{.loopitem.population}}"
-
hello 2000m
-
.. ok
cmd( 1):
echo "hello {{.loopindex}}"
-
hello 2
-
.. ok
cmd( 2):
echo "hello {{.loopindex1}}"
-
hello 3
-
.. ok
cmd( 3):
echo "hello {{.loopitem.name}}"
-
hello China
-
.. ok
cmd( 4):
echo "hello {{.loopitem.population}}"
-
hello 1.4b
-
.. ok
cmd( 1):
echo "hello {{.loopindex}}"
-
hello 3
-
.. ok
cmd( 2):
echo "hello {{.loopindex1}}"
-
hello 4
-
.. ok
cmd( 3):
echo "hello {{.loopitem.name}}"
-
hello Danmark
-
.. ok
cmd( 4):
echo "hello {{.loopitem.population}}"
-
hello 30m
-
.. ok
. ok