Fix nmap inventory plugin: skip setting reserved 'name' variable (#11893)

* Fix nmap inventory plugin: skip setting reserved 'name' variable

Skip setting 'name' as a host variable to avoid Ansible warning:
'[WARNING]: Found variable using reserved name name'

The 'name' is already used as the hostname, so it's redundant as a
separate host variable. This fixes the warning that appears when using
the nmap inventory plugin.

Fixes: https://github.com/ansible-collections/community.general/issues/11766

* nmap inventory plugin: add set_name_variable option to control name variable
Add a new 'set_name_variable' option to control whether the 'name' variable
is set for each host. When true (default), maintains backward compatibility.
When false, skips setting the 'name' variable to avoid the 'Found variable
using reserved name' warning.
Fixes: https://github.com/ansible-collections/community.general/issues/11766

* Address review feedback: fix changelog category and version number

- Change 'bugfixes' to 'minor_changes' in changelog fragment
- Add PR and issue links to changelog description
- Update version_added from 10.6.0 to 13.0.0

Reviewed-by: felixfontein

---------

Co-authored-by: jiaza <jiaza@nscc-tj.cn>
This commit is contained in:
RealCharlesChia
2026-04-26 03:07:19 +08:00
committed by GitHub
parent e2b0d39d14
commit 18a8791ece
2 changed files with 22 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
minor_changes:
- nmap inventory plugin - added ``set_name_variable`` option to control whether the ``name`` variable is set for each host,
allowing users to avoid the "Found variable using reserved name" warning while maintaining backward compatibility
(https://github.com/ansible-collections/community.general/pull/11893, https://github.com/ansible-collections/community.general/issues/11766).

View File

@@ -95,6 +95,14 @@ options:
type: boolean
default: true
version_added: 7.4.0
set_name_variable:
description:
- Set the C(name) variable for each host.
- When V(true), sets the C(name) variable which may trigger a warning about using a reserved name.
- Set to V(false) to avoid the warning when C(name) is not needed as a variable.
type: boolean
default: true
version_added: 13.0.0
notes:
- At least one of O(ipv4) or O(ipv6) is required to be V(true); both can be V(true), but they cannot both be V(false).
- 'TODO: add OS fingerprinting.'
@@ -121,6 +129,12 @@ exclude: 192.168.0.1, web.example.com
port: 22, 443
groups:
web_servers: "ports | selectattr('port', 'equalto', '443')"
---
# an nmap scan without setting the 'name' variable to avoid warnings
plugin: community.general.nmap
address: 192.168.0.0/24
set_name_variable: false
"""
import os
@@ -148,12 +162,15 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def _populate(self, hosts):
# Use constructed if applicable
strict = self.get_option("strict")
set_name_variable = self.get_option("set_name_variable")
for host in hosts:
host = make_unsafe(host)
hostname = host["name"]
self.inventory.add_host(hostname)
for var, value in host.items():
if var == "name" and not set_name_variable:
continue
self.inventory.set_variable(hostname, var, value)
# Composed variables