gautam-y / Ansible

Content related to Ansible
0 stars 0 forks source link

Jt #9

Open gautam-y opened 3 months ago

gautam-y commented 3 months ago

#!/bin/bash

# Variables
TOWER_HOST="https://your-ansible-tower-url"
REPORT_FILE="job_templates_report.csv"
JT_NAMES_FILE="jtnames.txt"

# Initialize CSV file
echo "Name,ID,Org Name,Last Ran,Verbosity" > $REPORT_FILE

# Read job template names from the file
while IFS= read -r jtname; do
  # Fetch job template details by name
  template=$(awx job_template list --name "$jtname" | jq -c '.results[0]')

  if [ "$template" != "null" ]; then
    id=$(echo "$template" | jq '.id')
    name=$(echo "$template" | jq -r '.name')

    # Fetch job template details
    detail=$(awx job_template get "$id")

    org_name=$(echo "$detail" | jq -r '.summary_fields.organization.name // "N/A"')
    last_ran=$(echo "$detail" | jq -r '.last_job_run // "N/A"')
    verbosity=$(echo "$detail" | jq -r '.verbosity // "N/A"')

    # Append to CSV file
    echo "$name,$id,$org_name,$last_ran,$verbosity" >> $REPORT_FILE
  else
    echo "Job template $jtname not found."
  fi
done < "$JT_NAMES_FILE"

echo "Report generated: $REPORT_FILE"
gautam-y commented 3 months ago

#!/bin/bash

# Variables
TOWER_HOST="https://your-ansible-tower-url"
TOWER_USERNAME="your_username"
TOWER_PASSWORD="your_password"
JT_NAMES_FILE="jtnames.txt"
REPORT_FILE="job_templates_report.csv"

# Initialize CSV file
echo "Name,ID,Org Name,Last Ran,Verbosity" > $REPORT_FILE

# Function to URL encode a string
url_encode() {
    echo -n "$1" | jq -sRr @uri
}

# Read job template names from the file
while IFS= read -r jtname; do
    # URL encode the job template name
    ENCODED_JT_NAME=$(url_encode "$jtname")

    # Fetch job template details
    response=$(curl -s -X GET "${TOWER_HOST}/api/v2/job_templates/?name=${ENCODED_JT_NAME}" \
                      -u ${TOWER_USERNAME}:${TOWER_PASSWORD})

    # Extract job template details
    id=$(echo "$response" | jq -r '.results[0].id // "N/A"')
    name=$(echo "$response" | jq -r '.results[0].name // "N/A"')
    org_name=$(echo "$response" | jq -r '.results[0].summary_fields.organization.name // "N/A"')
    last_ran=$(echo "$response" | jq -r '.results[0].last_job_run // "N/A"')
    verbosity=$(echo "$response" | jq -r '.results[0].verbosity // "N/A"')

    # Append to CSV file
    echo "$name,$id,$org_name,$last_ran,$verbosity" >> $REPORT_FILE

done < "$JT_NAMES_FILE"

echo "Report generated: $REPORT_FILE"
gautam-y commented 2 months ago

#!/bin/bash

# Ansible Tower/ AWX API credentials
TOWER_HOST="https://your-tower-url"
TOWER_USER="your_username"
TOWER_PASSWORD="your_password"
OUTPUT_FILE="organizations_admins.csv"

# Function to get auth token
get_token() {
  curl -s -X POST "$TOWER_HOST/api/v2/tokens/" \
    -u "$TOWER_USER:$TOWER_PASSWORD" \
    -H "Content-Type: application/json" \
    -d '{}' | jq -r '.token'
}

# Fetch the token
TOKEN=$(get_token)

# Initialize CSV file
echo "Organization Name,Admin User Name,Admin Email" > $OUTPUT_FILE

# Function to fetch all pages of a paginated API endpoint
fetch_all_pages() {
  local url=$1
  local results=()
  while [[ $url != "null" ]]; do
    response=$(curl -s -H "Authorization: Bearer $TOKEN" "$url")
    results+=($(echo $response | jq -c '.results[]'))
    url=$(echo $response | jq -r '.next')
  done
  echo "${results[@]}"
}

# Fetch all organizations
ORGANIZATIONS=$(fetch_all_pages "$TOWER_HOST/api/v2/organizations/")

# Iterate over each organization
for ORG in "${ORGANIZATIONS[@]}"; do
    ORG_NAME=$(echo $ORG | jq -r '.name')
    ORG_ID=$(echo $ORG | jq -r '.id')

    # Fetch all admins for the organization
    ADMINS=$(fetch_all_pages "$TOWER_HOST/api/v2/organizations/$ORG_ID/admin_role/")

    for ADMIN in "${ADMINS[@]}"; do
        ADMIN_NAME=$(echo $ADMIN | jq -r '.username')
        ADMIN_EMAIL=$(echo $ADMIN | jq -r '.email')

        # Append to CSV
        echo "$ORG_NAME,$ADMIN_NAME,$ADMIN_EMAIL" >> $OUTPUT_FILE
    done
done

echo "Export completed: $OUTPUT_FILE"
gautam-y commented 2 months ago

#!/bin/bash

# Ansible Tower/ AWX API credentials and token
TOWER_HOST="https://your-tower-url"
API_TOKEN="your_existing_token"
OUTPUT_FILE="organizations_admins.csv"

# Initialize CSV file
echo "Organization Name,Admin User Name,Admin Email" > $OUTPUT_FILE

# Function to fetch all pages of a paginated API endpoint
fetch_all_pages() {
  local url=$1
  local results=()
  while [[ $url != "null" ]]; do
    response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$url")
    results+=($(echo $response | jq -c '.results[]'))
    url=$(echo $response | jq -r '.next')
  done
  echo "${results[@]}"
}

# Fetch all organizations
ORGANIZATIONS=$(fetch_all_pages "$TOWER_HOST/api/v2/organizations/")

# Iterate over each organization
for ORG in "${ORGANIZATIONS[@]}"; do
    ORG_NAME=$(echo $ORG | jq -r '.name')
    ORG_ID=$(echo $ORG | jq -r '.id')

    # Fetch all admins for the organization
    ADMINS=$(fetch_all_pages "$TOWER_HOST/api/v2/organizations/$ORG_ID/admins/")

    for ADMIN in "${ADMINS[@]}"; do
        ADMIN_NAME=$(echo $ADMIN | jq -r '.username')
        ADMIN_EMAIL=$(echo $ADMIN | jq -r '.email')

        # Append to CSV
        echo "$ORG_NAME,$ADMIN_NAME,$ADMIN_EMAIL" >> $OUTPUT_FILE
    done
done

echo "Export completed: $OUTPUT_FILE"
gautam-y commented 2 months ago

