Playbook: tcp - Helmigreg/ant GitHub Wiki

Technical Documentation: TCP Port Connectivity Playbook


Overview

This Ansible playbook performs automated TCP connectivity tests using netcat (nc) from hosts defined in the tcp inventory group. It checks the reachability of specified IP addresses and TCP ports, making it suitable for verifying service availability or firewall rules in a test environment.

The playbook uses Ansible's command module to execute nc -zv commands and captures their results.


Structure

Hosts

  • tcp: The playbook targets all machines listed under the tcp group in the Ansible inventory file.

Playbook Tasks

1. Run netcat TCP test on {{ inventory_hostname }}

  • Module: ansible.builtin.command
  • Command: nc -zv -w5 {{ item[0] }} {{ item[1] }}
    • -z: Zero-I/O mode (used for scanning)
    • -v: Verbose output
    • -w5: Timeout of 5 seconds
  • Loop: The task loops over the Cartesian product of the destination and dport variables, forming (IP, port) pairs.
  • Register: Results of the command are stored in nc_tcp_result.
  • Ignore Errors: Enabled, allowing the play to continue even if a port is closed or unreachable.
  • SSH Options: Disables strict host key checking via ansible_ssh_common_args.

2. Print result

  • Module: debug
  • Variable: Displays the nc_tcp_result.results object, which contains the outcome of each netcat test.

Dependencies

  • Ansible: Required to run the playbook.
  • Netcat (nc): Must be installed on the remote hosts.
  • Inventory: A valid inventory file with a group named tcp.
  • Open TCP Ports: Firewall rules or services must allow TCP connections to the specified ports.

Example Inventory Snippet

tcp:
  hosts:
    test-vm-1:
      ansible_host: 192.168.1.20
      ansible_user: testuser
      ansible_password: testpass