gautam-y / Ansible

Content related to Ansible
0 stars 0 forks source link

New #6

Open gautam-y opened 2 months ago

gautam-y commented 2 months ago
#!/bin/bash

# Set the Ansible Tower URL and credentials
TOWER_URL="https://your-ansible-tower-url"
TOWER_USERNAME="your-username"
TOWER_PASSWORD="your-password"

# Set the output CSV file
OUTPUT_FILE="team_roles.csv"

# Read the team IDs from the team.txt file
readarray -t TEAM_IDS < team.txt

# Create the CSV header
echo "Team Name,Role Name,Role Description" > "$OUTPUT_FILE"

# Loop through each team ID
for team_id in "${TEAM_IDS[@]}"; do
    # Get the team details
    team_data=$(curl -X GET -u "$TOWER_USERNAME:$TOWER_PASSWORD" "$TOWER_URL/api/v2/teams/$team_id/" -s)
    team_name=$(echo "$team_data" | jq -r '.name')

    # Get the roles for the team
    roles_data=$(curl -X GET -u "$TOWER_USERNAME:$TOWER_PASSWORD" "$TOWER_URL/api/v2/teams/$team_id/roles/" -s)

    # Loop through each role and append to the CSV file
    echo "$roles_data" | jq -r '.results[] | [.name, .description] | @csv' | while IFS="," read -r role_name role_description; do
        echo "$team_name,$role_name,$role_description" >> "$OUTPUT_FILE"
    done
done
gautam-y commented 2 months ago

!/bin/bash

Set the Ansible Tower URL and authentication details

TOWER_URL="https://your-ansible-tower-url.com/api/v2" AUTH_HEADER="Authorization: Bearer YOUR_ACCESS_TOKEN"

Output CSV file

OUTPUT_FILE="team_details.csv"

Initialize the CSV file with headers

echo "team_name,team_id,organization_name,object_role,object_name" > $OUTPUT_FILE

Function to fetch organization name by ID

get_organization_name() { ORG_ID=$1 ORG_NAME=$(curl -s -H "$AUTH_HEADER" "${TOWER_URL}/organizations/${ORG_ID}/" | jq -r '.name') echo $ORG_NAME }

Read each team ID from teamid.txt and fetch team details

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"