Nested Loop with exit condition on inner loop

Describe the problem/error/question

I have a workflow where i want to go through a list of servers and run a set of commands via SSH.
If one of the commands fails i want to stop running commands for this host and move on to the next host.
I could work out how to nest two loops and have a workflow iterate through hosts and command but i can’t figure out how for a give host (first loop) stop iterating through commands (second list) if one has failed.

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.16
  • Database (default: SQLite): SQlite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): own
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Debian

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @pelicangris, welcome to the community!

Personally I’d suggest avoiding nested loops at all costs. Such workflows are hard to read and manage, especially when re-visiting them at a later stage.

Instead, consider moving the inner into a sub workflow which can be called through the Execute Workflow node. If your sub-workflow execution fails, n8n will also stop the parent execution.

For example:

Parent workflow:

Sub-workflow:

Now when the sub-workflow hits an error, this error will also stop the parent:

Perhaps this approach works for you?

Thanks for the pointer! Just implemented this and it works for me!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.