From 07eebd93fb98977944230a66a7d0c66f95095c52 Mon Sep 17 00:00:00 2001 From: jiangweidong Date: Wed, 15 Mar 2023 15:16:27 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=B5=8B=E8=AF=95=E8=B5=84=E4=BA=A7?= =?UTF-8?q?=E5=8F=AF=E8=BF=9E=E6=8E=A5=E6=80=A7=E9=80=89=E6=8B=A9=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E7=AD=96=E7=95=A5=E4=BC=98=E5=8C=96=20(#9954)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/ansible/inventory.py | 48 ++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/apps/ops/ansible/inventory.py b/apps/ops/ansible/inventory.py index e5df2332b..2dbfc44ef 100644 --- a/apps/ops/ansible/inventory.py +++ b/apps/ops/ansible/inventory.py @@ -17,7 +17,7 @@ class JMSInventory: :param account_policy: privileged_only, privileged_first, skip """ self.assets = self.clean_assets(assets) - self.account_prefer = account_prefer + self.account_prefer = self.get_account_prefer(account_prefer) self.account_policy = account_policy self.host_callback = host_callback self.exclude_hosts = {} @@ -146,33 +146,45 @@ class JMSInventory: accounts_connectivity_no = list(accounts.exclude(connectivity=Connectivity.OK)) return accounts_connectivity_ok + accounts_connectivity_no + @staticmethod + def get_account_prefer(account_prefer): + account_usernames = [] + if isinstance(account_prefer, str) and account_prefer: + account_usernames = list(map(lambda x: x.lower(), account_prefer.split(','))) + return account_usernames + + def get_refer_account(self, accounts): + account = None + if accounts: + account = list(filter( + lambda a: a.username.lower() in self.account_prefer, accounts + )) + account = account[0] if account else None + return account + def select_account(self, asset): accounts = self.get_asset_accounts(asset) - if not accounts: + if not accounts or self.account_policy == 'skip': return None account_selected = None - account_usernames = self.account_prefer - if isinstance(self.account_prefer, str) and account_usernames: - account_usernames = self.account_prefer.split(',') - - # 优先使用提供的名称 - if account_usernames: - account_matched = list(filter(lambda account: account.username in account_usernames, accounts)) - account_selected = account_matched[0] if account_matched else None - - if account_selected or self.account_policy == 'skip': - return account_selected + # 首先找到特权账号 + privileged_accounts = list(filter(lambda account: account.privileged, accounts)) + # 不同类型的账号选择,优先使用提供的名称 + refer_privileged_account = self.get_refer_account(privileged_accounts) if self.account_policy in ['privileged_only', 'privileged_first']: - account_selected = accounts[0] if accounts else None + first_privileged = privileged_accounts[0] if privileged_accounts else None + account_selected = refer_privileged_account or first_privileged - if account_selected: + # 此策略不管是否匹配到账号都需强制返回 + if self.account_policy == 'privileged_only': return account_selected - if self.account_policy == 'privileged_first': - account_selected = accounts[0] if accounts else None - return account_selected + if not account_selected: + account_selected = self.get_refer_account(accounts) + + return account_selected or accounts[0] def generate(self, path_dir): hosts = []