# Question on wait\_for

**URL:** <https://forum.ansible.com/t/question-on-wait-for/17500>\
**Category:** Ansible Project\
**Created:** [February 11, 2015, 11:28am UTC](https://forum.ansible.com/t/question-on-wait-for/17500 "2015-02-11T11:28:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![t.hirsch](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@t.hirsch](https://forum.ansible.com/u/t.hirsch)\
**Post date:** [February 11, 2015, 11:28am UTC](https://forum.ansible.com/t/question-on-wait-for/17500/1 "2015-02-11T11:28:23Z")

</div>

Hi,  
I want to make a simple check after deploying an application with Ansible.  
The task looks like this:

- name: Wait for application to listen  
wait\_for: host=localhost port={{ application\_port }} state=started timeout=30  
tags:
- check

I expect an HTTP server to listen on that port, and t check it myself I would use something like curl.

When I run it from my local machine, it works fine, but when it’s run on the CI server, this last step breaks after the timeout.  
I have also seen this thing wrapped in the “local\_action” module, but since the above example works, I do not think that’s the issue.  
The only difference between CI run and me doing it locally is the remote\_user, but I find it hard to image how that can possibly have anything to do with it., when only running the check.

There are probably many ways to solve the problem “Have Ansible exit with an error, if the deployment fails”, though this one is probably the easiest, so any hint is appreciated.

The error I am getting:

```auto
failed: [162.13.11.115] => {"elapsed": 34, "failed": true}
msg: Timeout when waiting for localhost:8000

```

```auto

```

---

<div class="post-metadata">

**Author:** ![t.hirsch](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@t.hirsch](https://forum.ansible.com/u/t.hirsch)\
**Post date:** [February 11, 2015, 11:41am UTC](https://forum.ansible.com/t/question-on-wait-for/17500/2 "2015-02-11T11:41:58Z")

</div>

This works:

- name: Pausing to wait for application to start (fixed interval)  
pause: seconds=10  
tags:

- check

- name: Run curl  
command: curl localhost:8000  
register: run\_curl  
tags:

- check

- name: Check curl output  
fail: msg=‘curl output was not as expected’

# ‘Not found’ is the expected content

when: “run\_curl.stdout.find(‘Not Found’) == -1”  
tags:

- check

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [February 13, 2015, 10:31am UTC](https://forum.ansible.com/t/question-on-wait-for/17500/3 "2015-02-13T10:31:49Z")

</div>

The wait\_for module accepts a 'delay' parameter, so you don't need the  
pause task.

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

Giovanni

---

<div class="post-metadata">

**Author:** ![t.hirsch](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@t.hirsch](https://forum.ansible.com/u/t.hirsch)\
**Post date:** [February 13, 2015, 2:03pm UTC](https://forum.ansible.com/t/question-on-wait-for/17500/4 "2015-02-13T14:03:49Z")

</div>

Hi Giovanni!

Using wait\_for did not work, that is why I changed to using pause. I would have preferred to use wait\_for.  
My question is: Under what circumstances would “wait\_for: port=8000” fail, when “command: curl localhost:8000” (after waiting for the server) would succeed?

Thank you!

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [February 13, 2015, 2:53pm UTC](https://forum.ansible.com/t/question-on-wait-for/17500/5 "2015-02-13T14:53:37Z")

</div>

Even with the 'delay' parameter, wait\_for still doesn't work? That would  
make wait\_for wait as many seconds as needed for the service to start,  
so you avoid trying to connect uselessly (and possibly having to wait  
for a timeout to expire).

If a pause fixes this for you, it could be a timing issue. The server  
could be listening on the port but not accepting new connections yet, or  
something related, I'm guessing.

Giovanni

---

<div class="post-metadata">

**Author:** ![Jarek\_Smiejczak](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@Jarek\_Smiejczak](https://forum.ansible.com/u/Jarek_Smiejczak)\
**Post date:** [February 13, 2015, 3:13pm UTC](https://forum.ansible.com/t/question-on-wait-for/17500/6 "2015-02-13T15:13:12Z")

</div>

Maybe Thomas should change wait\_for conditions a little and wait until curl gets a page content instead of just checking port?

I’m not sure how to do this in ansible ☹

btw. Hi, I’m new here 😉 Greetings from Poland.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 13, 2015, 3:43pm UTC](https://forum.ansible.com/t/question-on-wait-for/17500/7 "2015-02-13T15:43:42Z")

</div>

The issue can sometimes be caused by an app opens a port well before  
it actually is capable of responding to traffic \*stares at tomcat\*,  
this is usually the case when the main daemon runs as root to get  
privileged resources (low ports, logs) and then changes to a lower  
privileged user to do the work and it then has a slow loading  
application \*stares at jboss\*.

---

<div class="post-metadata">

**Author:** ![t.hirsch](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@t.hirsch](https://forum.ansible.com/u/t.hirsch)\
**Post date:** [February 17, 2015, 10:52am UTC](https://forum.ansible.com/t/question-on-wait-for/17500/8 "2015-02-17T10:52:36Z")

</div>

@Giovanni: Yes the problem also happens, when I use wait\_for with a delay

@Brian: Deploying a NodeJS application, normally it takes a few seconds for it to be ready. Also, everything is running with a dedicated non-root user.
