Ansible - delay parameter

Both the pause module and delay parameter are used to create a pause or delay during task execution.

Let's say you want to retry a particular tasks a number of times, and have a delay between each attempt. In this example, you want to retry the ps command 3 times with a 3 second delay between each attempt. The retries option is often used with the delay option.

- name: ps command
  shell: "ps -ef | grep httpd"
  retries: 3
  delay: 3
  register: out
  until: oit.rc == 0


In this example, the return code of the ps command will be 0 (success) when there is an HTTPD process running on the managed node (e.g. target system). On the other hand, the return code will be 1 (failed) when there are no HTTPD processes running on the managed node. In this example, as long as the return code is not 0, the ps command will be retried 3 times.

Running this play should produce the following.

PLAY [all] 

TASK [ps command] 
FAILED - RETRYING: ps command (3 retries left).
FAILED - RETRYING: ps command (2 retries left).
FAILED - RETRYING: ps command (1 retries left).
fatal: []: FAILED! => {"attempts": 3, "changed": true, "cmd": "ps -ef | grep httpd", "delta": "0:00:00.014413", "end": "2020-07-20 05:23:37.348236", "msg": "non-zero return code", "rc": 1, "start": "2020-07-20 05:23:37.333823", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

PLAY RECAP : ok=1    changed=0    unreachable=0    failed=1


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 8b45c in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |