3

I want to break out of the with_items loop based on a condition. That condition for arguments sake is if the stdout of a command is equal to a particular string.

Obviously the example below does not work but this is an idea of what I want to do.

For example:

- name: testing loop
  shell: "echo {{ item }}"
  with_items:
     - "one"
     - "two"
     - "three"
  register: shell_command # registering the shell command and it's attributes
  when: shell_command.stdout == "two" # break once the stdout of the run shell command matches the string "two". So it will run twice and break on the second.
Ali Hamdan
  • 31
  • 1
  • 3
  • Welcome to Stack Overflow! In the future, you may want to specify how your example "does not work". Your question is fine, but we get a lot of questions where the entire description of the issue is "it doesn't work", and that tends to bias reviewers against those words. – Haem Nov 06 '18 at 12:58
  • @Haem Got that! Thanks – Ali Hamdan Nov 06 '18 at 13:54

2 Answers2

1

This seems not possible at the moment as you can see here. There exists an untested hack out there.

JGK
  • 3,710
  • 1
  • 21
  • 26
0

If you want to abort whole playbook, try this:

 - name: testing loop
            shell: "echo {{ item }}"
            with_items:
              - "one"
              - "two"
              - "three"
            register: shell_command
            failed_when: "'two' in shell_command.stdout"

Or you can just add ignore_errors: yes

rafal1337
  • 164
  • 10
  • 1
    I don't want to abort the whole playbook. I just want to break the loop so I can `continue` on with the rest of the playlook – Ali Hamdan Nov 06 '18 at 12:49