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
Solved! Go to Solution.
Nominating a forum post submits a request to create a new Knowledge Article based on the forum post topic. Please ensure your nomination includes a solution within the reply.
Hello Rafael,
I've set a variable called joke_count to 3
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}}
So we insert that Jinja as a for each loop on the playbook step that calls the joke api
Running the playbook with that loop shows 3 different jokes came back
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')}}
Displaying the jokes is pretty easy to do now with a manual input step
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 :)
Hello Rafael,
I've set a variable called joke_count to 3
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}}
So we insert that Jinja as a for each loop on the playbook step that calls the joke api
Running the playbook with that loop shows 3 different jokes came back
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')}}
Displaying the jokes is pretty easy to do now with a manual input step
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 :)
The Fortinet Security Fabric brings together the concepts of convergence and consolidation to provide comprehensive cybersecurity protection for all users, devices, and applications and across all network edges.
Copyright 2024 Fortinet, Inc. All Rights Reserved.