ssh암복호화 해결 해야함
This commit is contained in:
49
ansible/services.py
Normal file
49
ansible/services.py
Normal file
@ -0,0 +1,49 @@
|
||||
# services.py
|
||||
import os
|
||||
import tempfile
|
||||
import subprocess
|
||||
from .models import AnsibleTask
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def run_ansible_job(task: AnsibleTask, ssh_key: str):
|
||||
task.status = "running"
|
||||
task.save()
|
||||
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(delete=False, mode="w") as playbook_file, \
|
||||
tempfile.NamedTemporaryFile(delete=False, mode="w") as inventory_file, \
|
||||
tempfile.NamedTemporaryFile(delete=False, mode="w") as private_key_file:
|
||||
|
||||
playbook_file.write(task.playbook_content.strip())
|
||||
playbook_file.close()
|
||||
|
||||
inventory_file.write(task.inventory_content.strip())
|
||||
inventory_file.close()
|
||||
|
||||
private_key_file.write(ssh_key.strip() + "\n")
|
||||
private_key_file.close()
|
||||
os.chmod(private_key_file.name, 0o600)
|
||||
|
||||
command = [
|
||||
"ansible-playbook",
|
||||
playbook_file.name,
|
||||
"-i", inventory_file.name,
|
||||
"--private-key", private_key_file.name,
|
||||
"-u", "ubuntu",
|
||||
]
|
||||
|
||||
result = subprocess.run(command, capture_output=True, text=True)
|
||||
task.status = "success" if result.returncode == 0 else "failed"
|
||||
task.output = result.stdout + "\n" + result.stderr
|
||||
|
||||
except Exception as e:
|
||||
task.status = "error"
|
||||
task.output = f"\u274c 실행 중 예외 발생: {str(e)}"
|
||||
|
||||
finally:
|
||||
for f in [playbook_file.name, inventory_file.name, private_key_file.name]:
|
||||
if os.path.exists(f):
|
||||
os.remove(f)
|
||||
|
||||
task.save()
|
Reference in New Issue
Block a user