From 18a8791ece69fa27075f18bf4a8e1311421fcf83 Mon Sep 17 00:00:00 2001 From: RealCharlesChia <161665317+RealCharlesChia@users.noreply.github.com> Date: Sun, 26 Apr 2026 03:07:19 +0800 Subject: [PATCH] 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 --- .../fragments/11766-nmap-set-name-variable.yaml | 5 +++++ plugins/inventory/nmap.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 changelogs/fragments/11766-nmap-set-name-variable.yaml diff --git a/changelogs/fragments/11766-nmap-set-name-variable.yaml b/changelogs/fragments/11766-nmap-set-name-variable.yaml new file mode 100644 index 0000000000..f7a2cca6cb --- /dev/null +++ b/changelogs/fragments/11766-nmap-set-name-variable.yaml @@ -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). diff --git a/plugins/inventory/nmap.py b/plugins/inventory/nmap.py index 668b49077d..5c234137f0 100644 --- a/plugins/inventory/nmap.py +++ b/plugins/inventory/nmap.py @@ -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