Open gautam-y opened 2 months ago
TOWER_URL="https://your-ansible-tower-url.com/api/v2" AUTH_HEADER="Authorization: Bearer YOUR_ACCESS_TOKEN"
OUTPUT_FILE="team_details.csv"
echo "team_name,team_id,organization_name,object_role,object_name" > $OUTPUT_FILE
get_organization_name() { ORG_ID=$1 ORG_NAME=$(curl -s -H "$AUTH_HEADER" "${TOWER_URL}/organizations/${ORG_ID}/" | jq -r '.name') echo $ORG_NAME }
while read -r TEAM_ID; do if [[ ! -z "$TEAM_ID" ]]; then echo "Fetching details for team ID: $TEAM_ID"
# Fetch team details
TEAM_RESPONSE=$(curl -s -H "$AUTH_HEADER" "${TOWER_URL}/teams/${TEAM_ID}/")
TEAM_NAME=$(echo $TEAM_RESPONSE | jq -r '.name')
ORG_ID=$(echo $TEAM_RESPONSE | jq -r '.organization')
ORG_NAME=$(get_organization_name $ORG_ID)
# Fetch roles for the given team ID
ROLES_RESPONSE=$(curl -s -H "$AUTH_HEADER" "${TOWER_URL}/teams/$TEAM_ID/roles/")
# Check if the response contains results
if [[ $(echo $ROLES_RESPONSE | jq '.results | length') -gt 0 ]]; then
# Extract role details and append to CSV file
echo $ROLES_RESPONSE | jq -r --arg TEAM_NAME "$TEAM_NAME" --arg TEAM_ID "$TEAM_ID" --arg ORG_NAME "$ORG_NAME" \
'.results[] | [$TEAM_NAME, $TEAM_ID, $ORG_NAME, .name, .summary_fields.content_object.name] | @csv' >> $OUTPUT_FILE
else
echo "No roles found for team ID: $TEAM_ID"
fi
fi
done < teamid.txt
echo "Team details have been exported to $OUTPUT_FILE"