check connectivity (telnet <ip_address> <port>

91 views
Skip to first unread message

dulh...@mailbox.org

unread,
Jun 4, 2023, 6:07:00 AM6/4/23
to ansible...@googlegroups.com
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?

Dick Visser

unread,
Jun 4, 2023, 7:22:22 AM6/4/23
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1926803861.486837.1685873199295%40office.mailbox.org.
--
Sent from Gmail Mobile

Vladimir Botka

unread,
Jun 4, 2023, 7:38:44 AM6/4/23
to dulhaver via Ansible Project
On Sun, 4 Jun 2023 12:06:39 +0200 (CEST)
dulhaver via Ansible Project <ansible...@googlegroups.com> wrote:

> ... couple of postgres-remotes ...
> ... to check whether postgres1 has a connection via port 5432 to a specific IP address ...
> ... what would be the right approach (aka module) for such in an Ansible TASK?

Use the module *wait_for*. See
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/wait_for_module.html


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

- hosts: postgres1
tasks:
- wait_for:
host: <specific IP address>
port: 5432

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

- hosts: all
tasks:
- wait_for:
host: <specific IP address>
port: 5432
delegate_to: postgres1
run_once: true

See: pg_isready
https://www.postgresql.org/docs/current/app-pg-isready.html

--
Vladimir Botka

dulh...@mailbox.org

unread,
Jun 5, 2023, 5:54:04 AM6/5/23
to ansible...@googlegroups.com
probably I did not make myself clear enough

> - hosts: postgres1
> tasks:
> - wait_for:
> host: <specific IP address>
> 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
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20230604133822.77edfdfe%40gmail.com.

---
gunnar wagner | fichtestr. 1, 19386 lübz | fon: 0176 7808 9090

---
gunnar wagner | fichtestr. 1, 19386 lübz | fon: 0176 7808 9090

Vladimir Botka

unread,
Jun 5, 2023, 7:08:12 AM6/5/23
to dulhaver via Ansible Project
On Mon, 5 Jun 2023 11:53:44 +0200 (CEST)
dulhaver via Ansible Project <ansible...@googlegroups.com> wrote:

> 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

> > - hosts: postgres1
> > tasks:
> > - wait_for:
> > host: <specific IP address>
> > port: 5432
>
> I guess this would check whether 'postgres1' can connect to
> <specific IP address>:5432. What I wanted to check ... whether
> *postgres1* would be ready to accept connections from <specific IP
> address>:5432


--
Vladimir Botka

dulh...@mailbox.org

unread,
Jun 5, 2023, 7:37:27 AM6/5/23
to ansible...@googlegroups.com
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.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20230605130752.2165b095%40gmail.com.
Reply all
Reply to author
Forward
0 new messages