# wait\_for module

**URL:** <https://forum.ansible.com/t/wait-for-module/25933>\
**Category:** Ansible Project\
**Created:** [November 6, 2017, 5:52am UTC](https://forum.ansible.com/t/wait-for-module/25933 "2017-11-06T05:52:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [November 6, 2017, 5:52am UTC](https://forum.ansible.com/t/wait-for-module/25933/1 "2017-11-06T05:52:42Z")

</div>

Hi,

After rebooting the host and need to check if that process is back online.  
Is there a way in ansible (or “wait\_for module” ) to check the service or process to be running after a reboot. could you please share your insight about it.Thank you…

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 6, 2017, 3:21pm UTC](https://forum.ansible.com/t/wait-for-module/25933/2 "2017-11-06T15:21:35Z")

</div>

For the reboot you can use

- name: Restart server  
&nbsp;&nbsp;shell: sleep 2 && /sbin/shutdown -r now  
&nbsp;&nbsp;async: 1  
&nbsp;&nbsp;poll: 0

If you can use wait\_for depends on what your process is doing,  
wait\_for can do many things you find it in the documentation[1].

To check if a service is running you have service module that can do that.

[1] [https://docs.ansible.com/ansible/latest/wait\_for\_module.html](https://docs.ansible.com/ansible/latest/wait_for_module.html)

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [November 7, 2017, 5:22pm UTC](https://forum.ansible.com/t/wait-for-module/25933/3 "2017-11-07T17:22:16Z")

</div>

Thanks Kai , looking for something like the server is rebooted and looks for the processes or service and then move to the next server reboots and check the service and goes to the next hosts and does the same.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 7, 2017, 5:50pm UTC](https://forum.ansible.com/t/wait-for-module/25933/4 "2017-11-07T17:50:58Z")

</div>

You can do something like this.

&nbsp;&nbsp;- shell: sleep 2 && /sbin/shutdown -r now  
&nbsp;&nbsp;&nbsp;&nbsp;async: 1  
&nbsp;&nbsp;&nbsp;&nbsp;poll: 0

&nbsp;&nbsp;- wait\_for:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: 22  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: '{{ inventory\_hostname }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search\_regex: OpenSSH  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delay: 10  
&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

&nbsp;&nbsp;- service:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: service-name  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: started  
&nbsp;&nbsp;&nbsp;&nbsp;check\_mode: yes  
&nbsp;&nbsp;&nbsp;&nbsp;register: r

&nbsp;&nbsp;- fail:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Something went wrong  
&nbsp;&nbsp;&nbsp;&nbsp;when: r.state != 'started'

But if your process is listening on a port that you can check, then you can use wait\_for directly.

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [November 8, 2017, 6:20am UTC](https://forum.ansible.com/t/wait-for-module/25933/5 "2017-11-08T06:20:23Z")

</div>

Thanks Kai for that insight , i have taken out the port=22 and include the timeout=600 option in wait\_for module in order to check the services by login to server. Also can we set different timeout values for different host in wait\_for module.Can you please throw some light as i am created a playbook for reboot and calling it in other playbook by specifying specific host. like below but its not working .

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 8, 2017, 6:52am UTC](https://forum.ansible.com/t/wait-for-module/25933/6 "2017-11-08T06:52:04Z")

</div>

> Thanks Kai for that insight , i have taken out the port=22 and include the  
> timeout=600 option in wait\_for module in order to check the services by  
> login to server. Also can we set different timeout values for different  
> host in wait\_for module.

You can have a variable for each host that you specify the timeout.

> Can you please throw some light as i am created a  
> playbook for reboot and calling it in other playbook by specifying specific  
> host. like below but its not working .

There is a lot of errors in your playbook, you should read up on YAML file syntax.  
[http://docs.ansible.com/ansible/latest/YAMLSyntax.html](http://docs.ansible.com/ansible/latest/YAMLSyntax.html)

And then study the other examples you find all over the documentation and remember to pay attention to the indentation since that is very important in YAML.

> ---  
> tasks:  
> &nbsp;&nbsp;&nbsp;shell: sleep 2 && shutdown -r now  
> &nbsp;&nbsp;&nbsp;async: 1  
> &nbsp;&nbsp;&nbsp;poll: 0  
> &nbsp;&nbsp;&nbsp;ignore\_errors: true

This is a task file, it can't have "tasks:" in it.  
Remove the tasks and add a dash in front of the shell line and make sure the indentation is correct.

> server.yml
> 
> ---  
> - name: for the individual hosts  
> - hosts: 192.168.3.154 \<========( This server requires to wait for the server to come online after reboot )

Since you are using the name, you can't have the dash on hosts:

> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;include: tasks/restart1.yml

The include is missing a dash, and need to be indented properly.

> &nbsp;&nbsp;&nbsp;- name: wait for the server to be online  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local\_action: wait\_for host={{ inventory\_hostname }} state=started delay=5 timeout=120
> 
> - hosts: 192.168.3.153 \<============ ( this server doesnot require to wait for the server to come back after reboot )  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;- include: tasks/restart1.yml

Here the indentation is incorrect.

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [November 8, 2017, 9:09am UTC](https://forum.ansible.com/t/wait-for-module/25933/7 "2017-11-08T09:09:59Z")

</div>

Thanks Kai for sorting this out. I have rectified the indentations & other corrections and its working now …will add more logic to get the desired output.Appreciate your prompt help Kai…
