diff --git a/apps/ops/ansible/inventory.py b/apps/ops/ansible/inventory.py index 804f1ba18..963406e6a 100644 --- a/apps/ops/ansible/inventory.py +++ b/apps/ops/ansible/inventory.py @@ -214,13 +214,18 @@ class JMSInventory: ) return host - def get_asset_sorted_accounts(self, asset): - accounts = list(asset.accounts.filter(is_active=True)) + @staticmethod + def sorted_accounts(accounts): connectivity_score = {'ok': 2, '-': 1, 'err': 0} sort_key = lambda x: (x.privileged, connectivity_score.get(x.connectivity, 0), x.date_updated) accounts_sorted = sorted(accounts, key=sort_key, reverse=True) return accounts_sorted + def get_asset_sorted_accounts(self, asset): + accounts = list(asset.accounts.filter(is_active=True)) + accounts_sorted = self.sorted_accounts(accounts) + return accounts_sorted + @staticmethod def get_account_prefer(account_prefer): account_usernames = [] diff --git a/apps/ops/models/job.py b/apps/ops/models/job.py index cb81a565d..e7245568b 100644 --- a/apps/ops/models/job.py +++ b/apps/ops/models/job.py @@ -94,7 +94,8 @@ class JMSPermedInventory(JMSInventory): def get_asset_sorted_accounts(self, asset): accounts = self.assets_accounts_mapper.get(asset.id, []) - return list(accounts) + accounts_sorted = self.sorted_accounts(accounts) + return list(accounts_sorted) def get_assets_accounts_mapper(self): mapper = defaultdict(set)