```bash
#!/bin/bash

# Variables
TOWER_HOST="https://your_ansible_tower_url"
TOWER_USERNAME="your_username"
TOWER_PASSWORD="your_password"
OUTPUT_FILE="organizations_admins.csv"

# Initialize CSV file with headers
echo "Organization Name,Admin Username,Admin Email" > "$OUTPUT_FILE"

# Function to fetch JSON data from Ansible Tower API
fetch_json() {
    local url=$1
    curl -s -u "$TOWER_USERNAME:$TOWER_PASSWORD" -H "Content-Type: application/json" "$url"
}

# Get all organizations
org_url="$TOWER_HOST/api/v2/organizations/"
while [ -n "$org_url" ]; do
    org_response=$(fetch_json "$org_url")
    org_url=$(echo "$org_response" | jq -r '.next')

    # Iterate through organizations
    echo "$org_response" | jq -c '.results[]' | while read -r org; do
        org_name=$(echo "$org" | jq -r '.name')
        org_id=$(echo "$org" | jq -r '.id')

        # Get all admin users for the organization
        admin_url="$TOWER_HOST/api/v2/organizations/$org_id/admin_role/users/"
        while [ -n "$admin_url" ]; do
            admin_response=$(fetch_json "$admin_url")
            admin_url=$(echo "$admin_response" | jq -r '.next')

            # Iterate through admin users
            echo "$admin_response" | jq -c '.results[]' | while read -r admin; do
                admin_username=$(echo "$admin" | jq -r '.username')
                admin_email=$(echo "$admin" | jq -r '.email')

                # Append to CSV
                echo "$org_name,$admin_username,$admin_email" >> "$OUTPUT_FILE"
            done
        done
    done
done

echo "CSV file has been created: $OUTPUT_FILE"
gautam-y commented 2 months ago

```bash
#!/bin/bash

# Variables
TOWER_HOST="https://your_ansible_tower_url"
TOWER_USERNAME="your_username"
TOWER_PASSWORD="your_password"
OUTPUT_FILE="organizations_admins.csv"

# Initialize CSV file with headers
echo "Organization Name,Admin Username,Admin Email" > "$OUTPUT_FILE"

# Function to fetch JSON data from Ansible Tower API
fetch_json() {
    local url=$1
    curl -s -u "$TOWER_USERNAME:$TOWER_PASSWORD" -H "Content-Type: application/json" "$url"
}

# Get all organizations
org_url="$TOWER_HOST/api/v2/organizations/"
while [ -n "$org_url" ]; do
    org_response=$(fetch_json "$org_url")
    org_url=$(echo "$org_response" | jq -r '.next')

    # Iterate through organizations
    echo "$org_response" | jq -c '.results[]' | while read -r org; do
        org_name=$(echo "$org" | jq -r '.name')
        org_id=$(echo "$org" | jq -r '.id')

        # Get all admin users for the organization
        admin_url="$TOWER_HOST/api/v2/organizations/$org_id/admin_role/users/"
        while [ -n "$admin_url" ]; do
            admin_response=$(fetch_json "$admin_url")
            admin_url=$(echo "$admin_response" | jq -r '.next')

            # Iterate through admin users
            echo "$admin_response" | jq -c '.results[]' | while read -r admin; do
                admin_username=$(echo "$admin" | jq -r '.username')
                admin_email=$(echo "$admin" | jq -r '.email')

                # Append to CSV
                echo "$org_name,$admin_username,$admin_email" >> "$OUTPUT_FILE"
            done
        done
    done
done

echo "CSV file has been created: $OUTPUT_FILE"
gautam-y commented 2 months ago

#!/bin/bash

# Set Ansible Tower API URL and the output CSV file name
TOWER_API_URL="https://your_ansible_tower_instance/api/v2"
OUTPUT_FILE="org_admins.csv"

# Set your Ansible Tower API token
API_TOKEN="your_ansible_tower_api_token"

# Prepare the CSV header
echo "Organization Name,Admin Username,Admin Email" > $OUTPUT_FILE

# Function to fetch paginated results
fetch_paginated_results() {
    local url=$1
    local results=()
    while [ -n "$url" ]; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$url")
        results+=($(echo "$response" | jq -c '.results[]'))
        url=$(echo "$response" | jq -r '.next')
    done
    echo "${results[@]}"
}

# Get all organizations
orgs=$(fetch_paginated_results "$TOWER_API_URL/organizations/")

# Iterate over each organization
for org in "${orgs[@]}"; do
  org_name=$(echo $org | jq -r '.name')
  org_id=$(echo $org | jq -r '.id')

  # Get all admins for the organization
  admins=$(fetch_paginated_results "$TOWER_API_URL/organizations/$org_id/admins/")

  # Iterate over each admin and append to the CSV file
  for admin in "${admins[@]}"; do
    username=$(echo $admin | jq -r '.username')
    email=$(echo $admin | jq -r '.email')

    echo "$org_name,$username,$email" >> $OUTPUT_FILE
  done
done

echo "Export completed. CSV file saved as $OUTPUT_FILE."
gautam-y commented 2 months ago

#!/bin/bash

# Set Ansible Tower API URL and the output CSV file name
TOWER_API_URL="https://your_ansible_tower_instance/api/v2"
OUTPUT_FILE="org_admins.csv"

# Set your Ansible Tower API token
API_TOKEN="your_ansible_tower_api_token"

# Prepare the CSV header
echo "Organization Name,Admin Username,Admin Email" > $OUTPUT_FILE

# Function to fetch paginated results
fetch_paginated_results() {
    local url=$1
    local results=()
    while [ -n "$url" ] && [ "$url" != "null" ]; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$url")
        results+=($(echo "$response" | jq -c '.results[]'))
        url=$(echo "$response" | jq -r '.next')
    done
    echo "${results[@]}"
}

# Get all organizations
orgs=$(fetch_paginated_results "$TOWER_API_URL/organizations/")

# Iterate over each organization
for org in "${orgs[@]}"; do
  org_name=$(echo $org | jq -r '.name')
  org_id=$(echo $org | jq -r '.id')

  # Get all admins for the organization, handling pagination
  admins=$(fetch_paginated_results "$TOWER_API_URL/organizations/$org_id/admins/")

  # Iterate over each admin and append to the CSV file
  for admin in "${admins[@]}"; do
    username=$(echo $admin | jq -r '.username')
    email=$(echo $admin | jq -r '.email')

    echo "$org_name,$username,$email" >> $OUTPUT_FILE
  done
done

echo "Export completed. CSV file saved as $OUTPUT_FILE."
gautam-y commented 2 months ago

#!/bin/bash

# Ansible Tower API URL and Authentication
TOWER_HOST="https://your-ansible-tower-host"
API_TOKEN="your_api_token"
OUTPUT_FILE="organization_admins.csv"

