#!/usr/bin/env bash set -euo pipefail # awx_trigger_inventory_update.sh # Trigger an AWX inventory source update and print its stdout. # Usage: # AWX_HOST=http://... AWX_USER=admin AWX_PASS=secret SRC_ID=9 ./awx_trigger_inventory_update.sh # Defaults are set to the local minikube/awx values used in this workspace. AWX_HOST="${AWX_HOST:-http://192.168.49.2:30081}" AWX_USER="${AWX_USER:-admin}" AWX_PASS="${AWX_PASS:-Aase#1234!}" SRC_ID="${SRC_ID:-9}" TIMEOUT_SECONDS="${TIMEOUT_SECONDS:-60}" AUTH=("-u" "${AWX_USER}:${AWX_PASS}") tmpfile=$(mktemp /tmp/awx_inv_resp.XXXX.json) cleanup() { rm -f "$tmpfile"; } trap cleanup EXIT echo "Triggering inventory source update for source id=$SRC_ID on $AWX_HOST" if ! curl -sS "${AUTH[@]}" -H "Content-Type: application/json" -X POST "$AWX_HOST/api/v2/inventory_sources/$SRC_ID/update/" -o "$tmpfile"; then echo "Warning: request failed (curl exit non-zero). See contents of $tmpfile:" >&2 cat "$tmpfile" >&2 || true fi # Try to read inventory_update id from the POST response INV_UP_ID=$(jq -r '.inventory_update.id // .id // empty' "$tmpfile" 2>/dev/null || true) # If not present, poll the inventory_source object to find the active update id if [ -z "$INV_UP_ID" ]; then echo "No inventory_update id in initial response, polling inventory_source for current/last update id..." poll_end=$((SECONDS + 15)) while [ $SECONDS -lt $poll_end ]; do src_json=$(curl -sS "${AUTH[@]}" "$AWX_HOST/api/v2/inventory_sources/$SRC_ID/" || true) # prefer current_update -> summary_fields.current_update -> last_job -> summary_fields.last_job -> last_update INV_UP_ID=$(echo "$src_json" | jq -r '.current_update.id // .summary_fields.current_update.id // .last_job.id // .summary_fields.last_job.id // .last_update.id // .summary_fields.last_update.id // empty' 2>/dev/null || true) if [ -n "$INV_UP_ID" ]; then break fi sleep 1 done fi # Final fallback: use newest inventory_updates list if [ -z "$INV_UP_ID" ]; then echo "Falling back to newest inventory_update from list..." INV_UP_ID=$(curl -sS "${AUTH[@]}" "$AWX_HOST/api/v2/inventory_sources/$SRC_ID/inventory_updates/?page_size=1" | jq -r '.results[0].id // empty' || true) fi if [ -z "$INV_UP_ID" ]; then echo "ERROR: could not determine inventory_update id. Response was:" >&2 jq -C '.' "$tmpfile" || cat "$tmpfile" || true exit 1 fi echo "Inventory update id: $INV_UP_ID" end=$((SECONDS + TIMEOUT_SECONDS)) status="" while [ $SECONDS -lt $end ]; do status=$(curl -sS "${AUTH[@]}" "$AWX_HOST/api/v2/inventory_updates/$INV_UP_ID/" | jq -r '.status // empty' || true) echo "Status: ${status:-unknown}" case "$status" in pending|running) sleep 2 ;; *) break ;; esac done echo "Final status: ${status:-unknown}" echo "\n--- STDOUT (raw text) for inventory_update $INV_UP_ID ---" curl -sS "${AUTH[@]}" "$AWX_HOST/api/v2/inventory_updates/$INV_UP_ID/stdout/?format=txt" || true