From 3bd25c682e33ce2be68d2f4cae3d539bf31e0820 Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Thu, 27 Jul 2023 22:25:14 -0400 Subject: [PATCH] Allow setting ip_address for execution nodes --- awx/api/serializers.py | 20 ++++++++++++++++++-- awx/api/views/__init__.py | 1 + awx/api/views/instance_install_bundle.py | 3 ++- awx/main/tasks/receptor.py | 3 ++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 94797182ca..9bf64b313f 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -5391,7 +5391,7 @@ class InstanceSerializer(BaseSerializer): class Meta: model = Instance - read_only_fields = ('ip_address', 'uuid', 'version') + read_only_fields = ('uuid', 'version') fields = ( 'id', 'hostname', @@ -5487,6 +5487,10 @@ class InstanceSerializer(BaseSerializer): node_type = get_field_from_model_or_attrs("node_type") peers_from_control_nodes = get_field_from_model_or_attrs("peers_from_control_nodes") listener_port = get_field_from_model_or_attrs("listener_port") + ip_address = get_field_from_model_or_attrs("ip_address") + hostname = get_field_from_model_or_attrs("hostname") + if not ip_address: + attrs["ip_address"] = hostname if peers_from_control_nodes and node_type not in (Instance.Types.EXECUTION, Instance.Types.HOP): raise serializers.ValidationError(_("peers_from_control_nodes can only be enabled for execution or hop nodes.")) @@ -5549,7 +5553,19 @@ class InstanceSerializer(BaseSerializer): class InstanceHealthCheckSerializer(BaseSerializer): class Meta: model = Instance - read_only_fields = ('uuid', 'hostname', 'version', 'last_health_check', 'errors', 'cpu', 'memory', 'cpu_capacity', 'mem_capacity', 'capacity') + read_only_fields = ( + 'uuid', + 'hostname', + 'ip_address', + 'version', + 'last_health_check', + 'errors', + 'cpu', + 'memory', + 'cpu_capacity', + 'mem_capacity', + 'capacity', + ) fields = read_only_fields diff --git a/awx/api/views/__init__.py b/awx/api/views/__init__.py index de432ac824..b42d51b5d3 100644 --- a/awx/api/views/__init__.py +++ b/awx/api/views/__init__.py @@ -344,6 +344,7 @@ class InstanceDetail(RetrieveUpdateAPIView): data.pop('listener_port', None) data.pop('node_type', None) data.pop('hostname', None) + data.pop('ip_address', None) return super(InstanceDetail, self).update_raw_data(data) def update(self, request, *args, **kwargs): diff --git a/awx/api/views/instance_install_bundle.py b/awx/api/views/instance_install_bundle.py index 060ccf9f20..9b9e45c2d7 100644 --- a/awx/api/views/instance_install_bundle.py +++ b/awx/api/views/instance_install_bundle.py @@ -121,7 +121,8 @@ def generate_inventory_yml(instance_obj): def generate_group_vars_all_yml(instance_obj): peers = [] for instance in instance_obj.peers.all(): - peers.append(dict(host=instance.hostname, port=instance.listener_port)) + host_or_ip = instance.ip_address or instance.hostname + peers.append(dict(host=host_or_ip, port=instance.listener_port)) return render_to_string("instance_install_bundle/group_vars/all.yml", context=dict(instance=instance_obj, peers=peers)) diff --git a/awx/main/tasks/receptor.py b/awx/main/tasks/receptor.py index 877b842ded..a93f727f5e 100644 --- a/awx/main/tasks/receptor.py +++ b/awx/main/tasks/receptor.py @@ -704,7 +704,8 @@ def generate_config_data(): receptor_config = list(RECEPTOR_CONFIG_STARTER) for instance in instances: - peer = {'tcp-peer': {'address': f'{instance.hostname}:{instance.listener_port}', 'tls': 'tlsclient'}} + host_or_ip = instance.ip_address or instance.hostname + peer = {'tcp-peer': {'address': f'{host_or_ip}:{instance.listener_port}', 'tls': 'tlsclient'}} receptor_config.append(peer) should_update = should_update_config(instances) return receptor_config, should_update