gautam-y / Ansible

Content related to Ansible
0 stars 0 forks source link

Github #5

Open gautam-y opened 3 months ago

gautam-y commented 3 months ago

gautam-y commented 3 months ago
---
- hosts: all
  become: yes
  vars:
    repo_url: "https://github.com/your-username/your-repo.git"
    repo_dir: "/path/to/repo"
    proxy_url: "http://proxy.example.com:8080"
    proxy_env:
      http_proxy: "{{ proxy_url }}"
      https_proxy: "{{ proxy_url }}"

  tasks:

    - name: Install required packages
      apt:
        name:
          - git
        state: present
        update_cache: yes

    - name: Clone repository
      git:
        repo: "{{ repo_url }}"
        dest: "{{ repo_dir }}"
        force: yes
      environment: "{{ proxy_env }}"

    - name: Create a new file
      copy:
        content: "This is a test file created by Ansible"
        dest: "{{ repo_dir }}/test.txt"

    - name: Add new file to Git
      command: git add test.txt
      args:
        chdir: "{{ repo_dir }}"

    - name: Commit changes
      command: git commit -m "Added test file"
      args:
        chdir: "{{ repo_dir }}"

    - name: Push changes to remote
      command: git push
      args:
        chdir: "{{ repo_dir }}"
      environment: "{{ proxy_env }}"
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"

