FortiSOAR Discussions
rrighi
Staff
Staff

Help for creating Basic loops and iterations in FortiSOAR Playbooks

Hi team,

 

I am developing some playbooks, which then will be useful when showing the solution to potential customers.

 

Firstlly I´ve created a playbook that calls https://icanhazdadjoke.com/api (RestFull API) and then shows the result joke to the customer. 

 

Now, I am trying to modify the playbook to call the above API call X times (the variable X is configured inside the playbook). For each execution the API call returns a string (the joke). So, my purpose in this second test is to present to the cusromer X jokes together, like the following:

"joke 1"+"\n"+joke 2"+"\n"+....+joke X"

 

The two playbooks are similar. The first one calls the API call just once. 

The second playbook call the API many times (variable X) and return the outputs together (append or join could be used).

 

Could you help me on how to create loops (like the "for" or while commands in C), execute a code inside the loop and then show the results? Is there any example playbook you could share that does something similarly?

 

Thanks in advance for you attention on this matter.

 

Best Regards,

Rafael Righi

Consulting Systems Engineer
1 Solution
dspille
Staff
Staff

Hello Rafael,

 

I've set a variable called joke_count to 3

joke_count.png

To run a step on a loop, we need to pass the step a list to iterate over. So in this case I'm going to convert the joke_variable to a list that has the length of the integer. This is done with the jinja

{{range(vars.joke_count) | list}}

jinja_eval.png

 

 

So we insert that Jinja as a for each loop on the playbook step that calls the joke api 

 

joke_for_loop.png

 

Running the playbook with that loop shows 3 different jokes came back

 

joke_result.png

 

You can then use a jinja filter called json_query to grab just the jokes from all the responses in another set variable step. The correct jinja to do this is {{vars.steps.Get_joke | json_query('[].data.joke')}}

joke_extract.png

 

Displaying the jokes is pretty easy to do now with a manual input step 

 

manual_input_config.png

display_jokes.png


I've attached this playbook as an example, but I now challenge you to make the joke_count variable be dynamically set from another Manual Input, so that the end user can determine how many jokes they want :) 

Dylan Spille

View solution in original post

1 REPLY 1
dspille
Staff
Staff

Hello Rafael,

 

I've set a variable called joke_count to 3

joke_count.png

To run a step on a loop, we need to pass the step a list to iterate over. So in this case I'm going to convert the joke_variable to a list that has the length of the integer. This is done with the jinja

{{range(vars.joke_count) | list}}

jinja_eval.png

 

 

So we insert that Jinja as a for each loop on the playbook step that calls the joke api 

 

joke_for_loop.png

 

Running the playbook with that loop shows 3 different jokes came back

 

joke_result.png

 

You can then use a jinja filter called json_query to grab just the jokes from all the responses in another set variable step. The correct jinja to do this is {{vars.steps.Get_joke | json_query('[].data.joke')}}

joke_extract.png

 

Displaying the jokes is pretty easy to do now with a manual input step 

 

manual_input_config.png

display_jokes.png


I've attached this playbook as an example, but I now challenge you to make the joke_count variable be dynamically set from another Manual Input, so that the end user can determine how many jokes they want :) 

Dylan Spille