# Function to fetch all pages of a given API endpoint
fetch_all_pages() {
    local url=$1
    local result=""

    while [[ $url ]]; do
        response=$(curl -s -k -H "Authorization: Bearer $API_TOKEN" "$url")
        result+=$(echo "$response" | jq -r '.results | @json')
        url=$(echo "$response" | jq -r '.next')
    done

    echo "$result" | jq -s 'flatten'
}

# Fetch all organizations
echo "Fetching organizations..."
organizations=$(fetch_all_pages "$TOWER_HOST/api/v2/organizations/")

# Prepare CSV file with header
echo "organization_name,admin_username,admin_email" > "$OUTPUT_FILE"

# Loop through all organizations to get their admins
echo "Fetching admins for each organization..."
for org in $(echo "$organizations" | jq -c '.[]'); do
    org_name=$(echo "$org" | jq -r '.name')
    org_id=$(echo "$org" | jq -r '.id')

    # Fetch all admins for the current organization
    admins=$(fetch_all_pages "$TOWER_HOST/api/v2/organizations/$org_id/admins/")

    # Loop through all admins and write to CSV
    for admin in $(echo "$admins" | jq -c '.[]'); do
        admin_username=$(echo "$admin" | jq -r '.username')
        admin_email=$(echo "$admin" | jq -r '.email')

        # Append data to the CSV file
        echo "$org_name,$admin_username,$admin_email" >> "$OUTPUT_FILE"
    done
done

echo "Export complete! Data saved to $OUTPUT_FILE."
gautam-y commented 2 months ago

#!/bin/bash

# Ansible Tower API URL and Authentication
TOWER_HOST="https://your-ansible-tower-host"
API_TOKEN="your_api_token"
OUTPUT_FILE="organization_admins.csv"

# Function to fetch all pages of a given API endpoint
fetch_all_pages() {
    local url=$1
    local result=""

    while [[ $url ]]; do
        # Fetch the response
        response=$(curl -s -k -H "Authorization: Bearer $API_TOKEN" "$url")

        # Check for errors in the response
        if echo "$response" | jq -e . >/dev/null 2>&1; then
            result+=$(echo "$response" | jq -c '.results[]? | @json')
            url=$(echo "$response" | jq -r '.next')
        else
            echo "Error fetching data from $url"
            exit 1
        fi
    done

    echo "$result"
}

# Fetch all organizations
echo "Fetching organizations..."
organizations=$(fetch_all_pages "$TOWER_HOST/api/v2/organizations/")

# Check if organizations data is empty
if [ -z "$organizations" ]; then
    echo "No organizations found or error fetching organizations."
    exit 1
fi

# Prepare CSV file with header
echo "organization_name,admin_username,admin_email" > "$OUTPUT_FILE"

# Loop through all organizations to get their admins
echo "Fetching admins for each organization..."
for org in $(echo "$organizations" | jq -c '.'); do
    org_name=$(echo "$org" | jq -r '.name')
    org_id=$(echo "$org" | jq -r '.id')

    # Fetch all admins for the current organization
    admins=$(fetch_all_pages "$TOWER_HOST/api/v2/organizations/$org_id/admins/")

    # Check if admins data is empty
    if [ -z "$admins" ]; then
        echo "No admins found for organization $org_name."
        continue
    fi

    # Loop through all admins and write to CSV
    for admin in $(echo "$admins" | jq -c '.'); do
        admin_username=$(echo "$admin" | jq -r '.username')
        admin_email=$(echo "$admin" | jq -r '.email')

        # Append data to the CSV file
        echo "$org_name,$admin_username,$admin_email" >> "$OUTPUT_FILE"
    done
done

echo "Export complete! Data saved to $OUTPUT_FILE."
gautam-y commented 2 months ago

#!/bin/bash

# Ansible Tower API URL and Authentication
TOWER_HOST="https://your-ansible-tower-host"
API_TOKEN="your_api_token"
OUTPUT_FILE="organization_admins.csv"

# Function to fetch all pages of a given API endpoint
fetch_all_pages() {
    local url=$1
    local result=""

    while [[ $url ]]; do
        # Fetch the response
        response=$(curl -s -k -H "Authorization: Bearer $API_TOKEN" "$url")

        # Debugging: Print the raw response
        echo "Raw response: $response" >&2

        # Check for errors in the response
        if echo "$response" | jq . >/dev/null 2>&1; then
            result+=$(echo "$response" | jq -c '.results[]?')
            url=$(echo "$response" | jq -r '.next')
        else
            echo "Error parsing JSON response from $url"
            exit 1
        fi
    done

    echo "$result"
}

# Fetch all organizations
echo "Fetching organizations..."
organizations=$(fetch_all_pages "$TOWER_HOST/api/v2/organizations/")

# Check if organizations data is empty
if [ -z "$organizations" ]; then
    echo "No organizations found or error fetching organizations."
    exit 1
fi

# Prepare CSV file with header
echo "organization_name,admin_username,admin_email" > "$OUTPUT_FILE"

# Loop through all organizations to get their admins
echo "Fetching admins for each organization..."
for org in $(echo "$organizations" | jq -c '.'); do
    org_name=$(echo "$org" | jq -r '.name')
    org_id=$(echo "$org" | jq -r '.id')

    # Fetch all admins for the current organization
    admins=$(fetch_all_pages "$TOWER_HOST/api/v2/organizations/$org_id/admins/")

    # Check if admins data is empty
    if [ -z "$admins" ]; then
        echo "No admins found for organization $org_name."
        continue
    fi

    # Loop through all admins and write to CSV
    for admin in $(echo "$admins" | jq -c '.'); do
        admin_username=$(echo "$admin" | jq -r '.username')
        admin_email=$(echo "$admin" | jq -r '.email')

        # Append data to the CSV file
        echo "$org_name,$admin_username,$admin_email" >> "$OUTPUT_FILE"
    done
done

echo "Export complete! Data saved to $OUTPUT_FILE."
gautam-y commented 2 months ago

