feat(ansible): host edit page Ansible target link/create/unlink section
This commit is contained in:
+79
-4
@@ -147,14 +147,38 @@ async def create_host():
|
||||
@hosts_bp.get("/<host_id>/edit")
|
||||
@require_role(UserRole.operator)
|
||||
async def edit_host(host_id: str):
|
||||
from steward.models.ansible_inventory import AnsibleTarget
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
async with current_app.db_sessionmaker() as db:
|
||||
result = await db.execute(select(Host).where(Host.id == host_id))
|
||||
host = result.scalar_one_or_none()
|
||||
if host is None:
|
||||
return "Not found", 404
|
||||
if host is None:
|
||||
return "Not found", 404
|
||||
|
||||
linked_result = await db.execute(
|
||||
select(AnsibleTarget)
|
||||
.where(AnsibleTarget.host_id == host_id)
|
||||
.options(selectinload(AnsibleTarget.groups))
|
||||
)
|
||||
linked_target = linked_result.scalar_one_or_none()
|
||||
|
||||
# Only show targets not yet linked to any host (plus the currently linked one)
|
||||
linkable_result = await db.execute(
|
||||
select(AnsibleTarget)
|
||||
.where(AnsibleTarget.host_id.is_(None))
|
||||
.order_by(AnsibleTarget.name)
|
||||
)
|
||||
linkable_targets = linkable_result.scalars().all()
|
||||
|
||||
return await render_template(
|
||||
"hosts/form.html", host=host, probe_types=list(ProbeType),
|
||||
ansible_sources=_ansible_source_names())
|
||||
"hosts/form.html",
|
||||
host=host,
|
||||
probe_types=list(ProbeType),
|
||||
ansible_sources=_ansible_source_names(),
|
||||
linked_target=linked_target,
|
||||
linkable_targets=linkable_targets,
|
||||
)
|
||||
|
||||
|
||||
@hosts_bp.post("/<host_id>")
|
||||
@@ -178,6 +202,57 @@ async def update_host(host_id: str):
|
||||
return redirect(url_for("hosts.list_hosts"))
|
||||
|
||||
|
||||
@hosts_bp.post("/<host_id>/ansible-link")
|
||||
@require_role(UserRole.operator)
|
||||
async def ansible_link(host_id: str):
|
||||
"""Link, unlink, or create an AnsibleTarget from this host."""
|
||||
from steward.models.ansible_inventory import AnsibleTarget
|
||||
|
||||
form = await request.form
|
||||
action = form.get("action", "")
|
||||
|
||||
async with current_app.db_sessionmaker() as db:
|
||||
async with db.begin():
|
||||
if action == "unlink":
|
||||
linked_result = await db.execute(
|
||||
select(AnsibleTarget).where(AnsibleTarget.host_id == host_id)
|
||||
)
|
||||
linked = linked_result.scalar_one_or_none()
|
||||
if linked:
|
||||
linked.host_id = None
|
||||
|
||||
elif action == "link":
|
||||
target_id = form.get("target_id", "").strip()
|
||||
if target_id:
|
||||
result = await db.execute(
|
||||
select(AnsibleTarget).where(AnsibleTarget.id == target_id)
|
||||
)
|
||||
target = result.scalar_one_or_none()
|
||||
if target:
|
||||
# Clear any previous link for this host first
|
||||
old_result = await db.execute(
|
||||
select(AnsibleTarget).where(AnsibleTarget.host_id == host_id)
|
||||
)
|
||||
old = old_result.scalar_one_or_none()
|
||||
if old and old.id != target_id:
|
||||
old.host_id = None
|
||||
target.host_id = host_id
|
||||
|
||||
elif action == "create":
|
||||
host_result = await db.execute(select(Host).where(Host.id == host_id))
|
||||
host = host_result.scalar_one_or_none()
|
||||
if host:
|
||||
new_target = AnsibleTarget(
|
||||
id=str(uuid.uuid4()),
|
||||
name=host.name,
|
||||
address=host.address,
|
||||
host_id=host_id,
|
||||
)
|
||||
db.add(new_target)
|
||||
|
||||
return redirect(f"/hosts/{host_id}/edit")
|
||||
|
||||
|
||||
@hosts_bp.post("/<host_id>/run-playbook")
|
||||
@require_role(UserRole.operator)
|
||||
async def run_playbook(host_id: str):
|
||||
|
||||
@@ -88,4 +88,51 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{% if host %}
|
||||
<div class="card" style="margin-top:1.5rem;max-width:560px;margin-left:auto;margin-right:auto;">
|
||||
<h3 class="section-title">Ansible Target</h3>
|
||||
{% if linked_target %}
|
||||
<div style="display:flex;align-items:center;gap:1rem;padding:0.75rem;background:var(--bg-elevated);border-radius:4px;margin-bottom:1rem;">
|
||||
<div style="flex:1;">
|
||||
<strong>{{ linked_target.name }}</strong>
|
||||
<span style="color:var(--text-muted);font-size:0.85rem;margin-left:0.5rem;">{{ linked_target.address }}</span>
|
||||
{% if linked_target.groups %}
|
||||
<div style="margin-top:0.25rem;">
|
||||
{% for grp in linked_target.groups %}
|
||||
<span style="font-size:0.78rem;background:var(--bg);border-radius:3px;padding:0.1em 0.4em;margin-right:0.2rem;color:var(--text-muted);">{{ grp.name }}</span>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
<a href="/ansible/inventory/targets/{{ linked_target.id }}" class="btn btn-ghost btn-sm">Edit Target</a>
|
||||
<form method="post" action="/hosts/{{ host.id }}/ansible-link" style="display:inline;">
|
||||
<input type="hidden" name="action" value="unlink">
|
||||
<button type="submit" class="btn btn-ghost btn-sm">Unlink</button>
|
||||
</form>
|
||||
</div>
|
||||
{% else %}
|
||||
<p style="color:var(--text-muted);font-size:0.9rem;margin-bottom:1rem;">
|
||||
No Ansible target linked. Link an existing target or create one from this host.
|
||||
</p>
|
||||
<div style="display:flex;gap:0.75rem;flex-wrap:wrap;align-items:flex-end;">
|
||||
{% if linkable_targets %}
|
||||
<form method="post" action="/hosts/{{ host.id }}/ansible-link" style="display:flex;gap:0.5rem;align-items:center;flex:1;">
|
||||
<input type="hidden" name="action" value="link">
|
||||
<select name="target_id" style="flex:1;">
|
||||
{% for tgt in linkable_targets %}
|
||||
<option value="{{ tgt.id }}">{{ tgt.name }} ({{ tgt.address }})</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<button type="submit" class="btn btn-sm">Link</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
<form method="post" action="/hosts/{{ host.id }}/ansible-link">
|
||||
<input type="hidden" name="action" value="create">
|
||||
<button type="submit" class="btn btn-sm btn-ghost">Create Target from This Host</button>
|
||||
</form>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user