gautam-y / Ansible

Content related to Ansible
0 stars 0 forks source link

Org #4

Open gautam-y opened 3 months ago

gautam-y commented 3 months ago

!/bin/bash

Configuration

TOWER_URL="https://YOUR_TOWER_URL/api/v2" USERNAME="YOUR_USERNAME" PASSWORD="YOUR_PASSWORD" OUTPUT_FILE="organizations.csv"

Function to make authenticated API requests

get_api_response() { local url=$1 curl -s -u "$USERNAME:$PASSWORD" "$url" }

Fetch organizations

response=$(get_api_response "$TOWER_URL/organizations/")

Parse JSON response and extract organization details

echo "Name,ID,Instance Group Name" > "$OUTPUT_FILE" echo "$response" | jq -r '.results[] | [.name, .id] | @csv' >> "$OUTPUT_FILE"

Fetch instance groups for each organization

organizations=$(echo "$response" | jq -r '.results[] | [.id] | @csv')

while IFS=',' read -r org_id do

Fetch instance groups for the organization

instance_group_response=$(get_api_response "$TOWER_URL/organizations/$org_id/instance_groups/")

# Parse instance group response and extract names
instance_group_names=$(echo "$instance_group_response" | jq -r '.results[].name' | paste -sd "," -)

# Find the organization name based on the org_id
org_name=$(echo "$response" | jq -r ".results[] | select(.id == $org_id) | .name")

# Append instance group names to the CSV file
echo "$org_name,$org_id,$instance_group_names" >> "$OUTPUT_FILE"

done <<< "$organizations"

echo "Organizations and instance group details have been exported to $OUTPUT_FILE"

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

# Set the API URL and authentication credentials
API_URL="https://url/api/v2"
AUTH_TOKEN="your_auth_token"

# Set the input file containing organization IDs
INPUT_FILE="org_ids.txt"

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

# Create the output file and write the header row
echo "Organization ID,Instance Group Name" > "$OUTPUT_FILE"

# Loop through each organization ID in the input file
while read -r ORG_ID; do
    # Make the API call to retrieve the instance group name
    RESPONSE=$(curl -s -X GET -H "Authorization: Bearer $AUTH_TOKEN" "$API_URL/organizations/$ORG_ID/instance_groups/")

    # Extract the instance group name from the API response
    INSTANCE_GROUP_NAME=$(echo "$RESPONSE" | grep -o '"name":"[^"]*' | cut -d':' -f2 | tr -d '"')

    # Write the organization ID and instance group name to the output file
    echo "$ORG_ID,$INSTANCE_GROUP_NAME" >> "$OUTPUT_FILE"
done < "$INPUT_FILE"

echo "Instance group names exported to $OUTPUT_FILE"
gautam-y commented 3 months ago
#!/bin/bash

# Set the API URL and authentication credentials
API_URL="https://url/api/v2"
AUTH_TOKEN="your_auth_token"

# Set the input file containing organization IDs
INPUT_FILE="org_ids.txt"

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

# Create the output file and write the header row
echo "Organization ID,Instance Group Name" > "$OUTPUT_FILE"

# Loop through each organization ID in the input file
while read -r ORG_ID; do
    # Make the API call to retrieve the instance group name
    RESPONSE=$(curl -s -X GET -H "Authorization: Bearer $AUTH_TOKEN" "$API_URL/organizations/$ORG_ID/instance_groups/")

    # Extract the instance group name from the API response
    INSTANCE_GROUP_NAME=$(echo "$RESPONSE" | grep -o '"name":"[^"]*' | cut -d':' -f2 | tr -d '"')

    # Check if an instance group name was found
    if [ -z "$INSTANCE_GROUP_NAME" ]; then
        INSTANCE_GROUP_NAME="No Instance Group"
    fi

    # Write the organization ID and instance group name to the output file
    echo "$ORG_ID,$INSTANCE_GROUP_NAME" >> "$OUTPUT_FILE"
done < "$INPUT_FILE"

echo "Instance group names exported to $OUTPUT_FILE"
gautam-y commented 3 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"