```bash
#!/bin/bash

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

# Output CSV file
OUTPUT_FILE="organization_admins.csv"

# Initialize CSV file with headers
echo "Organization Name,Admin Username,Admin Email" > "$OUTPUT_FILE"

# Function to fetch data from Ansible Tower API
fetch_data() {
    local url=$1
    curl -s -u "$USERNAME:$PASSWORD" -H "Content-Type: application/json" "$url"
}

# Fetch all organizations
org_url="$TOWER_URL/api/v2/organizations/"
while [ -n "$org_url" ]; do
    # Get JSON response for the current page of organizations
    org_response=$(fetch_data "$org_url")

    # Extract the URL for the next page of organizations
    org_url=$(echo "$org_response" | jq -r '.next')

    # Iterate over each organization
    echo "$org_response" | jq -c '.results[]' | while read -r org; do
        # Extract organization details
        org_name=$(echo "$org" | jq -r '.name')
        org_id=$(echo "$org" | jq -r '.id')

        # Fetch all admins for the current organization
        admin_url="$TOWER_URL/api/v2/organizations/$org_id/admin_role/users/"
        while [ -n "$admin_url" ]; do
            # Get JSON response for the current page of admins
            admin_response=$(fetch_data "$admin_url")

            # Extract the URL for the next page of admins
            admin_url=$(echo "$admin_response" | jq -r '.next')

            # Iterate over each admin
            echo "$admin_response" | jq -c '.results[]' | while read -r admin; do
                # Extract admin details
                admin_username=$(echo "$admin" | jq -r '.username')
                admin_email=$(echo "$admin" | jq -r '.email')

                # Append admin details to CSV file
                echo "$org_name,$admin_username,$admin_email" >> "$OUTPUT_FILE"
            done
        done
    done
done

echo "Export completed. Check the $OUTPUT_FILE file for results."
gautam-y commented 2 months ago

```bash
#!/bin/bash

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

# Output CSV file
OUTPUT_FILE="organization_admins.csv"

# Initialize CSV file with headers
echo "Organization Name,Admin Username,Admin Email" > "$OUTPUT_FILE"

# Function to fetch data from Ansible Tower API
fetch_data() {
    local url=$1
    curl -s -u "$USERNAME:$PASSWORD" -H "Content-Type: application/json" "$url"
}

# Fetch all organizations
org_url="$TOWER_URL/api/v2/organizations/"
while [ -n "$org_url" ]; do
    # Get JSON response for the current page of organizations
    org_response=$(fetch_data "$org_url")

    # Extract the URL for the next page of organizations
    org_url=$(echo "$org_response" | jq -r '.next')

    # Iterate over each organization
    echo "$org_response" | jq -c '.results[]' | while read -r org; do
        # Extract organization details
        org_name=$(echo "$org" | jq -r '.name')
        org_id=$(echo "$org" | jq -r '.id')

        # Fetch all admins for the current organization
        admin_url="$TOWER_URL/api/v2/organizations/$org_id/admin_role/users/"
        while [ -n "$admin_url" ]; do
            # Get JSON response for the current page of admins
            admin_response=$(fetch_data "$admin_url")

            # Extract the URL for the next page of admins
            admin_url=$(echo "$admin_response" | jq -r '.next')

            # Iterate over each admin
            echo "$admin_response" | jq -c '.results[]' | while read -r admin; do
                # Extract admin details
                admin_username=$(echo "$admin" | jq -r '.username')
                admin_email=$(echo "$admin" | jq -r '.email')

                # Append admin details to CSV file
                echo "$org_name,$admin_username,$admin_email" >> "$OUTPUT_FILE"
            done
        done
    done
done

echo "Export completed. Check the $OUTPUT_FILE file for results."
gautam-y commented 2 months ago

#!/bin/bash

# Ansible Tower URL and API token
TOWER_URL="https://url"
API_TOKEN="your_api_token"

# Output CSV file
OUTPUT_FILE="org_admins_report.csv"

# Write the header to the CSV file
echo "Org Name,Org ID,Admin Username,Admin Email" > $OUTPUT_FILE

# Function to fetch admins for a given organization
fetch_admins() {
    local org_id=$1
    local org_name=$2
    local page=1
    local has_more=true

    while $has_more; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" \
                        "$TOWER_URL/api/v2/organizations/$org_id/admins/?page=$page")

        # Extract admin usernames and emails
        admins=$(echo "$response" | jq -r '.results[] | "\(.username),\(.email)"')

        # Write admins to CSV file
        while IFS=',' read -r username email; do
            echo "$org_name,$org_id,$username,$email" >> $OUTPUT_FILE
        done <<< "$admins"

        # Check if there are more pages
        has_more=$(echo "$response" | jq -r '.next != null')
        page=$((page + 1))
    done
}

# Fetch the list of organizations
orgs=$(curl -s -H "Authorization: Bearer $API_TOKEN" \
              "$TOWER_URL/api/v2/organizations/" | jq -r '.results[] | "\(.id),\(.name)"')

# Iterate over each organization and fetch admins
while IFS=',' read -r org_id org_name; do
    echo "Fetching admins for Organization: $org_name (ID: $org_id)"
    fetch_admins $org_id "$org_name"
done <<< "$orgs"

echo "Report generated: $OUTPUT_FILE"
gautam-y commented 2 months ago

#!/bin/bash

# Ansible Tower URL and API token
TOWER_URL="https://url"
API_TOKEN="your_api_token"

# Output CSV file
OUTPUT_FILE="org_admins_report.csv"

# Write the header to the CSV file
echo "Org Name,Org ID,Admin Username,Admin Email" > $OUTPUT_FILE

# Function to fetch admins for a given organization
fetch_admins() {
    local org_id=$1
    local org_name=$2
    local page=1
    local has_more=true

    while $has_more; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" \
                        "$TOWER_URL/api/v2/organizations/$org_id/admins/?page=$page")

        # Extract admin usernames and emails
        admins=$(echo "$response" | jq -r '.results[] | "\(.username),\(.email)"')

        # Write admins to CSV file
        while IFS=',' read -r username email; do
            echo "$org_name,$org_id,$username,$email" >> $OUTPUT_FILE
        done <<< "$admins"

        # Check if there are more pages
        has_more=$(echo "$response" | jq -r '.next != null')
        page=$((page + 1))
    done
}

# Function to fetch all organizations
fetch_organizations() {
    local page=1
    local has_more=true

    while $has_more; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" \
                        "$TOWER_URL/api/v2/organizations/?page=$page")

        # Extract org ids and names
        orgs=$(echo "$response" | jq -r '.results[] | "\(.id),\(.name)"')

        # Iterate over each organization and fetch admins
        while IFS=',' read -r org_id org_name; do
            echo "Fetching admins for Organization: $org_name (ID: $org_id)"
            fetch_admins $org_id "$org_name"
        done <<< "$orgs"

        # Check if there are more pages
        has_more=$(echo "$response" | jq -r '.next != null')
        page=$((page + 1))
    done
}

# Fetch all organizations and their admins
fetch_organizations

echo "Report generated: $OUTPUT_FILE"
gautam-y commented 2 months ago

#!/bin/bash

# Ansible Tower URL and API token
TOWER_URL="https://url"
API_TOKEN="your_api_token"

# Output CSV file
OUTPUT_FILE="org_admins_report.csv"

# Write the header to the CSV file
echo "Org Name,Org ID,Admin Username,Admin Email" > $OUTPUT_FILE

