ansible.builtin.listen_ports_facts (v2.9.27) — module

Gather facts on processes listening on TCP and UDP ports.

| "added in version" 2.9 of ansible.builtin"

Authors: Nathan Davison (@ndavison)

preview | supported by community

Install Ansible via pip

Install with pip install ansible==2.9.27

Description

Gather facts on processes listening on TCP and UDP ports.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Gather facts on listening ports
  listen_ports_facts:
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: TCP whitelist violation
  debug:
    msg: TCP port {{ item.port }} by pid {{ item.pid }} violates the whitelist
  vars:
    tcp_listen_violations: "{{ ansible_facts.tcp_listen | selectattr('port', 'in', tcp_whitelist) | list }}"
    tcp_whitelist:
      - 22
      - 25
  loop: "{{ tcp_listen_violations }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: List TCP ports
  debug:
    msg: "{{ ansible_facts.tcp_listen  | map(attribute='port') | sort | list }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: List UDP ports
  debug:
    msg: "{{ ansible_facts.udp_listen | map(attribute='port') | sort | list }}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: List all ports
  debug:
    msg: "{{ (ansible_facts.tcp_listen + ansible_facts.udp_listen) | map(attribute='port') | unique | sort | list }}"

Outputs

ansible_facts:
  contains:
    tcp_listen:
      contains:
        address:
          description: The address the server is listening on.
          returned: always
          sample: 0.0.0.0
          type: str
        name:
          description: The name of the listening process.
          returned: if user permissions allow
          sample: mysqld
          type: str
        pid:
          description: The pid of the listening process.
          returned: always
          sample: 1223
          type: int
        port:
          description: The port the server is listening on.
          returned: always
          sample: 3306
          type: int
        protocol:
          description: The network protocol of the server.
          returned: always
          sample: tcp
          type: str
        stime:
          description: The start time of the listening process.
          returned: always
          sample: Thu Feb  2 13:29:45 2017
          type: str
        user:
          description: The user who is running the listening process.
          returned: always
          sample: mysql
          type: str
      description: A list of processes that are listening on a TCP port.
      returned: if TCP servers were found
      type: list
    udp_listen:
      contains:
        address:
          description: The address the server is listening on.
          returned: always
          sample: 0.0.0.0
          type: str
        name:
          description: The name of the listening process.
          returned: if user permissions allow
          sample: rsyslogd
          type: str
        pid:
          description: The pid of the listening process.
          returned: always
          sample: 609
          type: int
        port:
          description: The port the server is listening on.
          returned: always
          sample: 514
          type: int
        protocol:
          description: The network protocol of the server.
          returned: always
          sample: udp
          type: str
        stime:
          description: The start time of the listening process.
          returned: always
          sample: Thu Feb  2 13:29:45 2017
          type: str
        user:
          description: The user who is running the listening process.
          returned: always
          sample: root
          type: str
      description: A list of processes that are listening on a UDP port.
      returned: if UDP servers were found
      type: list
  description: Dictionary containing details of TCP and UDP ports with listening servers
  returned: always
  type: complex