
Let's say the following is being returned when attempting a GET/POST/PUT/PATCH request to a remote system using the uri or get_url module.
urlopen error timed out
By default, the connection will timeout after 30 seconds. The timeout paramater can be used to set some other timeout period, such as 60 seconds.
---
- hosts: localhost
tasks:
- name: submit GET request
uri:
url: http://example.com/api/v1
method: GET
timeout: 60
register: out
- debug:
var: out
...
By default, the uri or get_url modules will be executed on the target server. If the target server is unable to connect to the URL, you may want to try using delegate_to: localhost so that the connection is made from the Ansible control node to the URL.
---
- hosts: localhost
tasks:
- name: submit GET request
uri:
url: http://example.com/api/v1
method: GET
delegate_to: localhost
register: out
- debug:
var: out
...
Ansible is kind of interesting in that an SSH connection is made from the Ansible control node (that's your Ansible server) to the managed node (e.g. target server), and then a Python command is issued to read or download a file from the remote system to the managed node.
Before spending all sorts of time trying to determine if there is some issue on the Ansible side, it make much more sense to first determine if the managed node is able to read or download files from a remote system. If the managed node is a Linux system, the curl command can be used to see if you are able to read a file on a remote system.
[john.doe@server1 ~]# curl --url http://www.example.com/foo.txt
curl: (7) Failed connect to www.example.com:80; Connection timed out
Similarly, the wget command can used to see if you are able to download a file from a remote system.
[john.doe@server1 ~]# wget https://www.example.com/foo.txt
--2021-08-12 06:16:15-- https://www.example.com/foo.txt
Resolving www.example.com (www.example.com)... 93.184.216.34
Connecting to www.example.com (www.example.com)|93.184.216.34|:80... failed: Connection timed out.
Notice in these examples that "connection timed out" is returned, which suggests some issue on server1 in this example.
Did you find this article helpful?
If so, consider buying me a coffee over at