From ce4c1c11b3e818ca17d7cf4db2bad6ba08becf17 Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Tue, 14 Mar 2023 15:26:00 -0400 Subject: [PATCH] Remove towervars from constructed inventory hosts (#13686) --- awx/main/management/commands/inventory_import.py | 8 +++++++- awx/main/models/inventory.py | 13 ++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/awx/main/management/commands/inventory_import.py b/awx/main/management/commands/inventory_import.py index 6fa5b59346..8150936054 100644 --- a/awx/main/management/commands/inventory_import.py +++ b/awx/main/management/commands/inventory_import.py @@ -654,13 +654,19 @@ class Command(BaseCommand): mem_host = self.all_group.all_hosts[mem_host_name] import_vars = mem_host.variables host_desc = import_vars.pop('_awx_description', 'imported') - host_attrs = dict(variables=json.dumps(import_vars), description=host_desc) + host_attrs = dict(description=host_desc) enabled = self._get_enabled(mem_host.variables) if enabled is not None: host_attrs['enabled'] = enabled if self.instance_id_var: instance_id = self._get_instance_id(mem_host.variables) host_attrs['instance_id'] = instance_id + if self.inventory.kind == 'constructed': + # remote towervars so the constructed hosts do not have extra variables + for prefix in ('host', 'tower'): + for var in ('remote_{}_enabled', 'remote_{}_id'): + import_vars.pop(var.format(prefix), None) + host_attrs['variables'] = json.dumps(import_vars) try: sanitize_jinja(mem_host_name) except ValueError as e: diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index 978e520278..010cfaa342 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -357,13 +357,12 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin, RelatedJobsMixin): for host in hosts: data['_meta']['hostvars'][host.name] = host.variables_dict if towervars: - tower_dict = dict( - remote_tower_enabled=str(host.enabled).lower(), - remote_tower_id=host.id, - remote_host_enabled=str(host.enabled).lower(), - remote_host_id=host.id, - ) - data['_meta']['hostvars'][host.name].update(tower_dict) + for prefix in ('host', 'tower'): + tower_dict = { + f'remote_{prefix}_enabled': str(host.enabled).lower(), + f'remote_{prefix}_id': host.id, + } + data['_meta']['hostvars'][host.name].update(tower_dict) return data