'until' loop label invisible, and not possible to customise?

74 views
Skip to first unread message

Dick Visser

unread,
Jul 7, 2023, 5:54:37 AM7/7/23
to ansible...@googlegroups.com
Hii,
I'm using the 'until' loop to wait for a condition to be true:

---
- name: Do something until it doesn't fail
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Wait for synced directory
community.docker.docker_container_exec:
container: ldap_consumer
command: install/check_sync_status.sh
register: status
until:
- status.failed != true
retries: 3600
delay: 6

- debug: var=status.stdout


This works:

dick.visser@GA0267 ~$ ansible-playbook work/tasks/until1.yml

PLAY [Do something until it doesn't fail]
**************************************************************

TASK [Wait for synced directory]
***********************************************************************
FAILED - RETRYING: [localhost]: Wait for synced directory (3600 retries left).
FAILED - RETRYING: [localhost]: Wait for synced directory (3599 retries left).
FAILED - RETRYING: [localhost]: Wait for synced directory (3598 retries left).
changed: [localhost]

TASK [debug] *******************************************************************************************
ok: [localhost] =>
status.stdout: 'OK: Both local and remote have the same contextCSN:
20230707093240.412420Z#000000#000#000000'

PLAY RECAP *********************************************************************************************
localhost : ok=2 changed=1 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0


One problem is that the FAILED retry attempts are readable here in
plain text, but are otherwise pretty much invisible in my terminal.
This can be confusing when there have been so many attempts that the
terminal appears to be blank.

Another issue is that I can't seem to get a custom label on this loop.
The usual way:

loop_control:
label: status.stdout
extended: true

does not work...

Is there a way to do this?
thx!

Dick

Vladimir Botka

unread,
Jul 7, 2023, 6:49:20 AM7/7/23
to Dick Visser, ansible...@googlegroups.com
On Fri, 7 Jul 2023 11:54:02 +0200
Dick Visser <dnmv...@gmail.com> wrote:

> loop_control:
> label: status.stdout
> extended: true
>
> does not work...
>
> Is there a way to do this?

It is not, I'm afraid. The controller will see the value of *status*
after the loop completes. (The condition is evaluated on the remote
host).

--
Vladimir Botka

Brian Coca

unread,
Jul 7, 2023, 12:32:38 PM7/7/23
to ansible...@googlegroups.com
> (The condition is evaluated on the remote
host).

No, conditions (as well as any templating) is always evaluated on the
controller, the caveat is that DATA for this evaluation might come
from the remote host. via `register:`.

--
----------
Brian Coca

Vladimir Botka

unread,
Jul 7, 2023, 5:21:36 PM7/7/23
to ansible...@googlegroups.com
On Fri, 7 Jul 2023 12:30:08 -0400
Brian Coca <bc...@redhat.com> wrote:

> > (The condition is evaluated on the remote host).

> No, conditions (as well as any templating) is always evaluated on the
> controller, the caveat is that DATA for this evaluation might come
> from the remote host. via `register:`.

Right. The *task_executor* takes care of this
https://github.com/ansible/ansible/blob/devel/lib/ansible/executor/task_executor.py#L775

I was too quick. The registered variable can be used in the label. If
the status of the task (changed) is available when the label is
displayed the registered variable should be available as well. For
example,


- command: "echo {{ item }}"
register: status
loop: [1, 2, 3, 4, 5]
loop_control:
label: "{{ status.stdout|int + 10 }}"
until: status.stdout|int < 4
delay: 1

gives

TASK [command]
***************************************************************
changed: [test_11] => (item=11)
changed: [test_11] => (item=12)
changed: [test_11] => (item=13)
FAILED - RETRYING: [test_11]: command (3 retries left).
FAILED - RETRYING: [test_11]: command (2 retries left).
FAILED - RETRYING: [test_11]: command (1 retries left).
failed: [test_11] (item=14) => changed=true
ansible_loop_var: item
attempts: 3
cmd:
- echo
- '4'
delta: '0:00:00.015013'
end: '2023-07-07 21:06:24.412645'
item: 4
msg: ''
rc: 0
start: '2023-07-07 21:06:24.397632'
stderr: ''
stderr_lines: <omitted>
stdout: '4'
stdout_lines: <omitted>
FAILED - RETRYING: [test_11]: command (3 retries left).
FAILED - RETRYING: [test_11]: command (2 retries left).
FAILED - RETRYING: [test_11]: command (1 retries left).
failed: [test_11] (item=15) => changed=true
ansible_loop_var: item
attempts: 3
cmd:
- echo
- '5'
delta: '0:00:00.015014'
end: '2023-07-07 21:06:33.054414'
item: 5
msg: ''
rc: 0
start: '2023-07-07 21:06:33.039400'
stderr: ''
stderr_lines: <omitted>
stdout: '5'
stdout_lines: <omitted>


--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages