Improve pl task
This commit is contained in:
33
tasks.py
33
tasks.py
@@ -3,8 +3,10 @@
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from invoke.context import Context
|
||||
from invoke.exceptions import Exit
|
||||
from invoke.tasks import task
|
||||
|
||||
HOSTS_FILE = "production.yml"
|
||||
@@ -26,6 +28,24 @@ def _remote_host() -> str:
|
||||
return _yq(".ungrouped.hosts.server.ansible_host")
|
||||
|
||||
|
||||
def _rest_args() -> list[str]:
|
||||
"""Возвращает аргументы после '--' из sys.argv"""
|
||||
try:
|
||||
return sys.argv[sys.argv.index("--") + 1 :]
|
||||
except ValueError:
|
||||
return []
|
||||
|
||||
|
||||
def _resolve_playbook(name: str) -> str:
|
||||
candidates = [name, f"{name}.yml", f"playbook-{name}.yml"]
|
||||
for candidate in candidates:
|
||||
if os.path.isfile(candidate):
|
||||
return candidate
|
||||
raise Exit(
|
||||
f"Плейбук для '{name}' не найден. Проверял: {', '.join(candidates)}", code=1
|
||||
)
|
||||
|
||||
|
||||
@task
|
||||
def install_roles(ctx: Context) -> None:
|
||||
"""Установить ansible-galaxy roles"""
|
||||
@@ -33,9 +53,16 @@ def install_roles(ctx: Context) -> None:
|
||||
|
||||
|
||||
@task
|
||||
def pl(ctx: Context, args: str = "") -> None:
|
||||
"""Запустить плейбуки (передать файл плейбука через --args)"""
|
||||
ctx.run(f"uv run ansible-playbook -i production.yml --diff {args}")
|
||||
def pl(ctx: Context) -> None:
|
||||
"""Запустить плейбуки по имени: inv pl -- gitea miniflux"""
|
||||
names = _rest_args()
|
||||
if not names:
|
||||
raise Exit("Укажи хотя бы один плейбук: inv pl -- <name> [name ...]", code=1)
|
||||
playbooks = [_resolve_playbook(name) for name in names]
|
||||
ctx.run(
|
||||
f"uv run ansible-playbook -i production.yml --diff {' '.join(playbooks)}",
|
||||
pty=True,
|
||||
)
|
||||
|
||||
|
||||
@task
|
||||
|
||||
Reference in New Issue
Block a user