- AnsibleFest
- Products
- Community
- Webinars & Training
- Blog

Documentation

# listen_ports_facts – Gather facts on processes listening on TCP and UDP ports

New in version 2.9.

- Synopsis

- Examples

- Returned Facts

- Status

## Synopsis

- Gather facts on processes listening on TCP and UDP ports.

## Examples

    - name: Gather facts on listening ports
      listen_ports_facts:

    - 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 }}"

    - name: List TCP ports
      debug:
        msg: "{{ ansible_facts.tcp_listen  | map(attribute='port') | sort | list }}"

    - name: List UDP ports
      debug:
        msg: "{{ ansible_facts.udp_listen | map(attribute='port') | sort | list }}"

    - name: List all ports
      debug:
        msg: "{{ (ansible_facts.tcp_listen + ansible_facts.udp_listen) | map(attribute='port') | unique | sort | list }}"

## Returned Facts

Facts returned by this module are added/updated in the `hostvars` host facts and can be referenced by name just like any other host fact. They do not need to be registered in order to use them.

Fact | Returned | Description
--- | --- | ---
tcp_listen list | if TCP servers were found | A list of processes that are listening on a TCP port.
| address string | always | The address the server is listening on. Sample: 0.0.0.0
| name string | if user permissions allow | The name of the listening process. Sample: mysqld
| pid integer | always | The pid of the listening process. Sample: 1223
| port integer | always | The port the server is listening on. Sample: 3306
| protocol string | always | The network protocol of the server. Sample: tcp
| stime string | always | The start time of the listening process. Sample: Thu Feb 2 13:29:45 2017
| user string | always | The user who is running the listening process. Sample: mysql
udp_listen list | if UDP servers were found | A list of processes that are listening on a UDP port.
| address string | always | The address the server is listening on. Sample: 0.0.0.0
| name string | if user permissions allow | The name of the listening process. Sample: rsyslogd
| pid integer | always | The pid of the listening process. Sample: 609
| port integer | always | The port the server is listening on. Sample: 514
| protocol string | always | The network protocol of the server. Sample: udp
| stime string | always | The start time of the listening process. Sample: Thu Feb 2 13:29:45 2017
| user string | always | The user who is running the listening process. Sample: root

## Status

- This module is not guaranteed to have a backwards compatible interface. [preview]

- This module is maintained by the Ansible Community. [community]

### Authors

- Nathan Davison (@ndavison)

Hint

If you notice any issues in this documentation, you can edit this document to improve it.