# Function to fetch admins for a given organization
fetch_admins() {
    local org_id=$1
    local org_name=$2
    local page=1
    local has_more=true

    while $has_more; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" \
                        "$TOWER_URL/api/v2/organizations/$org_id/admins/?page=$page")

        # Extract admin usernames and emails
        admins=$(echo "$response" | jq -r '.results[] | "\(.username),\(.email)"')

        # Write admins to CSV file
        while IFS=',' read -r username email; do
            echo "$org_name,$org_id,$username,$email" >> $OUTPUT_FILE
        done <<< "$admins"

        # Check if there are more pages
        has_more=$(echo "$response" | jq -r '.next != null')
        if [ "$has_more" == "true" ]; then
            page=$((page + 1))
        else
            has_more=false
        fi
    done
}

# Function to fetch all organizations
fetch_organizations() {
    local page=1
    local has_more=true

    while $has_more; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" \
                        "$TOWER_URL/api/v2/organizations/?page=$page")

        # Extract org ids and names
        orgs=$(echo "$response" | jq -r '.results[] | "\(.id),\(.name)"')

        # Iterate over each organization and fetch admins
        while IFS=',' read -r org_id org_name; do
            echo "Fetching admins for Organization: $org_name (ID: $org_id)"
            fetch_admins $org_id "$org_name"
        done <<< "$orgs"

        # Check if there are more pages
        has_more=$(echo "$response" | jq -r '.next != null')
        if [ "$has_more" == "true" ]; then
            page=$((page + 1))
        else
            has_more=false
        fi
    done
}

# Fetch all organizations and their admins
fetch_organizations

echo "Report generated: $OUTPUT_FILE"
gautam-y commented 2 months ago

```bash
#!/bin/bash

# Function to get all pages of a paginated API endpoint
get_all_pages() {
  local url=$1
  local results=()
  while [ -n "$url" ]; do
    response=$(curl -s "$url")
    results+=($(echo "$response" | jq -r '.results[] | @base64'))
    url=$(echo "$response" | jq -r '.next')
  done
  echo "${results[@]}"
}

# Get all organizations
orgs=$(get_all_pages "https://url/api/v2/organizations/")

# Prepare CSV header
echo "Org Name,Org ID,Admin Username,Admin Email" > report.csv

# Iterate over organizations
for org in "${orgs[@]}"; do
  org=$(echo "$org" | base64 --decode)
  org_id=$(echo "$org" | jq -r '.id')
  org_name=$(echo "$org" | jq -r '.name')

  # Get all admins for the organization
  admins=$(get_all_pages "https://url/api/v2/organizations/$org_id/admins/")

  # Iterate over admins
  for admin in "${admins[@]}"; do
    admin=$(echo "$admin" | base64 --decode)
    admin_username=$(echo "$admin" | jq -r '.username')
    admin_email=$(echo "$admin" | jq -r '.email')

    # Append to CSV
    echo "$org_name,$org_id,$admin_username,$admin_email" >> report.csv
  done
done
gautam-y commented 2 months ago

```bash
#!/bin/bash

# Define your API token here
API_TOKEN="your_api_token_here"

# Function to get all pages of a paginated API endpoint
get_all_pages() {
  local url=$1
  local results=()
  while [ -n "$url" ]; do
    response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$url")
    results+=($(echo "$response" | jq -r '.results[] | @base64'))
    url=$(echo "$response" | jq -r '.next')
  done
  echo "${results[@]}"
}

# Get all organizations
orgs=$(get_all_pages "https://url/api/v2/organizations/")

# Prepare CSV header
echo "Org Name,Org ID,Admin Username,Admin Email" > report.csv

# Iterate over organizations
for org in "${orgs[@]}"; do
  org=$(echo "$org" | base64 --decode)
  org_id=$(echo "$org" | jq -r '.id')
  org_name=$(echo "$org" | jq -r '.name')

  # Get all admins for the organization
  admins=$(get_all_pages "https://url/api/v2/organizations/$org_id/admins/")

  # Iterate over admins
  for admin in "${admins[@]}"; do
    admin=$(echo "$admin" | base64 --decode)
    admin_username=$(echo "$admin" | jq -r '.username')
    admin_email=$(echo "$admin" | jq -r '.email')

    # Append to CSV
    echo "$org_name,$org_id,$admin_username,$admin_email" >> report.csv
  done
done
gautam-y commented 2 months ago

```bash
#!/bin/bash

# Define your API token here
API_TOKEN="your_api_token_here"

# Function to get all pages of a paginated API endpoint
get_all_pages() {
  local url=$1
  local results=()
  while [ -n "$url" ]; do
    response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$url")
    # Check if the response contains valid JSON
    if echo "$response" | jq . >/dev/null 2>&1; then
      results+=($(echo "$response" | jq -c '.results[]'))
      url=$(echo "$response" | jq -r '.next')
    else
      echo "Error: Invalid JSON response"
      exit 1
    fi
  done
  echo "${results[@]}"
}

# Get all organizations
orgs=$(get_all_pages "https://abc.net/api/v2/organizations/")

# Prepare CSV header
echo "Org Name,Org ID,Admin Username,Admin Email" > report.csv

# Iterate over organizations
for org in "${orgs[@]}"; do
  org_id=$(echo "$org" | jq -r '.id')
  org_name=$(echo "$org" | jq -r '.name')

  # Get all admins for the organization
  admins=$(get_all_pages "https://abc.net/api/v2/organizations/$org_id/admins/")

  # Iterate over admins
  for admin in "${admins[@]}"; do
    admin_username=$(echo "$admin" | jq -r '.username')
    admin_email=$(echo "$admin" | jq -r '.email')

    # Append to CSV
    echo "$org_name,$org_id,$admin_username,$admin_email" >> report.csv
  done
done
gautam-y commented 2 months ago

```python
import json
import csv

def extract_admin_details(json_file, csv_file):
    # Load JSON data from the file
    with open(json_file, 'r') as file:
        data = json.load(file)

    # Prepare the CSV file for writing
    with open(csv_file, 'w', newline='') as csvfile:
        fieldnames = ['First Name', 'Last Name', 'Email', 'Organization Name']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        # Write the header
        writer.writeheader()

        # Iterate over each organization in the JSON data
        for org in data.get('organizations', []):
            org_name = org.get('name', 'Unknown Organization')
            admins = org.get('admins', [])

            # Extract admin details
            for admin in admins:
                first_name = admin.get('first_name', 'Unknown')
                last_name = admin.get('last_name', 'Unknown')
                email = admin.get('email', 'Unknown')

                # Write the admin details to the CSV file
                writer.writerow({
                    'First Name': first_name,
                    'Last Name': last_name,
                    'Email': email,
                    'Organization Name': org_name
                })

if __name__ == '__main__':
    # Specify the input JSON file and output CSV file
    json_file = 'organizations.json'
    csv_file = 'organization_admins.csv'

    # Extract admin details and export to CSV
    extract_admin_details(json_file, csv_file)
