Ansible playbook rebooting the server , wait till reboot complete

Solution 1:

You should use wait_for_connection instead.

tasks:
  - name: Execute the script 
    shell: bash testscript.sh
    args:
      chdir: /home/ubuntu
  - name: wait
    wait_for_connection:
      delay: 10

I'd advise to do this in a task, not a handler. The handler is only executed after all tasks have been finished, so if you have tasks following the task that executes the reboot they will be tried before the playbook even starts to wait.

Alternatively, use the reboot module, which does this automatically.