diff --git a/changelogs/fragments/6165-nmap-port.yml b/changelogs/fragments/6165-nmap-port.yml new file mode 100644 index 0000000000..4daf2790ba --- /dev/null +++ b/changelogs/fragments/6165-nmap-port.yml @@ -0,0 +1,2 @@ +minor_changes: + - nmap inventory plugin - add new option ``port`` for port specific scan (https://github.com/ansible-collections/community.general/pull/6165). \ No newline at end of file diff --git a/plugins/inventory/nmap.py b/plugins/inventory/nmap.py index f0fa50e3b3..68ab2f9117 100644 --- a/plugins/inventory/nmap.py +++ b/plugins/inventory/nmap.py @@ -34,6 +34,13 @@ DOCUMENTATION = ''' description: list of addresses to exclude type: list elements: string + port: + description: + - Only scan specific port or port range (C(-p)). + - For example, you could pass C(22) for a single port, C(1-65535) for a range of ports, + or C(U:53,137,T:21-25,139,8080,S:9) to check port 53 with UDP, ports 21-25 with TCP, port 9 with SCTP, and ports 137, 139, and 8080 with all. + type: string + version_added: 6.5.0 ports: description: Enable/disable scanning for open ports type: boolean @@ -81,6 +88,14 @@ plugin: community.general.nmap sudo: true strict: false address: 192.168.0.0/24 + +# an nmap scan specifying ports and classifying results to an inventory group +plugin: community.general.nmap +address: 192.168.0.0/24 +exclude: 192.168.0.1, web.example.com +port: 22, 443 +groups: + web_servers: "ports | selectattr('port', 'equalto', '443')" ''' import os @@ -171,6 +186,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): if self._options['sudo']: cmd.insert(0, 'sudo') + if self._options['port']: + cmd.append('-p') + cmd.append(self._options['port']) + if not self._options['ports']: cmd.append('-sP')