fix: ansible playbook render and run in localhost

pull/12862/head
ibuler 2024-03-22 16:23:12 +08:00 committed by Bryan
parent eb9f261459
commit adbd73182b
4 changed files with 21 additions and 6 deletions

View File

@ -12,7 +12,7 @@ from sshtunnel import SSHTunnelForwarder
from assets.automations.methods import platform_automation_methods from assets.automations.methods import platform_automation_methods
from common.utils import get_logger, lazyproperty, is_openssh_format_key, ssh_pubkey_gen from common.utils import get_logger, lazyproperty, is_openssh_format_key, ssh_pubkey_gen
from ops.ansible import JMSInventory, PlaybookRunner, DefaultCallback from ops.ansible import JMSInventory, SuperPlaybookRunner, DefaultCallback
logger = get_logger(__name__) logger = get_logger(__name__)
@ -269,7 +269,7 @@ class BasePlaybookManager:
if not playbook_path: if not playbook_path:
continue continue
runer = PlaybookRunner( runer = SuperPlaybookRunner(
inventory_path, inventory_path,
playbook_path, playbook_path,
self.runtime_dir, self.runtime_dir,

View File

@ -1,6 +1,7 @@
import os import os
import uuid
import shutil import shutil
import uuid
import ansible_runner import ansible_runner
from django.conf import settings from django.conf import settings
from django.utils._os import safe_join from django.utils._os import safe_join
@ -43,6 +44,9 @@ class AdHocRunner:
if not os.path.exists(self.project_dir): if not os.path.exists(self.project_dir):
os.mkdir(self.project_dir, 0o755) os.mkdir(self.project_dir, 0o755)
private_env = safe_join(self.project_dir, 'env')
if os.path.exists(private_env):
shutil.rmtree(private_env)
ansible_runner.run( ansible_runner.run(
timeout=self.timeout if self.timeout > 0 else None, timeout=self.timeout if self.timeout > 0 else None,
@ -69,9 +73,13 @@ class PlaybookRunner:
if not callback: if not callback:
callback = DefaultCallback() callback = DefaultCallback()
self.cb = callback self.cb = callback
self.envs = {}
def run(self, verbosity=0, **kwargs): def run(self, verbosity=0, **kwargs):
verbosity = get_ansible_log_verbosity(verbosity) verbosity = get_ansible_log_verbosity(verbosity)
private_env = safe_join(self.project_dir, 'env')
if os.path.exists(private_env):
shutil.rmtree(private_env)
ansible_runner.run( ansible_runner.run(
private_data_dir=self.project_dir, private_data_dir=self.project_dir,
@ -81,11 +89,18 @@ class PlaybookRunner:
event_handler=self.cb.event_handler, event_handler=self.cb.event_handler,
status_handler=self.cb.status_handler, status_handler=self.cb.status_handler,
host_cwd=self.project_dir, host_cwd=self.project_dir,
envvars=self.envs,
**kwargs **kwargs
) )
return self.cb return self.cb
class SuperPlaybookRunner(PlaybookRunner):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.envs = {"LOCAL_CONNECTION_ENABLED": "1"}
class UploadFileRunner: class UploadFileRunner:
def __init__(self, inventory, job_id, dest_path, callback=None): def __init__(self, inventory, job_id, dest_path, callback=None):
self.id = uuid.uuid4() self.id = uuid.uuid4()

View File

@ -9,7 +9,7 @@ from django.utils import timezone
from common.db.utils import safe_db_connection from common.db.utils import safe_db_connection
from common.utils import get_logger, random_string from common.utils import get_logger, random_string
from ops.ansible import PlaybookRunner, JMSInventory from ops.ansible import SuperPlaybookRunner, JMSInventory
from terminal.models import Applet, AppletHostDeployment from terminal.models import Applet, AppletHostDeployment
logger = get_logger(__name__) logger = get_logger(__name__)
@ -114,7 +114,7 @@ class DeployAppletHostManager:
def _run_playbook(self, generate_playbook: callable, **kwargs): def _run_playbook(self, generate_playbook: callable, **kwargs):
inventory = self.generate_inventory() inventory = self.generate_inventory()
playbook = generate_playbook() playbook = generate_playbook()
runner = PlaybookRunner( runner = SuperPlaybookRunner(
inventory=inventory, playbook=playbook, project_dir=self.run_dir inventory=inventory, playbook=playbook, project_dir=self.run_dir
) )
return runner.run(**kwargs) return runner.run(**kwargs)

View File

@ -11,7 +11,7 @@ python = "^3.11"
cython = "3.0.0" cython = "3.0.0"
aiofiles = "23.1.0" aiofiles = "23.1.0"
amqp = "5.1.1" amqp = "5.1.1"
ansible-core = { url = "https://github.com/jumpserver/ansible/releases/download/v2.14.1.2/ansible-2.14.1.2.zip" } ansible-core = { url = "https://github.com/jumpserver/ansible/archive/refs/tags/v2.14.1.4.zip" }
ansible = "7.1.0" ansible = "7.1.0"
ansible-runner = "2.3.3" ansible-runner = "2.3.3"
asn1crypto = "1.5.1" asn1crypto = "1.5.1"