# check connectivity (telnet \<ip\_address\> \<port\>

**URL:** <https://forum.ansible.com/t/check-connectivity-telnet-ip-address-port/38054>\
**Category:** Ansible Project\
**Created:** [June 4, 2023, 10:06am UTC](https://forum.ansible.com/t/check-connectivity-telnet-ip-address-port/38054 "2023-06-04T10:06:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [June 4, 2023, 10:06am UTC](https://forum.ansible.com/t/check-connectivity-telnet-ip-address-port/38054/1 "2023-06-04T10:06:39Z")

</div>

hi,

I am running a playbook og a couple of postgres-remotes who play different roles in a complex server landscape.

Now I want to check to check whether i.e. postgres1 has a connection via port 5432 to a specific IP address. The true/false like result of such a check should be saved into a variable.

Manually I could i.e. do this with something like ‘telnet \<ip\_address\> 5432’

what would be the right approach (aka module) for such in an Ansible TASK?

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [June 4, 2023, 11:21am UTC](https://forum.ansible.com/t/check-connectivity-telnet-ip-address-port/38054/2 "2023-06-04T11:21:54Z")

</div>

Do you want to check if the server has an established connection to an IP address?  
Or if it is listening on a specific port?  
There are perhaps better ways of finding out if a server is running at some socket

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [June 4, 2023, 11:38am UTC](https://forum.ansible.com/t/check-connectivity-telnet-ip-address-port/38054/3 "2023-06-04T11:38:22Z")

</div>

Use the module \*wait\_for\*. See  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/wait\_for\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/wait_for_module.html)

If you run the play on \*postgres1\* the task is simple

&nbsp;&nbsp;- hosts: postgres1  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- wait\_for:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: \<specific IP address\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: 5432

If you run the play on multiple hosts you might want to run  
once and delegate the task

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- wait\_for:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: \<specific IP address\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: 5432  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: postgres1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

See: pg\_isready  
[https://www.postgresql.org/docs/current/app-pg-isready.html](https://www.postgresql.org/docs/current/app-pg-isready.html)

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [June 5, 2023, 9:53am UTC](https://forum.ansible.com/t/check-connectivity-telnet-ip-address-port/38054/4 "2023-06-05T09:53:44Z")

</div>

probably I did not make myself clear enough

> &nbsp;&nbsp;- hosts: postgres1  
> &nbsp;&nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- wait\_for:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: \<specific IP address\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: 5432

I guess this would check whether 'postgres1' can connect to \<specific IP address\>:5432.

What I wanted to check (without waiting for a connection to be ready actually, just gathering information) was the other way around ...

... whether 'postgres1' would be ready to accept connections from \<specific IP address\>:5432

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [June 5, 2023, 11:07am UTC](https://forum.ansible.com/t/check-connectivity-telnet-ip-address-port/38054/5 "2023-06-05T11:07:52Z")

</div>

> probably I did not make myself clear enough

No. Your statement can't be clearer: "to check whether postgres1 has  
a connection via port 5432 to a specific IP address"  
[https://groups.google.com/g/ansible-project/c/dkR80nKr25Q/m/rVRrMlTbBAAJ](https://groups.google.com/g/ansible-project/c/dkR80nKr25Q/m/rVRrMlTbBAAJ)

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [June 5, 2023, 11:37am UTC](https://forum.ansible.com/t/check-connectivity-telnet-ip-address-port/38054/6 "2023-06-05T11:37:09Z")

</div>

right. It was clear, but not correct I am afraid. Apologies.

I meant it the other way around. Checking whether postgres1 can be connected to FROM another machine.

Still the hint to the wait module was real value for me, thanks for that.
