# ============================================================
# Do While loops cannot be used on task includes.
# See: https://github.com/ansible/ansible/issues/17098
#
# So as a workaround we use a regular loop to repeatedly attempt
# obtaining a lock.
#
# For this to work we need to skip the subsequent iterations
# once we get a lock, and delay between iterations if we
# did not obtain the lock.
#
# This file encapsulates this logic to reduce the spam from
# skipped tasks in the ansible log.
# ============================================================

- include_tasks: obtain-lock.yaml
  # Skip obtaining a lock if we've already succeeded in getting it
  when: "not won_lock|default(False)|bool"

- name: Lock Retry Delay
  wait_for:
    # Add some random jitter to the delay to reduce lock contention
    timeout: "{{ obtain_lock_delay_seconds + 15|random }}"
  # Only delay if we're retrying, so skip the delay if we're
  # on the last attempt or have got the lock
  when: "obtain_lock_attempt < (max_obtain_lock_attempts - 1) and not won_lock|bool"