'''

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"
gautam-y commented 3 months ago

#!/bin/bash

# Input and output file names
input_file="teamid.txt"
output_file="teams_info.csv"

# Write the header to the CSV file
echo "Team Name,Team ID,Organization Name,Object Name,Object Role" > "$output_file"

# Function to fetch details for a given team ID
fetch_team_details() {
    local team_id=$1

    # Example command or API call to fetch team details
    # Replace this with the actual command or API call
    # Here, it's assumed that the command or API returns a JSON object with required fields
    # For demonstration, using placeholder JSON output
    team_details=$(cat <<EOF
{
    "team_name": "Team $team_id",
    "team_id": "$team_id",
    "organization_name": "Org $team_id",
    "object_name": "Object $team_id",
    "object_role": "Role $team_id"
}
EOF
)

    # Parse the JSON output to extract fields
    team_name=$(echo "$team_details" | jq -r '.team_name')
    team_id=$(echo "$team_details" | jq -r '.team_id')
    organization_name=$(echo "$team_details" | jq -r '.organization_name')
    object_name=$(echo "$team_details" | jq -r '.object_name')
    object_role=$(echo "$team_details" | jq -r '.object_role')

    # Write the details to the CSV file
    echo "$team_name,$team_id,$organization_name,$object_name,$object_role" >> "$output_file"
}

# Read each line from the input file and fetch details
while IFS= read -r team_id; do
    fetch_team_details "$team_id"
done < "$input_file"
gautam-y commented 3 months ago

#!/bin/bash

# Define the Ansible Tower API base URL and the authentication token
TOWER_API_URL="https://your-ansible-tower.example.com/api/v2"
AUTH_TOKEN="your_auth_token"

# Output CSV file
OUTPUT_FILE="teams_info.csv"

# Write CSV header
echo "Team Name,Team ID,Organization Name,Object Name,Object Role" > $OUTPUT_FILE

# Function to get organization name by organization id
get_organization_name() {
  ORG_ID=$1
  ORG_NAME=$(curl -s -H "Authorization: Bearer $AUTH_TOKEN" "$TOWER_API_URL/organizations/$ORG_ID/" | jq -r '.name')
  echo $ORG_NAME
}

# Read team IDs from the file
while IFS= read -r TEAM_ID; do
  # Fetch team details
  TEAM_INFO=$(curl -s -H "Authorization: Bearer $AUTH_TOKEN" "$TOWER_API_URL/teams/$TEAM_ID/")

  # Extract relevant fields
  TEAM_NAME=$(echo $TEAM_INFO | jq -r '.name')
  ORG_ID=$(echo $TEAM_INFO | jq -r '.organization')
  ORG_NAME=$(get_organization_name $ORG_ID)

  # Fetch roles for the team
  ROLES_INFO=$(curl -s -H "Authorization: Bearer $AUTH_TOKEN" "$TOWER_API_URL/teams/$TEAM_ID/roles/")

  # Loop through roles and extract object name and role
  echo $ROLES_INFO | jq -c '.results[]' | while IFS= read -r ROLE; do
    OBJECT_NAME=$(echo $ROLE | jq -r '.resource_name')
    OBJECT_ROLE=$(echo $ROLE | jq -r '.name')
    # Append to CSV file
    echo "$TEAM_NAME,$TEAM_ID,$ORG_NAME,$OBJECT_NAME,$OBJECT_ROLE" >> $OUTPUT_FILE
  done
done < teamid.txt

echo "CSV file $OUTPUT_FILE generated successfully."
gautam-y commented 3 months ago

#!/bin/bash

# Configuration
TOWER_HOST="https://your-ansible-tower-host.com"
AUTH_HEADER="Authorization: Bearer YOUR_API_TOKEN" # or "Authorization: Basic $(echo -n username:password | base64)"
INPUT_FILE="teamid.txt"
OUTPUT_FILE="team_roles.csv"

# Initialize the CSV file with headers
echo "Team Name,Team ID,Organization Name,Resource Name,Resource Type,Role Name" > $OUTPUT_FILE

# Function to fetch team details
get_team_details() {
    local team_id=$1

    # Fetch team details
    team_details=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/")
    team_name=$(echo $team_details | jq -r '.name')
    organization_id=$(echo $team_details | jq -r '.organization')

    # Fetch organization details
    org_details=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/organizations/$organization_id/")
    organization_name=$(echo $org_details | jq -r '.name')

    # Fetch team roles
    roles=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/roles/")

    # Parse and write roles to CSV
    echo $roles | jq -c '.results[]' | while read -r role; do
        resource_name=$(echo $role | jq -r '.summary_fields.resource_name')
        resource_type=$(echo $role | jq -r '.summary_fields.resource_type')
        role_name=$(echo $role | jq -r '.name')
        echo "$team_name,$team_id,$organization_name,$resource_name,$resource_type,$role_name" >> $OUTPUT_FILE
    done
}

# Read team IDs from the input file and fetch details for each
while IFS= read -r team_id; do
    get_team_details "$team_id"
done < "$INPUT_FILE"

echo "Data successfully written to $OUTPUT_FILE"
gautam-y commented 3 months ago

#!/bin/bash

# Configuration
TOWER_HOST="https://your-ansible-tower-host.com"
AUTH_HEADER="Authorization: Bearer YOUR_API_TOKEN" # or "Authorization: Basic $(echo -n username:password | base64)"
INPUT_FILE="teamid.txt"
OUTPUT_FILE="team_roles.csv"

# Initialize the CSV file with headers
echo "Team Name,Team ID,Organization Name,Resource Name,Resource Type,Role Name" > $OUTPUT_FILE

# Function to fetch team details
get_team_details() {
    local team_id=$1

    # Fetch team details
    team_details=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/")
    team_name=$(echo $team_details | jq -r '.name')
    organization_id=$(echo $team_details | jq -r '.organization')

    # Fetch organization details
    org_details=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/organizations/$organization_id/")
    organization_name=$(echo $org_details | jq -r '.name')

    # Fetch team roles
    roles=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/roles/")

    # Check if roles list is empty
    roles_count=$(echo $roles | jq '.count')
    if [ "$roles_count" -eq 0 ]; then
        echo "$team_name,$team_id,$organization_name,N/A,N/A,No Roles" >> $OUTPUT_FILE
    else
        # Parse and write roles to CSV
        echo $roles | jq -c '.results[]' | while read -r role; do
            resource_name=$(echo $role | jq -r '.summary_fields.resource_name')
            resource_type=$(echo $role | jq -r '.summary_fields.resource_type')
            role_name=$(echo $role | jq -r '.name')
            echo "$team_name,$team_id,$organization_name,$resource_name,$resource_type,$role_name" >> $OUTPUT_FILE
        done
    fi
}

# Read team IDs from the input file and fetch details for each
while IFS= read -r team_id; do
    get_team_details "$team_id"
done < "$INPUT_FILE"

echo "Data successfully written to $OUTPUT_FILE"
gautam-y commented 3 months ago

#!/bin/bash

# Configuration
TOWER_HOST="https://your-ansible-tower-host.com"
AUTH_HEADER="Authorization: Bearer YOUR_API_TOKEN" # or "Authorization: Basic $(echo -n username:password | base64)"
INPUT_FILE="teamid.txt"
OUTPUT_FILE="team_roles.csv"

# Initialize the CSV file with headers
echo "Team Name,Team ID,Organization Name,Resource Name,Resource Type,Role Name" > $OUTPUT_FILE

# Function to fetch team details
get_team_details() {
    local team_id=$1

    # Fetch team details
    team_details=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/")
    team_name=$(echo $team_details | jq -r '.name')
    organization_id=$(echo $team_details | jq -r '.organization')

    # Fetch organization details
    org_details=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/organizations/$organization_id/")
    organization_name=$(echo $org_details | jq -r '.name')

    # Fetch team roles
    roles=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/roles/")
    roles_count=$(echo $roles | jq -r '.count')

    # Check if roles list is empty
    if [ "$roles_count" -eq 0 ]; then
        echo "$team_name,$team_id,$organization_name,N/A,N/A,No Roles" >> $OUTPUT_FILE
    else
        # Parse and write roles to CSV
        echo $roles | jq -c '.results[]' | while read -r role; do
            resource_name=$(echo $role | jq -r '.summary_fields.resource_name')
            resource_type=$(echo $role | jq -r '.summary_fields.resource_type')
            role_name=$(echo $role | jq -r '.name')
            echo "$team_name,$team_id,$organization_name,$resource_name,$resource_type,$role_name" >> $OUTPUT_FILE
        done
    fi
}

# Read team IDs from the input file and fetch details for each
while IFS= read -r team_id; do
    get_team_details "$team_id"
done < "$INPUT_FILE"

echo "Data successfully written to $OUTPUT_FILE"
gautam-y commented 3 months ago

#!/bin/bash

# Configuration
TOWER_HOST="https://your-ansible-tower-host.com"
AUTH_HEADER="Authorization: Bearer YOUR_API_TOKEN" # or "Authorization: Basic $(echo -n username:password | base64)"
INPUT_FILE="teamid.txt"
OUTPUT_FILE="team_roles.csv"

# Initialize the CSV file with headers
echo "Team Name,Team ID,Organization Name,Resource Name,Resource Type,Role Name" > $OUTPUT_FILE

# Function to fetch team details
get_team_details() {
    local team_id=$1

    # Fetch team details
    team_details=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/")
    if [ $? -ne 0 ]; then
        echo "Error fetching details for team ID $team_id" >&2
        return
    fi

    team_name=$(echo "$team_details" | jq -r '.name')
    organization_id=$(echo "$team_details" | jq -r '.organization')

    if [ "$team_name" == "null" ] || [ "$organization_id" == "null" ]; then
        echo "Invalid team details for team ID $team_id" >&2
        return
    fi

    # Fetch organization details
    org_details=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/organizations/$organization_id/")
    organization_name=$(echo "$org_details" | jq -r '.name')

    if [ "$organization_name" == "null" ]; then
        echo "Invalid organization details for organization ID $organization_id" >&2
        return
    fi

    # Fetch team roles
    roles=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/roles/")
    roles_count=$(echo "$roles" | jq -r '.count')

    if [ "$roles_count" == "null" ] || ! [[ "$roles_count" =~ ^[0-9]+$ ]]; then
        echo "Invalid roles details for team ID $team_id" >&2
        return
    fi

    # Check if roles list is empty
    if [ "$roles_count" -eq 0 ]; then
        echo "$team_name,$team_id,$organization_name,N/A,N/A,No Roles" >> $OUTPUT_FILE
    else
        # Parse and write roles to CSV
        echo "$roles" | jq -c '.results[]' | while read -r role; do
            resource_name=$(echo "$role" | jq -r '.summary_fields.resource_name')
            resource_type=$(echo "$role" | jq -r '.summary_fields.resource_type')
            role_name=$(echo "$role" | jq -r '.name')

            if [ "$resource_name" == "null" ] || [ "$resource_type" == "null" ] || [ "$role_name" == "null" ]; then
                echo "Invalid role details for team ID $team_id" >&2
                continue
            fi

            echo "$team_name,$team_id,$organization_name,$resource_name,$resource_type,$role_name" >> $OUTPUT_FILE
        done
    fi
}

# Read team IDs from the input file and fetch details for each
while IFS= read -r team_id; do
    get_team_details "$team_id"
done < "$INPUT_FILE"

echo "Data successfully written to $OUTPUT_FILE"
gautam-y commented 3 months ago

#!/bin/bash

# Check if the input file is provided
if [ "$#" -ne 1 ]; then
  echo "Usage: $0 input.json"
  exit 1
fi

# Input JSON file
input_file="$1"

# Check if the input file exists
if [ ! -f "$input_file" ]; then
  echo "File $input_file not found!"
  exit 1
fi

# Convert JSON to CSV using jq
jq -r 'to_entries | map(.key as $k | .value | {organization, users, remove} | . + {name: $k}) | (first | keys_unsorted | @csv), (.[] | [.name, .organization, .users[0], .remove] | @csv)' "$input_file" > output.csv

echo "CSV file created as output.csv"
gautam-y commented 3 months ago

#!/bin/bash

# Configuration
TOWER_HOST="https://your-ansible-tower-host.com"
AUTH_HEADER="Authorization: Bearer YOUR_API_TOKEN" # or "Authorization: Basic $(echo -n username:password | base64)"
INPUT_FILE="teamid.txt"
OUTPUT_FILE="team_roles.csv"

# Initialize the CSV file with headers
echo "Team Name,Team ID,Organization Name,Resource Name,Resource Type,Role Name" > $OUTPUT_FILE

# Function to fetch team details
get_team_details() {
    local team_id=$1

    # Fetch team details
    echo "Fetching details for team ID: $team_id" >&2
    team_details=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/")
    if [ $? -ne 0 ]; then
        echo "Error fetching details for team ID $team_id" >&2
        return
    fi

    team_name=$(echo "$team_details" | jq -r '.name')
    organization_id=$(echo "$team_details" | jq -r '.organization')

    if [ "$team_name" == "null" ] || [ "$organization_id" == "null" ]; then
        echo "Invalid team details for team ID $team_id" >&2
        return
    fi

    echo "Team Name: $team_name, Organization ID: $organization_id" >&2

    # Fetch organization details
    org_details=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/organizations/$organization_id/")
    organization_name=$(echo "$org_details" | jq -r '.name')

    if [ "$organization_name" == "null" ]; then
        echo "Invalid organization details for organization ID $organization_id" >&2
        return
    fi

    echo "Organization Name: $organization_name" >&2

    # Fetch team roles
    roles=$(curl -s -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/roles/")
    roles_count=$(echo "$roles" | jq -r '.count')

    echo "Roles Count for Team ID $team_id: $roles_count" >&2

    # Check if roles_count is a valid integer
    if ! [[ "$roles_count" =~ ^[0-9]+$ ]]; then
        echo "Invalid roles details for team ID $team_id: roles_count is not a number" >&2
        return
    fi

    # Check if roles list is empty
    if [ "$roles_count" -eq 0 ]; then
        echo "$team_name,$team_id,$organization_name,N/A,N/A,No Roles" >> $OUTPUT_FILE
    else
        # Parse and write roles to CSV
        echo "$roles" | jq -c '.results[]' | while read -r role; do
            resource_name=$(echo "$role" | jq -r '.summary_fields.resource_name')
            resource_type=$(echo "$role" | jq -r '.summary_fields.resource_type')
            role_name=$(echo "$role" | jq -r '.name')

            if [ "$resource_name" == "null" ] || [ "$resource_type" == "null" ] || [ "$role_name" == "null" ]; then
                echo "Invalid role details for team ID $team_id" >&2
                continue
            fi

            echo "$team_name,$team_id,$organization_name,$resource_name,$resource_type,$role_name" >> $OUTPUT_FILE
        done
    fi
}

# Read team IDs from the input file and fetch details for each
while IFS= read -r team_id; do
    get_team_details "$team_id"
done < "$INPUT_FILE"

echo "Data successfully written to $OUTPUT_FILE"
gautam-y commented 3 months ago

#!/bin/bash

# Configuration
TOWER_HOST="https://your-ansible-tower-host.com"
AUTH_HEADER="Authorization: Bearer YOUR_API_TOKEN" # or "Authorization: Basic $(echo -n username:password | base64)"
INPUT_FILE="teamid.txt"
OUTPUT_FILE="team_roles.csv"

# Initialize the CSV file with headers
echo "Team Name,Team ID,Organization Name,Resource Name,Resource Type,Role Name" > $OUTPUT_FILE

# Function to fetch team details
get_team_details() {
    local team_id=$1

    # Fetch team details
    echo "Fetching details for team ID: $team_id" >&2
    team_details=$(curl -s -w "%{http_code}" -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/")
    http_code="${team_details: -3}"
    team_details="${team_details%$http_code}"

    if [ "$http_code" -ne 200 ]; then
        echo "Error fetching details for team ID $team_id, HTTP code: $http_code, Response: $team_details" >&2
        return
    fi

    team_name=$(echo "$team_details" | jq -r '.name')
    organization_id=$(echo "$team_details" | jq -r '.organization')

    if [ "$team_name" == "null" ] || [ "$organization_id" == "null" ]; then
        echo "Invalid team details for team ID $team_id" >&2
        return
    fi

    echo "Team Name: $team_name, Organization ID: $organization_id" >&2

    # Fetch organization details
    org_details=$(curl -s -w "%{http_code}" -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/organizations/$organization_id/")
    http_code="${org_details: -3}"
    org_details="${org_details%$http_code}"

    if [ "$http_code" -ne 200 ]; then
        echo "Error fetching details for organization ID $organization_id, HTTP code: $http_code, Response: $org_details" >&2
        return
    fi

    organization_name=$(echo "$org_details" | jq -r '.name')

    if [ "$organization_name" == "null" ]; then
        echo "Invalid organization details for organization ID $organization_id" >&2
        return
    fi

    echo "Organization Name: $organization_name" >&2

    # Fetch team roles
    roles=$(curl -s -w "%{http_code}" -H "$AUTH_HEADER" "$TOWER_HOST/api/v2/teams/$team_id/roles/")
    http_code="${roles: -3}"
    roles="${roles%$http_code}"

    if [ "$http_code" -ne 200 ]; then
        echo "Error fetching roles for team ID $team_id, HTTP code: $http_code, Response: $roles" >&2
        return
    fi

    roles_count=$(echo "$roles" | jq -r '.count')

    echo "Roles Count for Team ID $team_id: $roles_count" >&2

    # Check if roles_count is a valid integer
    if ! [[ "$roles_count" =~ ^[0-9]+$ ]]; then
        echo "Invalid roles details for team ID $team_id: roles_count is not a number" >&2
        return
    fi

    # Check if roles list is empty
    if [ "$roles_count" -eq 0 ]; then
        echo "$team_name,$team_id,$organization_name,N/A,N/A,No Roles" >> $OUTPUT_FILE
    else
        # Parse and write roles to CSV
        echo "$roles" | jq -c '.results[]' | while read -r role; do
            resource_name=$(echo "$role" | jq -r '.summary_fields.resource_name')
            resource_type=$(echo "$role" | jq -r '.summary_fields.resource_type')
            role_name=$(echo "$role" | jq -r '.name')

            if [ "$resource_name" == "null" ] || [ "$resource_type" == "null" ] || [ "$role_name" == "null" ]; then
                echo "Invalid role details for team ID $team_id" >&2
                continue
            fi

            echo "$team_name,$team_id,$organization_name,$resource_name,$resource_type,$role_name" >> $OUTPUT_FILE
        done
    fi
}

# Read team IDs from the input file and fetch details for each
while IFS= read -r team_id; do
    get_team_details "$team_id"
done < "$INPUT_FILE"

echo "Data successfully written to $OUTPUT_FILE"
gautam-y commented 3 months ago

#!/bin/bash

# Configuration
INPUT_FILE="teamid.txt"
OUTPUT_FILE="team_roles.csv"

# Initialize the CSV file with headers
echo "Team Name,Team ID,Organization Name,Resource Name,Resource Type,Role Name" > $OUTPUT_FILE

# Function to fetch team details
get_team_details() {
    local team_id=$1

    # Fetch team details
    echo "Fetching details for team ID: $team_id" >&2
    team_details=$(awx teams get "$team_id" --format json)

    if [ $? -ne 0 ]; then
        echo "Error fetching details for team ID $team_id" >&2
        return
    fi

    team_name=$(echo "$team_details" | jq -r '.name')
    organization_id=$(echo "$team_details" | jq -r '.organization')

    if [ "$team_name" == "null" ] || [ "$organization_id" == "null" ]; then
        echo "Invalid team details for team ID $team_id" >&2
        return
    fi

    echo "Team Name: $team_name, Organization ID: $organization_id" >&2

    # Fetch organization details
    org_details=$(awx organizations get "$organization_id" --format json)

    if [ $? -ne 0 ]; then
        echo "Error fetching details for organization ID $organization_id" >&2
        return
    fi

    organization_name=$(echo "$org_details" | jq -r '.name')

    if [ "$organization_name" == "null" ]; then
        echo "Invalid organization details for organization ID $organization_id" >&2
        return
    fi

    echo "Organization Name: $organization_name" >&2

    # Fetch team roles
    roles=$(awx teams roles "$team_id" --format json)

    if [ $? -ne 0 ]; then
        echo "Error fetching roles for team ID $team_id" >&2
        return
    fi

    roles_count=$(echo "$roles" | jq -r '.results | length')

    echo "Roles Count for Team ID $team_id: $roles_count" >&2

    # Check if roles list is empty
    if [ "$roles_count" -eq 0 ]; then
        echo "$team_name,$team_id,$organization_name,N/A,N/A,No Roles" >> $OUTPUT_FILE
    else
        # Parse and write roles to CSV
        echo "$roles" | jq -c '.results[]' | while read -r role; do
            resource_name=$(echo "$role" | jq -r '.summary_fields.resource_name')
            resource_type=$(echo "$role" | jq -r '.summary_fields.resource_type')
            role_name=$(echo "$role" | jq -r '.name')

            if [ "$resource_name" == "null" ] || [ "$resource_type" == "null" ] || [ "$role_name" == "null" ]; then
                echo "Invalid role details for team ID $team_id" >&2
                continue
            fi

            echo "$team_name,$team_id,$organization_name,$resource_name,$resource_type,$role_name" >> $OUTPUT_FILE
        done
    fi
}

# Read team IDs from the input file and fetch details for each
while IFS= read -r team_id; do
    get_team_details "$team_id"
done < "$INPUT_FILE"

echo "Data successfully written to $OUTPUT_FILE"
gautam-y commented 3 months ago

#!/bin/bash

# Configuration
INPUT_FILE="teamid.txt"
OUTPUT_FILE="team_roles.csv"
TOWER_URL="https://your-ansible-tower-instance"
TOWER_TOKEN="your_api_token"

# Initialize the CSV file with headers
echo "Team Name,Team ID,Organization Name,Resource Name,Resource Type,Role Name" > $OUTPUT_FILE

# Function to fetch team details
get_team_details() {
    local team_id=$1

    # Fetch team details
    echo "Fetching details for team ID: $team_id" >&2
    team_details=$(curl -s -H "Authorization: Bearer $TOWER_TOKEN" "$TOWER_URL/api/v2/teams/$team_id/")

    if [ $? -ne 0 ]; then
        echo "Error fetching details for team ID $team_id" >&2
        return
    fi

    team_name=$(echo "$team_details" | jq -r '.name')
    organization_id=$(echo "$team_details" | jq -r '.organization')

    if [ "$team_name" == "null" ] || [ "$organization_id" == "null" ]; then
        echo "Invalid team details for team ID $team_id" >&2
        return
    fi

    echo "Team Name: $team_name, Organization ID: $organization_id" >&2

    # Fetch organization details
    org_details=$(curl -s -H "Authorization: Bearer $TOWER_TOKEN" "$TOWER_URL/api/v2/organizations/$organization_id/")

    if [ $? -ne 0 ]; then
        echo "Error fetching details for organization ID $organization_id" >&2
        return
    fi

    organization_name=$(echo "$org_details" | jq -r '.name')

    if [ "$organization_name" == "null" ]; then
        echo "Invalid organization details for organization ID $organization_id" >&2
        return
    fi

    echo "Organization Name: $organization_name" >&2

    # Fetch team roles
    roles=$(curl -s -H "Authorization: Bearer $TOWER_TOKEN" "$TOWER_URL/api/v2/teams/$team_id/roles/")

    if [ $? -ne 0 ]; then
        echo "Error fetching roles for team ID $team_id" >&2
        return
    fi

    roles_count=$(echo "$roles" | jq -r '.results | length')

    echo "Roles Count for Team ID $team_id: $roles_count" >&2

    # Check if roles list is empty
    if [ "$roles_count" -eq 0 ]; then
        echo "$team_name,$team_id,$organization_name,N/A,N/A,No Roles" >> $OUTPUT_FILE
    else
        # Parse and write roles to CSV
        echo "$roles" | jq -c '.results[]' | while read -r role; do
            resource_name=$(echo "$role" | jq -r '.summary_fields.resource_name')
            resource_type=$(echo "$role" | jq -r '.summary_fields.resource_type')
            role_name=$(echo "$role" | jq -r '.name')

            if [ "$resource_name" == "null" ] || [ "$resource_type" == "null" ] || [ "$role_name" == "null" ]; then
                echo "Invalid role details for team ID $team_id" >&2
                continue
            fi

            echo "$team_name,$team_id,$organization_name,$resource_name,$resource_type,$role_name" >> $OUTPUT_FILE
        done
    fi
}

# Read team IDs from the input file and fetch details for each
while IFS= read -r team_id; do
    # Check if the line is not empty
    if [ -n "$team_id" ]; then
        get_team_details "$team_id"
    fi
done < "$INPUT_FILE"

echo "Data successfully written to $OUTPUT_FILE"