#!/bin/sh # Managed by Ansible — do not edit manually. # Registers this host with the device-inventory server (idempotent), # then runs a full hardware discovery and reports results back. set -eu HOST="{{ inventory_cli_server_host }}" PORT="{{ inventory_cli_server_port }}" NODE_NAME="{{ inventory_hostname }}" NODE_IP="{{ ansible_default_ipv4.address }}" DESCRIPTION="{{ inventory_cli_description }}" echo "[inventory-cli] node=$NODE_NAME ip=$NODE_IP server=$HOST:$PORT" # Check if server already registered (output format: "[N] name (hostname) loc: ...") SERVER_ID=$(device-inventory --host "$HOST" --port "$PORT" list-servers 2>/dev/null \ | grep "^\[[0-9]*\] $NODE_NAME (" | grep -o '^\[[0-9]*\]' | tr -d '[]') # Only register if not found if [ -z "$SERVER_ID" ]; then ADD_OUT=$(device-inventory --host "$HOST" --port "$PORT" \ add-server "$NODE_NAME" "$NODE_IP" "homelab" "$DESCRIPTION" 2>&1 || true) SERVER_ID=$(echo "$ADD_OUT" | grep -o '^\[[0-9]*\]' | tr -d '[]') fi if [ -z "$SERVER_ID" ]; then echo "[inventory-cli] ERROR: could not register or find server for $NODE_NAME" >&2 exit 1 fi echo "[inventory-cli] server_id=$SERVER_ID — running full hardware discovery" device-inventory --host "$HOST" --port "$PORT" discover "$SERVER_ID" echo "[inventory-cli] done"