check connectivity (telnet <ip_address> <port>

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?

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

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

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

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

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.