'Ansible retry get attempt number

I am using a task that connects via ssh to a device. Latency is not always constant and sometimes when the prompt is not displayed in time the task fails.

Assuming it is possible to control the timeout value for this task is it possible to dynamically increase this timeout proportionally to the number of the attempt performed?

Something like this

- name: task_name
  connection : local
  task_module:
    args...
    timeout : 10 * "{{ attempt_number }}"
  retries: 3
  delay: 2
  register: result
  until: result | success


Solution 1:[1]

I don't think its possible to get the current attempt number while running the task, it's quite unclear why you're trying to achieve such thing.

Can you elaborate a little bit more?

Solution 2:[2]

Yes, it's possible, here are docs.

When you run a task with until and register the result as a variable, the registered variable will include a key called “attempts”, which records the number of the retries for the task.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 levich
Solution 2 tkrishtop