gautam-y commented 1 month ago

#!/bin/bash

# Variables
API_URL="https://your-ansible-tower.com/api/v2/organizations/"
API_TOKEN="your_api_token_here"
OUTPUT_FILE="org_admins.csv"

# Initialize CSV file with headers
echo "Organization Name,Admin Username,Admin Email" > $OUTPUT_FILE

# Function to get admins from the admin URL
get_admins() {
    local org_name=$1
    local admin_url=$2
    local has_admins=false

    while [ -n "$admin_url" ]; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$admin_url")
        admin_url=$(echo "$response" | jq -r '.next')

        admins=$(echo "$response" | jq -c '.results[]')
        if [ -n "$admins" ]; then
            has_admins=true
            for admin in $admins; do
                admin_name=$(echo "$admin" | jq -r '.username')
                admin_email=$(echo "$admin" | jq -r '.email')
                echo "$org_name,$admin_name,$admin_email" >> $OUTPUT_FILE
            done
        fi
    done

    if [ "$has_admins" = false ]; then
        echo "$org_name,No Admins,No Admins" >> $OUTPUT_FILE
    fi
}

# Pagination through all organizations
while [ -n "$API_URL" ]; do
    response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$API_URL")
    API_URL=$(echo "$response" | jq -r '.next')

    organizations=$(echo "$response" | jq -c '.results[]')
    for org in $organizations; do
        org_name=$(echo "$org" | jq -r '.name')
        admin_url=$(echo "$org" | jq -r '.related.admins')
        get_admins "$org_name" "https://your-ansible-tower.com$admin_url"
    done
done

echo "Admin details exported to $OUTPUT_FILE"
gautam-y commented 1 month ago

#!/bin/bash

# Variables
BASE_URL="https://tower-url.net"
API_URL="$BASE_URL/api/v2/organizations/"
API_TOKEN="your_api_token_here"
OUTPUT_FILE="org_admins.csv"

# Initialize CSV file with headers
echo "Organization Name,Admin Username,Admin Email" > $OUTPUT_FILE

# Function to get admins from the admin URL
get_admins() {
    local org_name=$1
    local admin_url=$2
    local has_admins=false

    while [ -n "$admin_url" ]; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$admin_url")

        # Check if the response is empty or has invalid data
        if [[ $(echo "$response" | jq '.results | length') -eq 0 ]]; then
            break
        fi

        admin_url=$(echo "$response" | jq -r '.next')
        admins=$(echo "$response" | jq -c '.results[]')

        if [ -n "$admins" ]; then
            has_admins=true
            for admin in $admins; do
                admin_name=$(echo "$admin" | jq -r '.username')
                admin_email=$(echo "$admin" | jq -r '.email')
                echo "$org_name,$admin_name,$admin_email" >> $OUTPUT_FILE
            done
        fi
    done

    if [ "$has_admins" = false ]; then
        echo "$org_name,No Admins,No Admins" >> $OUTPUT_FILE
    fi
}

# Pagination through all organizations
while [ -n "$API_URL" ]; do
    response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$API_URL")
    API_URL=$(echo "$response" | jq -r '.next')

    organizations=$(echo "$response" | jq -c '.results[]')
    for org in $organizations; do
        org_name=$(echo "$org" | jq -r '.name')
        admin_url=$(echo "$org" | jq -r '.related.admins')
        get_admins "$org_name" "$BASE_URL$admin_url"
    done
done

echo "Admin details exported to $OUTPUT_FILE"
gautam-y commented 1 month ago

```bash
#!/bin/bash

# Define the base URL for the Ansible Tower API
BASE_URL="http://your-ansible-tower-url/api/v2"
# Add your authentication details here
USERNAME="your_username"
PASSWORD="your_password"

# Function to fetch data from a given URL
fetch_data() {
    local url="$1"
    curl -s -u "$USERNAME:$PASSWORD" "$url"
}

# Function to get all organizations
get_all_organizations() {
    local url="$BASE_URL/organizations/"
    local organizations=()

    while [ -n "$url" ]; do
        response=$(fetch_data "$url")
        organizations+=($(echo "$response" | jq -c '.results[]'))
        url=$(echo "$response" | jq -r '.next')
    done

    echo "${organizations[@]}"
}

# Function to get all admins for a given organization
get_admins() {
    local admins_url="$1"
    local admins=()

    while [ -n "$admins_url" ]; do
        response=$(fetch_data "$admins_url")
        admins+=($(echo "$response" | jq -c '.results[]'))
        admins_url=$(echo "$response" | jq -r '.next')
    done

    echo "${admins[@]}"
}

# Main function
main() {
    # Create CSV file and write header
    echo "Organization Name,Admin Name,Admin Email" > org_admins.csv

    organizations=$(get_all_organizations)

    for org in "${organizations[@]}"; do
        org_name=$(echo "$org" | jq -r '.name')
        admins_url="$BASE_URL$(echo "$org" | jq -r '.related.admins')"
        admins=$(get_admins "$admins_url")

        if [ -z "$admins" ]; then
            echo "$org_name,No Admins,No Admins" >> org_admins.csv
        else
            for admin in "${admins[@]}"; do
                admin_name=$(echo "$admin" | jq -r '.username')
                admin_email=$(echo "$admin" | jq -r '.email')
                echo "$org_name,$admin_name,$admin_email" >> org_admins.csv
            done
        fi
    done
}

# Run the main function
main
gautam-y commented 1 month ago

#!/bin/bash

# Variables
BASE_URL="https://tower-url.net"
API_URL="$BASE_URL/api/v2/organizations/"
API_TOKEN="your_api_token_here"
OUTPUT_FILE="org_admins.csv"

# Initialize CSV file with headers
echo "Organization Name,Admin Username,Admin Email" > $OUTPUT_FILE

# Function to get admins from the admin URL
get_admins() {
    local org_name=$1
    local admin_url=$2
    local has_admins=false

    while [ -n "$admin_url" ]; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$admin_url")

        # Check if the response is valid JSON
        if ! echo "$response" | jq . > /dev/null 2>&1; then
            echo "Invalid JSON response from $admin_url" >&2
            break
        fi

        # Extract the admin_url for the next page of results, if any
        admin_url=$(echo "$response" | jq -r '.next')

        # Get the list of admins
        admins=$(echo "$response" | jq -c '.results[]?')

        if [ -n "$admins" ]; then
            has_admins=true
            for admin in $admins; do
                admin_name=$(echo "$admin" | jq -r '.username // "No Username"')
                admin_email=$(echo "$admin" | jq -r '.email // "No Email"')
                echo "$org_name,$admin_name,$admin_email" >> $OUTPUT_FILE
            done
        fi
    done

    if [ "$has_admins" = false ]; then
        echo "$org_name,No Admins,No Admins" >> $OUTPUT_FILE
    fi
}

# Pagination through all organizations
while [ -n "$API_URL" ]; do
    response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$API_URL")

    # Check if the response is valid JSON
    if ! echo "$response" | jq . > /dev/null 2>&1; then
        echo "Invalid JSON response from $API_URL" >&2
        break
    fi

    # Extract the URL for the next page of results, if any
    API_URL=$(echo "$response" | jq -r '.next')

    # Get the list of organizations
    organizations=$(echo "$response" | jq -c '.results[]?')

    for org in $organizations; do
        org_name=$(echo "$org" | jq -r '.name // "No Organization Name"')
        admin_url=$(echo "$org" | jq -r '.related.admins // empty')

        # If admin_url is not empty, process the admins
        if [ -n "$admin_url" ]; then
            get_admins "$org_name" "$BASE_URL$admin_url"
        else
            echo "$org_name,No Admins,No Admins" >> $OUTPUT_FILE
        fi
    done
done

echo "Admin details exported to $OUTPUT_FILE"
gautam-y commented 1 month ago

```bash
#!/bin/bash

