# uri module and curl

**URL:** <https://forum.ansible.com/t/uri-module-and-curl/23839>\
**Category:** Ansible Project\
**Created:** [February 28, 2017, 9:42am UTC](https://forum.ansible.com/t/uri-module-and-curl/23839 "2017-02-28T09:42:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Norris](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@John\_Norris](https://forum.ansible.com/u/John_Norris)\
**Post date:** [February 28, 2017, 9:42am UTC](https://forum.ansible.com/t/uri-module-and-curl/23839/1 "2017-02-28T09:42:10Z")

</div>

Hi,  
is the uri module a curl substitute?  
I ask because if, on the command line, I run curl, I get the expected json output from a tomcat rest application. In the logs is a 200 result.  
But if I run the same in uri then …

- name: test with curl type command  
uri:  
url: [http://centos7-ansible:8080/heroes/heroes](http://centos7-ansible:8080/heroes/heroes)

TASK [test with curl type command] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“changed”: false, “content”: “”, “failed”: true, “msg”: “Status code was not [200]: Connection failure: timed out”, “redirected”: false, “status”: -1, “url”: “[http://centos7-ansible:8080/heroes/heroes](http://centos7-ansible:8080/heroes/heroes)”}

What am I doing wrong?

Regards,

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 28, 2017, 11:06am UTC](https://forum.ansible.com/t/uri-module-and-curl/23839/2 "2017-02-28T11:06:39Z")

</div>

Could it be that the host blocks outgoing HTTP traffic, and that you  
have some proxy environment variable set in your interactive terminal  
to reach HTTP content?

If that is the case, you should make Ansible aware of that:  
[http://docs.ansible.com/ansible/playbooks\_environment.html](http://docs.ansible.com/ansible/playbooks_environment.html)

Dick

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [February 28, 2017, 11:45am UTC](https://forum.ansible.com/t/uri-module-and-curl/23839/3 "2017-02-28T11:45:13Z")

</div>

It'd be worth checking if a command: task using curl works.  
If not it's probably a shell environment (proxy?) issue.

I've seen curl work where get\_url: / uri: doesn't if you are hitting an SNI  
endpoint, my workaround is a command: task. Issue here:

[https://github.com/ansible/ansible-modules-core/issues/1716](https://github.com/ansible/ansible-modules-core/issues/1716)

---

<div class="post-metadata">

**Author:** ![John\_Norris](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@John\_Norris](https://forum.ansible.com/u/John_Norris)\
**Post date:** [February 28, 2017, 1:38pm UTC](https://forum.ansible.com/t/uri-module-and-curl/23839/4 "2017-02-28T13:38:48Z")

</div>

many thanks to both of you for the replies.  
I tried both shell and command modules with the curl command and a  
curl: (7) Failed connect to centos7-ansible:8080; Connection refused"  
Not sure why the connection would be refused as there. The previous task in ansible is to start tomcat so would it be too soon to accept requests? Tried adding a pause and that worked with a command module but not the uri module. But curl does take a long time to run whereas outside of anisble it is instant.  
I did increase the timeout value on uri and that did allow it to complete OK.

This is all running on the same VM so there should not be proxy issues.

---

<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:** [February 28, 2017, 1:56pm UTC](https://forum.ansible.com/t/uri-module-and-curl/23839/5 "2017-02-28T13:56:29Z")

</div>

Instead of pause you could use until: so it retries until you get 200.  
[https://docs.ansible.com/ansible/playbooks\_loops.html#do-until-loops](https://docs.ansible.com/ansible/playbooks_loops.html#do-until-loops)

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [March 1, 2017, 10:29am UTC](https://forum.ansible.com/t/uri-module-and-curl/23839/6 "2017-03-01T10:29:27Z")

</div>

> Instead of pause you could use until: so it retries until you get 200.  
> [https://docs.ansible.com/ansible/playbooks\_loops.html#do-until-loops](https://docs.ansible.com/ansible/playbooks_loops.html#do-until-loops)

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

Cheers,  
Paul

---

<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:** [March 1, 2017, 1:05pm UTC](https://forum.ansible.com/t/uri-module-and-curl/23839/7 "2017-03-01T13:05:45Z")

</div>

Can you use wait\_for to check if a web server application return status code 200?  
If so how? It's not clear to me after reading the documentation.

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [March 1, 2017, 1:16pm UTC](https://forum.ansible.com/t/uri-module-and-curl/23839/8 "2017-03-01T13:16:34Z")

</div>

> Can you use wait\_for to check if a web server application return status  
> code 200?  
> If so how? It’s not clear to me after reading the documentation.

I haven’t tried, but I thought search\_regex: ‘HTTP/1.1 200 OK’ or something similar

Cheers,  
Paul

---

<div class="post-metadata">

**Author:** ![dch](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dch/32/7132_2.png) [@dch](https://forum.ansible.com/u/dch)\
**Post date:** [March 2, 2017, 7:22pm UTC](https://forum.ansible.com/t/uri-module-and-curl/23839/9 "2017-03-02T19:22:36Z")

</div>

I would check to see if this is resolving to an IPv6 address at one end  
and listening on IPv4 only at the other, or some variation on that.

Try doing that with `curl -v4sk ...` (force ipv4, verbose output, silent  
response, ignore SSL certificate chain) and see whether that provides  
more info. Sharing the output here may get you the info you need.

Either way, the next logical step here is to run a packet capture (ngrep  
or tcpdump for example) which will probably make it very obvious what is  
failing.

A+  
Dave

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [March 2, 2017, 8:48pm UTC](https://forum.ansible.com/t/uri-module-and-curl/23839/10 "2017-03-02T20:48:19Z")

</div>

I was thinking of a wait\_for: task \*before\* the uri: check, just to give tomcat  
enough time to start a http connector  
(I don't think wait\_for on its own would work, as there's no option  
to send a GET to the port).

The more I think about it, the more I prefer your idea; I've seen plenty of WARs  
that take ages to fire up after the port is open.
