[ansible-project] Re: Exit with_items loop after first success?

2020-05-21 Thread Harshavardhan Reddy A
Here you go...
https://www.reddit.com/r/ansible/comments/di4o6h/how_to_break_the_loop/



On Thursday, December 11, 2014 at 10:41:55 AM UTC-5, Hugh Saunders wrote:
>
> Hi All, 
> I've been asking on IRC,  twitter 
> , and github 
>  about this and was asked 
> to post to the ML as well so here goes :)
>
> I would like to be able to exit a with_items loop based on the result of 
> the previous iteration. Think of it as a generalisation of 
> with_first_found. 
>
> Two use cases: 
>
>1. I have a list of mirrors known to host file, I want to iterate over 
>that list until I successfully retrieve the file, then stop iterating. No 
>point in downloading the file multiple times.
>2. I have a git ref (branch/tag) and a list of mirrors of that repo (
>git.openstack.org, github.com), I want to resolve that ref to a SHA 
>without cloning the repo. Run git ls-remote via shell passing in each 
>remote url in turn until the ref is successfully resolved. No point in 
>resolving it twice. Note that for this use case I do not want to clone the 
>repo. 
>
> I'm sure there are other situations where it would be useful to break out 
> of a loop. 
>
> As explained in the issue, I can think of two ways of doing this, both 
> would require modifications to ansible: 
>
>1. Add previous_iteration variable for use in when clause, eg when: 
>previous_iteration | failed
>2. Allow registered variable to be used within a loop, eg when: 
>shell_result['results'][-1].rc != 0
>
>
> Thanks for any insight. 
>
> --
> Hugh Saunders
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/a15f0310-08e0-4805-a593-b90115b67a4b%40googlegroups.com.


Re: [ansible-project] Re: Exit with_items loop after first success?

2020-05-20 Thread Brian Coca
It is something i would like to add as part of a full looping revamp
https://github.com/ansible/proposals/issues/140




-- 
--
Brian Coca

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CACVha7cLDSK_q2o19UBW0nc6p%3D9RxMbpk0cbWU4NoOZnaXoOzQ%40mail.gmail.com.


Re: [ansible-project] Re: Exit with_items loop after first success?

2020-05-18 Thread Vladimir Botka
On Sun, 17 May 2020 02:19:26 -0700 (PDT)
Nikolay Dimov  wrote:

> I ended up using the following hack:
> 
> - name: shell test
> shell: "{{ item }}"
> with_items:
> - /bin/true 
> - /bin/false
> - /bin/true 
> when: r_shell.rc|default(0)==0
> register: r_shell
> 
> This exits on first failure.

No. It does not. This does not solve the problem that the loop "just keeps
running until it finishes the loop". The loop will run further after the
failure and the remaining items will be skipped because of the condition.

Then, because of the failure, the playbook will be terminated.

> To exit on success you can use:
> 
> when: r_shell.rc|default(1)!=0

The result of this condition is completely different. The first successful
iteration makes all the rest to be skipped. But, because there was no
failure, the playbook will continue. Right?

HTH,

-vlado

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200518181624.47c8bfe5%40gmail.com.


pgpkliWuAk_ed.pgp
Description: OpenPGP digital signature


Re: [ansible-project] Re: Exit with_items loop after first success?

2020-05-18 Thread Stefan Hornburg (Racke)
On 5/16/20 2:12 PM, Nikolay Dimov wrote:
> This a very important feature to have, and not only on success  but on any 
> condition (e.g. on failure). Currently, it
> just keeps running until it finishes the loop which is not always a desired 
> outcome.
> 

Hello Nikolay,

I never needed this very important feature, so what is your actual use case?

Regards
 Racke

> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to
> ansible-project+unsubscr...@googlegroups.com 
> .
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/eefefd1a-40a7-4fd5-8a0c-9091fd76d0da%40googlegroups.com
> .


-- 
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/b3bb0e8b-19b3-9f38-9f3c-ed107a6a9986%40linuxia.de.


signature.asc
Description: OpenPGP digital signature


[ansible-project] Re: Exit with_items loop after first success?

2020-05-18 Thread Nikolay Dimov
This a very important feature to have, and not only on success  but on any 
condition (e.g. on failure). Currently, it just keeps running until it 
finishes the loop which is not always a desired outcome.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/eefefd1a-40a7-4fd5-8a0c-9091fd76d0da%40googlegroups.com.


[ansible-project] Re: Exit with_items loop after first success?

2020-05-18 Thread Nikolay Dimov
I ended up using the following hack:

- name: shell test
shell: "{{ item }}"
with_items:
- /bin/true 
- /bin/false
- /bin/true 
when: r_shell.rc|default(0)==0
register: r_shell

This exits on first failure. To exit on success you can use:

when: r_shell.rc|default(1)!=0

This is obviously a hack, and it is strange that register looks different 
in the task and after the task ends, but it does the job. 
It would have been great if the register had the final form and the item 
results were appended as tasks were executed so we can check the results 
there with filters.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/1314a3ed-bf48-4a4d-b3e9-ee18daf29ae4%40googlegroups.com.


[ansible-project] Re: Exit with_items loop after first success?

2019-11-20 Thread Davide Scrimieri

This feature would be really helpful. Are there any news on this?

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/3a063d5f-550d-4693-a727-5eddd874abef%40googlegroups.com.


[ansible-project] Re: Exit with_items loop after first success?

2017-03-07 Thread Ivan Ogai
I also need this feature.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/451ee4c3-2a7c-4da6-b2d5-689a7b69a729%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.