# Variables
API_URL="https://your-ansible-tower.com/api/v2/organizations/"
API_TOKEN="your_api_token_here"
OUTPUT_FILE="org_admins.csv"

# Initialize CSV file with headers
echo "Organization Name,Admin Username,Admin Email" > "$OUTPUT_FILE"

# Function to get admins from the admin URL
get_admins() {
    local org_name="$1"
    local admin_url="$2"

    while [ -n "$admin_url" ]; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$admin_url")
        admin_url=$(echo "$response" | jq -r '.next')

        admins=$(echo "$response" | jq -c '.results[]')
        for admin in $admins; do
            admin_name=$(echo "$admin" | jq -r '.username')
            admin_email=$(echo "$admin" | jq -r '.email')
            echo "$org_name,$admin_name,$admin_email" >> "$OUTPUT_FILE"
        done
    done
}

# Pagination through all organizations
while [ -n "$API_URL" ]; do
    response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$API_URL")
    API_URL=$(echo "$response" | jq -r '.next')

    organizations=$(echo "$response" | jq -c '.results[]')
    for org in $organizations; do
        org_name=$(echo "$org" | jq -r '.name')
        admin_url=$(echo "$org" | jq -r '.related.admins')
        get_admins "$org_name" "https://your-ansible-tower.com$admin_url"
    done
done

echo "Admin details exported to $OUTPUT_FILE"
gautam-y commented 1 month ago

