Files
awx/scripts/create_cred_and_ping.sh

100 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# create_cred_and_ping.sh
# Creates an AWX Machine credential from a local private key and runs an ad-hoc ping
# against three static hosts. Configure AWX_HOST/AWX_USER/AWX_PASS via env if needed.
AWX_HOST="${AWX_HOST:-http://192.168.49.2:30081}"
AWX_USER="${AWX_USER:-admin}"
AWX_PASS="${AWX_PASS:-Aase#1234!}"
KEY_PATH="${KEY_PATH:-$HOME/.ssh/awx_deploy_key}"
CREDS_NAME="${CREDS_NAME:-auto-machine-awx_deploy_key}"
USERNAME="${USERNAME:-admin}"
ORG_ID="${ORG_ID:-1}"
if [ ! -f "$KEY_PATH" ]; then
echo "ERROR: private key not found at $KEY_PATH" >&2
exit 1
fi
TMP_JSON="/tmp/awx_new_cred.json"
TMP_BODY="/tmp/awx_new_cred_body.json"
TMP_CODE="/tmp/awx_new_cred_code.txt"
cat > /tmp/make_cred.py <<'PY'
import json,sys
keypath=sys.argv[1]
name=sys.argv[2]
username=sys.argv[3]
ct=int(sys.argv[4])
org=int(sys.argv[5])
with open(keypath,'r',encoding='utf-8') as f:
key=f.read()
payload={'name': name, 'credential_type': ct, 'organization': org, 'inputs': {'username': username, 'ssh_key_data': key}}
print(json.dumps(payload))
PY
python3 /tmp/make_cred.py "$KEY_PATH" "$CREDS_NAME" "$USERNAME" 1 "$ORG_ID" > "$TMP_JSON"
echo "Creating AWX credential from $KEY_PATH..."
curl -sS -u "$AWX_USER:$AWX_PASS" -H "Content-Type: application/json" -X POST "$AWX_HOST/api/v2/credentials/" -d "@$TMP_JSON" -o "$TMP_BODY" -w "%{http_code}" > "$TMP_CODE" || true
CODE=$(cat "$TMP_CODE" || true)
echo "HTTP status: $CODE"
cat "$TMP_BODY" || true
if [ "$CODE" -lt 200 ] || [ "$CODE" -ge 300 ]; then
echo "Failed to create credential (status=$CODE)." >&2
exit 1
fi
NEW_CRED_ID=$(jq -r '.id // empty' "$TMP_BODY")
if [ -z "$NEW_CRED_ID" ]; then
echo "No credential id returned; aborting" >&2
exit 1
fi
echo "Created credential id=$NEW_CRED_ID"
# Prepare ad-hoc payload
INV_ID="${INV_ID:-2}"
LIMIT="host-192-168-1-144,host-192-168-1-23,host-192-168-1-24"
ADHOC_JSON="/tmp/awx_adhoc.json"
cat > "$ADHOC_JSON" <<EOF
{
"inventory": $INV_ID,
"credential": $NEW_CRED_ID,
"module_name": "ping",
"module_args": "",
"limit": "$LIMIT"
}
EOF
echo "Launching ad-hoc ping..."
RESP_FILE="/tmp/awx_adhoc_resp.json"
curl -sS -u "$AWX_USER:$AWX_PASS" -H "Content-Type: application/json" -X POST "$AWX_HOST/api/v2/ad_hoc_commands/" -d "@$ADHOC_JSON" -o "$RESP_FILE" || true
cat "$RESP_FILE" || true
ADHOC_ID=$(jq -r '.id // empty' "$RESP_FILE" || true)
if [ -z "$ADHOC_ID" ]; then echo "Failed to launch ad-hoc command" >&2; exit 1; fi
echo "Launched ad-hoc id=$ADHOC_ID; polling until finished (180s max)"
END=$((SECONDS+180))
while [ $SECONDS -lt $END ]; do
STATUS=$(curl -sS -u "$AWX_USER:$AWX_PASS" "$AWX_HOST/api/v2/ad_hoc_commands/$ADHOC_ID/" | jq -r '.status // empty' || true)
echo "Status: ${STATUS:-unknown}"
if [ -n "$STATUS" ] && [ "$STATUS" != "running" ] && [ "$STATUS" != "pending" ]; then
break
fi
sleep 2
done
echo "Final status: ${STATUS:-unknown}"
echo "--- events (first 500) ---"
curl -sS -u "$AWX_USER:$AWX_PASS" "$AWX_HOST/api/v2/ad_hoc_commands/$ADHOC_ID/events/?page_size=500" | jq '.results[] | {counter: .counter, event: .event, host_name: .host_name, stdout: .stdout}' || true
echo "--- stdout (text) ---"
curl -sS -u "$AWX_USER:$AWX_PASS" "$AWX_HOST/api/v2/ad_hoc_commands/$ADHOC_ID/stdout/?format=txt" || true
echo "Done. If SSH auth still fails, ensure the private key has access to the target hosts (or use a key with passwordless access)."