Executing the mentioned command on CLI will result into an output of
nc -d -q0 -l 1234 &
[1] 123456
~/test$ Ncat: Invalid -d delay "-q0" (must be greater than 0). QUITTING.
^C
[1]+ Exit 2 nc -d -q0 -l 1234
whereby a minimal example playbook of
---
- hosts: test
become: false
gather_facts: false
tasks:
- name: Start LISTENer on Remote Node
shell:
cmd: "nc --listen 1234"
poll: 0
async: 60 # seconds
- name: Test from Control Node
delegate_to: controlnode.example.com
wait_for:
host: "{{ inventory_hostname }}"
port: 1234
state: drained
delay: 0
timeout: 3 # seconds
active_connection_states: SYN_RECV
- name: Test via nc
delegate_to: controlnode.example.com
shell:
cmd: "nc -vz {{ inventory_hostname }} 1234"
register: result
- name: Show result
debug:
var: result
will result into an output of
TASK [Start LISTENer on Remote Node] ************************
changed: [test1.example.com]
changed: [test2.example.com]
changed: [test3.example.com]
TASK [Test from Control Node] *******************************
ok: [test1.example.com -> controlnode.example.com]
ok: [test2.example.com -> controlnode.example.com]
ok: [test3.example.com -> controlnode.example.com]
TASK [Test via nc] ******************************************
changed: [test1.example.com -> controlnode.example.com]
changed: [test2.example.com -> controlnode.example.com]
changed: [test3.example.com -> controlnode.example.com]
TASK [Show result] ******************************************
ok: [test1.example.com] =>
result:
changed: true
cmd: nc -vz test1.example.com 1234
delta: '0:00:00.034145'
end: '2023-01-28 13:30:00.291207'
failed: false
msg: ''
rc: 0
start: '2023-01-28 13:30:00.257062'
stderr: |-
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.2.1:1234.
Ncat: 0 bytes sent, 0 bytes received in 0.02 seconds.
stderr_lines:
- 'Ncat: Version 7.50 ( https://nmap.org/ncat )'
- 'Ncat: Connected to 192.168.2.1:1234.'
- 'Ncat: 0 bytes sent, 0 bytes received in 0.02 seconds.'
stdout: ''
stdout_lines: []
...
Regarding
One of the tasks I'd like to use is to do "TCP ping" ...
it is recommended to use wait_for
module – Waits for a condition before continuing instead of
... to do "TCP ping" using netcat.
as one can see from the provided sample output already and because of the difference in the result sets and behavior.
Further Documentation