```bash
#!/bin/bash

# Variables
API_URL="https://your-ansible-tower.com/api/v2/organizations/"
API_TOKEN="your_api_token_here"
OUTPUT_FILE="org_admins.csv"

# Initialize CSV file with headers
echo "Organization Name,Admin Username,Admin Email" > "$OUTPUT_FILE"

# Function to get admins from the admin URL
get_admins() {
    local org_name="$1"
    local admin_url="$2"

    while [ -n "$admin_url" ]; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$admin_url")

        # Check if response is valid JSON
        if ! echo "$response" | jq empty 2>/dev/null; then
            echo "Error: Invalid JSON response from $admin_url"
            echo "Response: $response"
            return
        fi

        admin_url=$(echo "$response" | jq -r '.next')

        admins=$(echo "$response" | jq -c '.results[]')
        for admin in $admins; do
            admin_name=$(echo "$admin" | jq -r '.username')
            admin_email=$(echo "$admin" | jq -r '.email')
            echo "$org_name,$admin_name,$admin_email" >> "$OUTPUT_FILE"
        done
    done
}

# Pagination through all organizations
while [ -n "$API_URL" ]; do
    response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$API_URL")

    # Check if response is valid JSON
    if ! echo "$response" | jq empty 2>/dev/null; then
        echo "Error: Invalid JSON response from $API_URL"
        echo "Response: $response"
        break
    fi

    API_URL=$(echo "$response" | jq -r '.next')

    organizations=$(echo "$response" | jq -c '.results[]')
    for org in $organizations; do
        org_name=$(echo "$org" | jq -r '.name')
        admin_url=$(echo "$org" | jq -r '.related.admins')
        get_admins "$org_name" "https://your-ansible-tower.com$admin_url"
    done
done

echo "Admin details exported to $OUTPUT_FILE"
gautam-y commented 1 month ago

#!/bin/bash

# Variables
BASE_URL="https://tower-url.net"
API_URL="$BASE_URL/api/v2/organizations/"
API_TOKEN="your_api_token_here"
OUTPUT_FILE="org_admins.csv"

# Initialize CSV file with headers
echo "Organization Name,Admin Username,Admin Email" > $OUTPUT_FILE

# Function to get admins from the admin URL
get_admins() {
    local org_name=$1
    local admin_url=$2
    local has_admins=false

    while [ -n "$admin_url" ]; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$admin_url")

        # Check if the response is valid JSON
        if ! echo "$response" | jq . > /dev/null 2>&1; then
            echo "Invalid JSON response from $admin_url" >&2
            echo "Response was: $response" >&2  # Log the response for debugging
            break
        fi

        # Extract the admin_url for the next page of results, if any
        admin_url=$(echo "$response" | jq -r '.next')

        # Get the list of admins
        admins=$(echo "$response" | jq -c '.results[]?')

        if [ -n "$admins" ]; then
            has_admins=true
            for admin in $admins; do
                admin_name=$(echo "$admin" | jq -r '.username // "No Username"')
                admin_email=$(echo "$admin" | jq -r '.email // "No Email"')
                echo "$org_name,$admin_name,$admin_email" >> $OUTPUT_FILE
            done
        fi
    done

    if [ "$has_admins" = false ]; then
        echo "$org_name,No Admins,No Admins" >> $OUTPUT_FILE
    fi
}

# Pagination through all organizations
while [ -n "$API_URL" ]; do
    response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$API_URL")

    # Check if the response is valid JSON
    if ! echo "$response" | jq . > /dev/null 2>&1; then
        echo "Invalid JSON response from $API_URL" >&2
        echo "Response was: $response" >&2  # Log the response for debugging
        break
    fi

    # Extract the URL for the next page of results, if any
    API_URL=$(echo "$response" | jq -r '.next')

    # Get the list of organizations
    organizations=$(echo "$response" | jq -c '.results[]?')

    for org in $organizations; do
        org_name=$(echo "$org" | jq -r '.name // "No Organization Name"')
        admin_url=$(echo "$org" | jq -r '.related.admins // empty')

        # If admin_url is not empty, process the admins
        if [ -n "$admin_url" ]; then
            get_admins "$org_name" "$BASE_URL$admin_url"
        else
            echo "$org_name,No Admins,No Admins" >> $OUTPUT_FILE
        fi
    done
done

echo "Admin details exported to $OUTPUT_FILE"
gautam-y commented 1 month ago

#!/bin/bash

BASE_URL="https://tower-url.net"
API_URL="$BASE_URL/api/v2/organizations/"
API_TOKEN="your_api_token_here"
OUTPUT_FILE="org_admins.csv"

echo "Organization Name,Admin Username,Admin Email" > $OUTPUT_FILE

get_admins() {
    local org_name=$1
    local admin_url=$2
    local has_admins=false

    while [ -n "$admin_url" ]; do
        response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$admin_url")
        echo "Response length: ${#response}" >&2  # Log the response length

        if ! echo "$response" | jq . > /dev/null 2>&1; then
            echo "Invalid JSON response from $admin_url" >&2
            echo "Response was: $response" >&2
            echo "$response" > error_response.json  # Save to file for inspection
            break
        fi

        admin_url=$(echo "$response" | jq -r '.next')
        admins=$(echo "$response" | jq -c '.results[]?')

        if [ -n "$admins" ]; then
            has_admins=true
            for admin in $admins; do
                admin_name=$(echo "$admin" | jq -r '.username // "No Username"')
                admin_email=$(echo "$admin" | jq -r '.email // "No Email"')
                echo "$org_name,$admin_name,$admin_email" >> $OUTPUT_FILE
            done
        fi
    done

    if [ "$has_admins" = false ]; then
        echo "$org_name,No Admins,No Admins" >> $OUTPUT_FILE
    fi
}

while [ -n "$API_URL" ]; do
    response=$(curl -s -H "Authorization: Bearer $API_TOKEN" "$API_URL")
    echo "Response length: ${#response}" >&2  # Log the response length

    if ! echo "$response" | jq . > /dev/null 2>&1; then
        echo "Invalid JSON response from $API_URL" >&2
        echo "Response was: $response" >&2
        echo "$response" > error_response.json  # Save to file for inspection
        break
    fi

    API_URL=$(echo "$response" | jq -r '.next')
    organizations=$(echo "$response" | jq -c '.results[]?')

    for org in $organizations; do
        org_name=$(echo "$org" | jq -r '.name // "No Organization Name"')
        admin_url=$(echo "$org" | jq -r '.related.admins // empty')

        if [ -n "$admin_url" ]; then
            get_admins "$org_name" "$BASE_URL$admin_url"
        else
            echo "$org_name,No Admins,No Admins" >> $OUTPUT_FILE
        fi
    done
done

echo "Admin details exported to $OUTPUT_FILE"
gautam-y commented 1 month ago

#!/bin/bash

# Set your Ansible Tower details
TOWER_URL="https://your_ansible_tower_url"
AUTH_HEADER="Authorization: Bearer your_api_token"

# Output CSV file
OUTPUT_FILE="ansible_tower_users.csv"

# Write CSV header
echo "Username,Email,Admin Of Organizations" > "$OUTPUT_FILE"

# Function to get all users
get_users() {
    local page=1
    local users=()

    while true; do
        response=$(curl -s -H "$AUTH_HEADER" "$TOWER_URL/api/v2/users/?page=$page")
        users_page=$(echo "$response" | jq -r '.results[] | @base64')
        next=$(echo "$response" | jq -r '.next')

        for user in $users_page; do
            users+=("$user")
        done

        if [[ "$next" == "null" ]]; then
            break
        fi

        page=$((page + 1))
    done

    echo "${users[@]}"
}

# Function to get user's admin organizations
get_admin_organizations() {
    local user_id=$1
    local page=1
    local orgs=()

    while true; do
        response=$(curl -s -H "$AUTH_HEADER" "$TOWER_URL/api/v2/users/$user_id/admin_of_organizations/?page=$page")
        orgs_page=$(echo "$response" | jq -r '.results[] | .name')
        next=$(echo "$response" | jq -r '.next')

        for org in $orgs_page; do
            orgs+=("$org")
        done

        if [[ "$next" == "null" ]]; then
            break
        fi

        page=$((page + 1))
    done

    if [ ${#orgs[@]} -eq 0 ]; then
        echo "not admin"
    else
        echo "${orgs[*]}"
    fi
}

# Main script execution
users=$(get_users)

for user in $users; do
    _jq() {
        echo ${user} | base64 --decode | jq -r ${1}
    }

    username=$(_jq '.username')
    email=$(_jq '.email')
    user_id=$(_jq '.id')

    admin_orgs=$(get_admin_organizations "$user_id")

    echo "$username,$email,$admin_orgs" >> "$OUTPUT_FILE"
done

echo "CSV export completed: $OUTPUT_FILE"
gautam-y commented 1 month ago

#!/bin/bash

# Set your Ansible Tower details
TOWER_URL="https://your_ansible_tower_url"
AUTH_HEADER="Authorization: Bearer your_api_token"

# Output CSV file
OUTPUT_FILE="ansible_tower_users.csv"

# Write CSV header
echo "Username,Email,Admin Of Organizations" > "$OUTPUT_FILE"

# Function to get all users
get_users() {
    local page=1
    local users=()

    while true; do
        response=$(curl -s -H "$AUTH_HEADER" "$TOWER_URL/api/v2/users/?page=$page")
        users_page=$(echo "$response" | jq -r '.results[] | @base64')
        next=$(echo "$response" | jq -r '.next')

        for user in $users_page; do
            users+=("$user")
        done

        if [[ "$next" == "null" ]]; then
            break
        fi

        page=$((page + 1))
    done

    echo "${users[@]}"
}

# Function to get user's admin organizations
get_admin_organizations() {
    local user_id=$1
    local page=1
    local orgs=()

    while true; do
        response=$(curl -s -H "$AUTH_HEADER" "$TOWER_URL/api/v2/users/$user_id/admin_of_organizations/?page=$page")
        orgs_page=$(echo "$response" | jq -r '.results[] | .name')
        next=$(echo "$response" | jq -r '.next')

        for org in $orgs_page; do
            orgs+=("$org")
        done

        if [[ "$next" == "null" ]]; then
            break
        fi

        page=$((page + 1))
    done

    if [ ${#orgs[@]} -eq 0 ]; then
        echo "not admin"
    else
        echo "${orgs[*]}" | tr ' ' '\n'
    fi
}

# Main script execution
users=$(get_users)

for user in $users; do
    _jq() {
        echo ${user} | base64 --decode | jq -r ${1}
    }

    username=$(_jq '.username')
    email=$(_jq '.email')
    user_id=$(_jq '.id')

    admin_orgs=$(get_admin_organizations "$user_id")

    echo -e "$username,$email,\"$admin_orgs\"" >> "$OUTPUT_FILE"
done

echo "CSV export completed: $OUTPUT_FILE"