diff --git a/apps/assets/migrations/0111_auto_20230321_1633.py b/apps/assets/migrations/0111_auto_20230321_1633.py index ad7d57982..0f1ba7f14 100644 --- a/apps/assets/migrations/0111_auto_20230321_1633.py +++ b/apps/assets/migrations/0111_auto_20230321_1633.py @@ -8,7 +8,7 @@ def migrate_platform_charset(apps, schema_editor): platform_model.objects.filter(charset='utf8').update(charset='utf-8') -def migrate_platform_protocol_required(apps, schema_editor): +def migrate_platform_protocol_primary(apps, schema_editor): platform_model = apps.get_model('assets', 'Platform') platforms = platform_model.objects.all() @@ -31,5 +31,5 @@ class Migration(migrations.Migration): field=models.BooleanField(default=False, verbose_name='Primary'), ), migrations.RunPython(migrate_platform_charset), - migrations.RunPython(migrate_platform_protocol_required), + migrations.RunPython(migrate_platform_protocol_primary), ] diff --git a/apps/assets/serializers/platform.py b/apps/assets/serializers/platform.py index c4b51934b..21c6a124a 100644 --- a/apps/assets/serializers/platform.py +++ b/apps/assets/serializers/platform.py @@ -132,6 +132,7 @@ class PlatformSerializer(WritableNestedModelSerializer): if not primary: protocols[0]['primary'] = True protocols[0]['default'] = False + # 这里不设置不行,write_nested 不使用 validated 中的 self.initial_data['protocols'] = protocols return protocols diff --git a/apps/ops/ansible/inventory.py b/apps/ops/ansible/inventory.py index 6048d9472..e3954c063 100644 --- a/apps/ops/ansible/inventory.py +++ b/apps/ops/ansible/inventory.py @@ -100,9 +100,17 @@ class JMSInventory: host.update(self.make_proxy_command(gateway)) def asset_to_host(self, asset, account, automation, protocols, platform): - if protocols: - protocol = protocols[0].name - port = protocol[0].port + primary_protocol = [p for p in protocols if p.primary] + if len(primary_protocol) >= 1: + primary = primary_protocol[0] + elif protocols: + primary = protocols[0] + else: + primary = None + + if primary: + protocol = primary.name + port = primary.port else: protocol = 'null' port = 0