Score:0

How to start netcat listening server in Ansible?

us flag

I'm working on larger Ansible playbook. One of the tasks I'd like to use is to do "TCP ping" using netcat. On one of the hosts, I'd like to start a netcat 'server', and then start the 'client' to send any string to the server.

I'm testing this without Ansible -- everything works OK, but in Ansible netcat returns 1. I know for a fact that it means that network connection was not established. Here's my netcat server task:

- shell: |
    nc -d -q0 -l 1234
  poll: 0
  async: 60

However, this just doesn't work. I've tried adding ampersand at the end of command but without success. Another thing I did was to set poll to > 0 and then check on 'server' host whether the port has been opened (via netstat -tulpn). It was not.

What am I missing here ?

Score:1
ca flag

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

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.