1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

nmap: fix cache support (#2282) (#2328)

* add cache support

* pep8 e501 fix

* revert verify_file function

* revert description update

* add changelog fragment

Co-authored-by: Dennis Israelsson <github@mdh.nu>
(cherry picked from commit 31c9ed0fe6)

Co-authored-by: Dennis Israelsson <dennis.israelsson@gmail.com>
This commit is contained in:
patchback[bot] 2021-04-22 07:47:45 +02:00 committed by GitHub
parent 457c92c8e2
commit c7736ab921
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 61 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- nmap inventory plugin - fix cache and constructed group support (https://github.com/ansible-collections/community.general/issues/2242).

View file

@ -72,6 +72,25 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
self._nmap = None self._nmap = None
super(InventoryModule, self).__init__() super(InventoryModule, self).__init__()
def _populate(self, hosts):
# Use constructed if applicable
strict = self.get_option('strict')
for host in hosts:
hostname = host['name']
self.inventory.add_host(hostname)
for var, value in host.items():
self.inventory.set_variable(hostname, var, value)
# Composed variables
self._set_composite_vars(self.get_option('compose'), host, hostname, strict=strict)
# Complex groups based on jinja2 conditionals, hosts that meet the conditional are added to group
self._add_host_to_composed_groups(self.get_option('groups'), host, hostname, strict=strict)
# Create groups based on variable values and add the corresponding hosts to it
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), host, hostname, strict=strict)
def verify_file(self, path): def verify_file(self, path):
valid = False valid = False
@ -83,7 +102,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
return valid return valid
def parse(self, inventory, loader, path, cache=False): def parse(self, inventory, loader, path, cache=True):
try: try:
self._nmap = get_bin_path('nmap') self._nmap = get_bin_path('nmap')
@ -94,6 +113,25 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
self._read_config_data(path) self._read_config_data(path)
cache_key = self.get_cache_key(path)
# cache may be True or False at this point to indicate if the inventory is being refreshed
# get the user's cache option too to see if we should save the cache if it is changing
user_cache_setting = self.get_option('cache')
# read if the user has caching enabled and the cache isn't being refreshed
attempt_to_read_cache = user_cache_setting and cache
# update if the user has caching enabled and the cache is being refreshed; update this value to True if the cache has expired below
cache_needs_update = user_cache_setting and not cache
if attempt_to_read_cache:
try:
results = self._cache[cache_key]
except KeyError:
# This occurs if the cache_key is not in the cache or if the cache_key expired, so the cache needs to be updated
cache_needs_update = True
if cache_needs_update:
# setup command # setup command
cmd = [self._nmap] cmd = [self._nmap]
if not self._options['ports']: if not self._options['ports']:
@ -122,6 +160,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
host = None host = None
ip = None ip = None
ports = [] ports = []
results = []
try: try:
t_stdout = to_text(stdout, errors='surrogate_or_strict') t_stdout = to_text(stdout, errors='surrogate_or_strict')
@ -131,8 +170,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
for line in t_stdout.splitlines(): for line in t_stdout.splitlines():
hits = self.find_host.match(line) hits = self.find_host.match(line)
if hits: if hits:
if host is not None: if host is not None and ports:
self.inventory.set_variable(host, 'ports', ports) results[-1]['ports'] = ports
# if dns only shows arpa, just use ip instead as hostname # if dns only shows arpa, just use ip instead as hostname
if hits.group(1).endswith('.in-addr.arpa'): if hits.group(1).endswith('.in-addr.arpa'):
@ -148,21 +187,27 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
if host is not None: if host is not None:
# update inventory # update inventory
self.inventory.add_host(host) results.append(dict())
self.inventory.set_variable(host, 'ip', ip) results[-1]['name'] = host
results[-1]['ip'] = ip
ports = [] ports = []
continue continue
host_ports = self.find_port.match(line) host_ports = self.find_port.match(line)
if host is not None and host_ports: if host is not None and host_ports:
ports.append({'port': host_ports.group(1), 'protocol': host_ports.group(2), 'state': host_ports.group(3), 'service': host_ports.group(4)}) ports.append({'port': host_ports.group(1),
'protocol': host_ports.group(2),
'state': host_ports.group(3),
'service': host_ports.group(4)})
continue continue
# TODO: parse more data, OS?
# if any leftovers # if any leftovers
if host and ports: if host and ports:
self.inventory.set_variable(host, 'ports', ports) results[-1]['ports'] = ports
except Exception as e: except Exception as e:
raise AnsibleParserError("failed to parse %s: %s " % (to_native(path), to_native(e))) raise AnsibleParserError("failed to parse %s: %s " % (to_native(path), to_native(e)))
self._cache[cache_key] = results
self._populate(results)