From f921f12171e87ff88d5c71dad144692f68c0704b Mon Sep 17 00:00:00 2001 From: ibuler Date: Sat, 8 Oct 2022 19:51:29 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20adhoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/ansible/inventory.py | 29 +++++++++++++++++++---------- apps/ops/ansible/runner.py | 3 --- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/apps/ops/ansible/inventory.py b/apps/ops/ansible/inventory.py index 85a3d03ff..6a7e3c5aa 100644 --- a/apps/ops/ansible/inventory.py +++ b/apps/ops/ansible/inventory.py @@ -3,6 +3,8 @@ from collections import defaultdict import json import os +from django.utils.translation import gettext as _ + __all__ = ['JMSInventory'] @@ -60,6 +62,7 @@ class JMSInventory: host = {'name': asset.name, 'vars': { 'asset_id': str(asset.id), 'asset_name': asset.name, 'asset_type': asset.type, 'asset_category': asset.category, + 'exclude': '' }} ansible_connection = automation.ansible_config.get('ansible_connection', 'ssh') gateway = None @@ -87,6 +90,8 @@ class JMSInventory: host['ansible_password'] = account.secret elif account.secret_type == 'private_key' and account.secret: host['ssh_private_key'] = account.private_key_file + else: + host['vars']['exclude'] = _("No account found") if gateway: host['vars'].update(self.make_proxy_command(gateway)) @@ -99,28 +104,26 @@ class JMSInventory: def select_account(self, asset): accounts = list(asset.accounts.all()) - if not accounts: - return None - account_selected = None account_username = self.account_username if isinstance(self.account_username, str): account_username = [self.account_username] + if account_username: for username in account_username: account_matched = list(filter(lambda account: account.username == username, accounts)) if account_matched: account_selected = account_matched[0] - return account_selected + break if not account_selected: if self.account_policy in ['privileged_must', 'privileged_first']: - account_selected = list(filter(lambda account: account.is_privileged, accounts)) - account_selected = account_selected[0] if account_selected else None + account_matched = list(filter(lambda account: account.is_privileged, accounts)) + account_selected = account_matched[0] if account_matched else None if not account_selected and self.account_policy == 'privileged_first': - account_selected = accounts[0] + account_selected = accounts[0] if accounts else None return account_selected def generate(self): @@ -130,14 +133,20 @@ class JMSInventory: automation = platform.automation protocols = platform.protocols.all() - if not automation.ansible_enabled: - continue - for asset in self.assets: account = self.select_account(asset) host = self.asset_to_host(asset, account, automation, protocols) + if not automation.ansible_enabled: + host['vars']['exclude'] = _('Ansible disabled') hosts.append(host) + exclude_hosts = list(filter(lambda x: x.get('exclude'), hosts)) + if exclude_hosts: + print(_("Skip hosts below:")) + for host in exclude_hosts: + print(" {}:\t{}".format(host['name'], host['exclude'])) + + hosts = list(filter(lambda x: not x.get('exclude'), hosts)) data = {'all': {'hosts': {}}} for host in hosts: name = host.pop('name') diff --git a/apps/ops/ansible/runner.py b/apps/ops/ansible/runner.py index 36a0e7e8c..e8f232e74 100644 --- a/apps/ops/ansible/runner.py +++ b/apps/ops/ansible/runner.py @@ -37,9 +37,6 @@ class AdHocRunner: if not os.path.exists(self.project_dir): os.mkdir(self.project_dir, 0o755) - print("inventory: ") - print(self.inventory) - ansible_runner.run( host_pattern=self.pattern, private_data_dir=self.project_dir,