gautam-y / Ansible

Content related to Ansible
0 stars 0 forks source link

Api #7

Open gautam-y opened 6 months ago

gautam-y commented 6 months ago

#!/bin/bash

# Replace with your Ansible API URL
API_URL="https://your.ansible.instance/api/v2/projects"
# Replace with your authentication token
AUTH_TOKEN="your_auth_token"

# File to store the CSV output
OUTPUT_FILE="ansible_projects.csv"

# Function to fetch data from the API
fetch_data() {
    local url=$1
    curl -s -H "Authorization: Bearer $AUTH_TOKEN" -H "Content-Type: application/json" "$url"
}

# Initialize the CSV file with headers
echo "Project Name,Project ID" > $OUTPUT_FILE

# Initial API call to get the first page of results
response=$(fetch_data "$API_URL")
next_url=$(echo $response | jq -r '.next')
results=$(echo $response | jq -r '.results[] | [.name, .id] | @csv')

# Append the first page of results to the CSV file
echo "$results" >> $OUTPUT_FILE

# Loop to get subsequent pages if 'next' is not null
while [ "$next_url" != "null" ]; do
    response=$(fetch_data "$next_url")
    next_url=$(echo $response | jq -r '.next')
    results=$(echo $response | jq -r '.results[] | [.name, .id] | @csv')
    echo "$results" >> $OUTPUT_FILE
done

echo "Data has been exported to $OUTPUT_FILE"
gautam-y commented 6 months ago

#!/bin/bash

# Configuration
TOWER_HOST="https://your_ansible_tower_url"
AUTH_TOKEN="your_auth_token"  # Alternatively, use username and password for basic auth

# Function to get data from Ansible Tower API
get_data() {
  local endpoint="$1"
  curl -s -H "Authorization: Bearer ${AUTH_TOKEN}" "${TOWER_HOST}/api/v2/${endpoint}/"
}

# Get job templates
job_templates=$(get_data "job_templates")

# Initialize the CSV file
csv_file="job_templates.csv"
echo "Template Name,Organization Name,Verbosity,Project Name,Project ID,Project SCM URL" > "$csv_file"

# Process each job template
echo "$job_templates" | jq -c '.results[]' | while read -r template; do
  template_name=$(echo "$template" | jq -r '.name')
  verbosity=$(echo "$template" | jq -r '.verbosity')

  # Get project details
  project_url=$(echo "$template" | jq -r '.related.project')
  project=$(curl -s -H "Authorization: Bearer ${AUTH_TOKEN}" "$project_url")
  project_name=$(echo "$project" | jq -r '.name')
  project_id=$(echo "$project" | jq -r '.id')
  project_scm_url=$(echo "$project" | jq -r '.scm_url')

  # Get organization details
  organization_url=$(echo "$template" | jq -r '.related.organization')
  organization=$(curl -s -H "Authorization: Bearer ${AUTH_TOKEN}" "$organization_url")
  organization_name=$(echo "$organization" | jq -r '.name')

  # Append to CSV file
  echo "$template_name,$organization_name,$verbosity,$project_name,$project_id,$project_scm_url" >> "$csv_file"
done

echo "Export completed: $csv_file"
gautam-y commented 6 months ago

```bash
#!/bin/bash

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

# Set the output file name
OUTPUT_FILE="job_templates.csv"

# Get an authentication token
TOKEN=$(curl -X POST -k "$TOWER_URL/api/v2/tokens/" \
  -H "Content-Type: application/json" \
  -d "{\"description\": \"Export Job Templates\", \"scope\": \"write\"}" \
  --user "$TOWER_USERNAME:$TOWER_PASSWORD" | grep -oP '(?<="token": ")[^"]*')

# Get job templates
JOB_TEMPLATES=$(curl -X GET -k "$TOWER_URL/api/v2/job_templates/" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json")

# Print the header row
echo "Job Template Name,Organization Name,Verbosity,Project Name,Project ID,Project SCM URL" > "$OUTPUT_FILE"

# Loop through job templates and extract the required information
for JOB_TEMPLATE in $(echo "$JOB_TEMPLATES" | grep -oP '{"id":[0-9]+,.*?}'); do
  JOB_TEMPLATE_NAME=$(echo "$JOB_TEMPLATE" | grep -oP '(?<="name": ")[^"]*')
  ORGANIZATION_NAME=$(echo "$JOB_TEMPLATE" | grep -oP '(?<="organization_name": ")[^"]*')
  VERBOSITY=$(echo "$JOB_TEMPLATE" | grep -oP '(?<="verbosity": )[^,]*')
  PROJECT_ID=$(echo "$JOB_TEMPLATE" | grep -oP '(?<="project": )[0-9]+')
  PROJECT_NAME=$(curl -X GET -k "$TOWER_URL/api/v2/projects/$PROJECT_ID/" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" | grep -oP '(?<="name": ")[^"]*')
  PROJECT_SCM_URL=$(curl -X GET -k "$TOWER_URL/api/v2/projects/$PROJECT_ID/" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" | grep -oP '(?<="scm_url": ")[^"]*')

  echo "$JOB_TEMPLATE_NAME,$ORGANIZATION_NAME,$VERBOSITY,$PROJECT_NAME,$PROJECT_ID,$PROJECT_SCM_URL" >> "$OUTPUT_FILE"
done

echo "Job templates exported to $OUTPUT_FILE"
gautam-y commented 6 months ago

#!/bin/bash

# Configuration
TOWER_URL="https://your-ansible-tower.com"
TOWER_USERNAME="your_username"
TOWER_PASSWORD="your_password"
OUTPUT_FILE="job_templates.csv"

# Function to fetch job templates
fetch_job_templates() {
  curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" \
    -H "Content-Type: application/json" \
    "${TOWER_URL}/api/v2/job_templates/" | jq '.results'
}

# Function to fetch project details
fetch_project_details() {
  local project_id=$1
  curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" \
    -H "Content-Type: application/json" \
    "${TOWER_URL}/api/v2/projects/${project_id}/"
}

# Initialize CSV file with headers
echo "Job Template Name,Organization Name,Verbosity,Project Name,Project ID,Project SCM URL" > "${OUTPUT_FILE}"

# Fetch job templates and iterate through them
job_templates=$(fetch_job_templates)
for row in $(echo "${job_templates}" | jq -r '.[] | @base64'); do
  _jq() {
    echo "${row}" | base64 --decode | jq -r "${1}"
  }

  job_template_name=$(_jq '.name')
  organization_name=$(_jq '.summary_fields.organization.name')
  verbosity=$(_jq '.verbosity')
  project_id=$(_jq '.project')

  # Fetch project details for each job template
  project_details=$(fetch_project_details "${project_id}")
  project_name=$(echo "${project_details}" | jq -r '.name')
  project_scm_url=$(echo "${project_details}" | jq -r '.scm_url')

  # Append the details to the CSV file
  echo "${job_template_name},${organization_name},${verbosity},${project_name},${project_id},${project_scm_url}" >> "${OUTPUT_FILE}"
done

echo "Job templates have been exported to ${OUTPUT_FILE}"
gautam-y commented 6 months ago

#!/bin/bash

# Configuration
TOWER_URL="https://your-ansible-tower.com"
TOWER_USERNAME="your_username"
TOWER_PASSWORD="your_password"
OUTPUT_FILE="job_templates.csv"

# Function to fetch job templates with pagination
fetch_all_job_templates() {
  local url="${TOWER_URL}/api/v2/job_templates/"
  local results=()

  while [ -n "$url" ]; do
    response=$(curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" -H "Content-Type: application/json" "$url")
    results+=($(echo "$response" | jq '.results | .[] | @base64'))
    url=$(echo "$response" | jq -r '.next')
  done

  echo "${results[@]}"
}

# Function to fetch project details
fetch_project_details() {
  local project_id=$1
  curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" -H "Content-Type: application/json" "${TOWER_URL}/api/v2/projects/${project_id}/"
}

# Initialize CSV file with headers
echo "Job Template Name,Organization Name,Verbosity,Project Name,Project ID,Project SCM URL" > "${OUTPUT_FILE}"

# Fetch all job templates and iterate through them
job_templates=$(fetch_all_job_templates)
for row in "${job_templates[@]}"; do
  _jq() {
    echo "${row}" | base64 --decode | jq -r "${1}"
  }

  job_template_name=$(_jq '.name')
  organization_name=$(_jq '.summary_fields.organization.name')
  verbosity=$(_jq '.verbosity')
  project_id=$(_jq '.project')

  # Fetch project details for each job template
  project_details=$(fetch_project_details "${project_id}")
  project_name=$(echo "${project_details}" | jq -r '.name')
  project_scm_url=$(echo "${project_details}" | jq -r '.scm_url')

  # Append the details to the CSV file
  echo "${job_template_name},${organization_name},${verbosity},${project_name},${project_id},${project_scm_url}" >> "${OUTPUT_FILE}"
done

echo "Job templates have been exported to ${OUTPUT_FILE}"
gautam-y commented 6 months ago

#!/bin/bash

# Configuration
TOWER_URL="https://your-ansible-tower.com"
TOWER_USERNAME="your_username"
TOWER_PASSWORD="your_password"
OUTPUT_FILE="job_templates.csv"

# Function to fetch job templates with pagination
fetch_all_job_templates() {
  local url="${TOWER_URL}/api/v2/job_templates/"
  local results=()

  while [ -n "$url" ] && [ "$url" != "null" ]; do
    response=$(curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" -H "Content-Type: application/json" "$url")
    results+=($(echo "$response" | jq -r '.results[] | @base64'))
    url=$(echo "$response" | jq -r '.next')
  done

  echo "${results[@]}"
}

# Function to fetch project details
fetch_project_details() {
  local project_id=$1
  curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" -H "Content-Type: application/json" "${TOWER_URL}/api/v2/projects/${project_id}/"
}

# Initialize CSV file with headers
echo "Job Template Name,Organization Name,Verbosity,Project Name,Project ID,Project SCM URL" > "${OUTPUT_FILE}"

# Fetch all job templates and iterate through them
job_templates=$(fetch_all_job_templates)
for row in ${job_templates[@]}; do
  _jq() {
    echo "${row}" | base64 --decode | jq -r "${1}"
  }

  job_template_name=$(_jq '.name')
  organization_name=$(_jq '.summary_fields.organization.name')
  verbosity=$(_jq '.verbosity')
  project_id=$(_jq '.project')

  # Fetch project details for each job template
  project_details=$(fetch_project_details "${project_id}")
  project_name=$(echo "${project_details}" | jq -r '.name')
  project_scm_url=$(echo "${project_details}" | jq -r '.scm_url')

  # Append the details to the CSV file
  echo "${job_template_name},${organization_name},${verbosity},${project_name},${project_id},${project_scm_url}" >> "${OUTPUT_FILE}"
done

echo "Job templates have been exported to ${OUTPUT_FILE}"
gautam-y commented 6 months ago

#!/bin/bash

# Configuration
TOWER_URL="https://your-ansible-tower.com"
TOWER_USERNAME="your_username"
TOWER_PASSWORD="your_password"
OUTPUT_FILE="job_templates.csv"

# Function to fetch job templates with pagination
fetch_all_job_templates() {
  local url="${TOWER_URL}/api/v2/job_templates/"
  local results=()

  while [ -n "$url" ] && [ "$url" != "null" ]; do
    response=$(curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" -H "Content-Type: application/json" "$url")
    # Check if results are not null
    if [ "$(echo "$response" | jq -r '.results | length')" -gt 0 ]; then
      results+=($(echo "$response" | jq -r '.results[] | @base64'))
    fi
    url=$(echo "$response" | jq -r '.next')
  done

  echo "${results[@]}"
}

# Function to fetch project details
fetch_project_details() {
  local project_id=$1
  curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" -H "Content-Type: application/json" "${TOWER_URL}/api/v2/projects/${project_id}/"
}

# Initialize CSV file with headers
echo "Job Template Name,Organization Name,Verbosity,Project Name,Project ID,Project SCM URL" > "${OUTPUT_FILE}"

# Fetch all job templates and iterate through them
job_templates=$(fetch_all_job_templates)
for row in ${job_templates[@]}; do
  _jq() {
    echo "${row}" | base64 --decode | jq -r "${1}"
  }

  job_template_name=$(_jq '.name')
  organization_name=$(_jq '.summary_fields.organization.name')
  verbosity=$(_jq '.verbosity')
  project_id=$(_jq '.project')

  # Fetch project details for each job template
  project_details=$(fetch_project_details "${project_id}")
  project_name=$(echo "${project_details}" | jq -r '.name')
  project_scm_url=$(echo "${project_details}" | jq -r '.scm_url')

  # Append the details to the CSV file
  echo "${job_template_name},${organization_name},${verbosity},${project_name},${project_id},${project_scm_url}" >> "${OUTPUT_FILE}"
done

echo "Job templates have been exported to ${OUTPUT_FILE}"
gautam-y commented 6 months ago

!/bin/bash

Configuration

TOWER_URL="https://your-ansible-tower.com" TOWER_USERNAME="your_username" TOWER_PASSWORD="your_password" OUTPUT_FILE="job_templates.csv"

Function to fetch job templates with pagination

fetch_all_job_templates() { local url="${TOWER_URL}/api/v2/job_templates/" local results=()

while [ -n "$url" ] && [ "$url" != "null" ]; do response=$(curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" -H "Content-Type: application/json" "$url")

# Debugging output for response
# Uncomment the next line to see the full response if needed
# echo "$response" | jq .

# Check if results are not null
result_length=$(echo "$response" | jq -r '.results | length')

if [ "$result_length" -gt 0 ]; then
  results+=($(echo "$response" | jq -r '.results[] | @base64'))
fi
url=$(echo "$response" | jq -r '.next')

done

echo "${results[@]}" }

Function to fetch project details

fetch_project_details() { local project_id=$1 curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" -H "Content-Type: application/json" "${TOWER_URL}/api/v2/projects/${project_id}/" }

Initialize CSV file with headers

echo "Job Template Name,Organization Name,Verbosity,Project Name,Project ID,Project SCM URL" > "${OUTPUT_FILE}"

Fetch all job templates and iterate through them

job_templates=$(fetch_all_job_templates) for row in ${job_templates[@]}; do _jq() { echo "${row}" | base64 --decode | jq -r "${1}" }

job_template_name=$(_jq '.name') organization_name=$(_jq '.summary_fields.organization.name') verbosity=$(_jq '.verbosity') project_id=$(_jq '.project')

Fetch project details for each job template

project_details=$(fetch_project_details "${project_id}") project_name=$(echo "${project_details}" | jq -r '.name') project_scm_url=$(echo "${project_details}" | jq -r '.scm_url')

Append the details to the CSV file

echo "${job_template_name},${organization_name},${verbosity},${project_name},${project_id},${project_scm_url}" >> "${OUTPUT_FILE}" done

echo "Job templates have been exported to ${OUTPUT_FILE}"

gautam-y commented 6 months ago

#!/bin/bash

# Configuration
TOWER_URL="https://your-ansible-tower.com"
TOWER_USERNAME="your_username"
TOWER_PASSWORD="your_password"
OUTPUT_FILE="job_templates.csv"

# Function to fetch job templates with pagination
fetch_all_job_templates() {
  local url="${TOWER_URL}/api/v2/job_templates/"
  local results=()

  while [ -n "$url" ] && [ "$url" != "null" ]; do
    response=$(curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" -H "Content-Type: application/json" "$url")

    # Debugging output for response
    # Uncomment the next line to see the full response if needed
    # echo "$response" | jq .

    # Check if results are not null
    result_length=$(echo "$response" | jq -r '.results | length')

    if [ "$result_length" -gt 0 ]; then
      results+=($(echo "$response" | jq -r '.results[] | @base64'))
    fi
    url=$(echo "$response" | jq -r '.next')
  done

  echo "${results[@]}"
}

# Function to fetch project details
fetch_project_details() {
  local project_id=$1
  curl -s -u "${TOWER_USERNAME}:${TOWER_PASSWORD}" -H "Content-Type: application/json" "${TOWER_URL}/api/v2/projects/${project_id}/"
}

# Initialize CSV file with headers
echo "Job Template Name,Organization Name,Verbosity,Project Name,Project ID,Project SCM URL" > "${OUTPUT_FILE}"

# Fetch all job templates and iterate through them
job_templates=$(fetch_all_job_templates)
for row in ${job_templates[@]}; do
  _jq() {
    echo "${row}" | base64 --decode | jq -r "${1}"
  }

  job_template_name=$(_jq '.name')
  organization_name=$(_jq '.summary_fields.organization.name')
  verbosity=$(_jq '.verbosity')
  project_id=$(_jq '.project')

  # Fetch project details for each job template
  project_details=$(fetch_project_details "${project_id}")
  project_name=$(echo "${project_details}" | jq -r '.name')
  project_scm_url=$(echo "${project_details}" | jq -r '.scm_url')

  # Append the details to the CSV file
  echo "${job_template_name},${organization_name},${verbosity},${project_name},${project_id},${project_scm_url}" >> "${OUTPUT_FILE}"
done

echo "Job templates have been exported to ${OUTPUT_FILE}"
gautam-y commented 6 months ago

Sure, here's an updated version of the script that retrieves job templates from Ansible Tower API pages until the `next` field in the response is `null`, indicating there are no more pages to retrieve:

```bash
#!/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 file name
output_file="job_templates.csv"

# Create a temporary file to store the API response
temp_file=$(mktemp)

# Function to retrieve job template data
get_job_templates() {
    local url=$1
    local next_url=""

    # Make the API request and store the response in the temporary file
    curl -s -k -u "${tower_username}:${tower_password}" "${url}" > "${temp_file}"

    # Extract the results and next URL from the response
    results=$(jq '.results[]' "${temp_file}")
    next_url=$(jq -r '.next' "${temp_file}")

    # Append the results to the output file
    echo "${results}" | jq -r '.name, .summary_fields.organization.name, .verbosity, .summary_fields.project.name, .summary_fields.project.id, .summary_fields.project.scm_url' | tr '\n' ',' >> "${output_file}"

    # Check if there are more pages
    if [ "${next_url}" != "null" ]; then
        get_job_templates "${next_url}"
    fi
}

# Create the output file and add the header row
echo "Job Template Name,Organization Name,Verbosity,Project Name,Project ID,Project SCM URL" > "${output_file}"

# Retrieve job templates from all available pages
get_job_templates "${tower_url}/api/v2/job_templates/"

# Clean up the temporary file
rm "${temp_file}"

echo "Job template data exported to ${output_file}"

Usage:

  1. Replace your-ansible-tower-url, your-username, and your-password with your actual Ansible Tower URL and credentials.
  2. Save the script to a file (e.g., get_job_templates.sh).
  3. Make the script executable: chmod +x get_job_templates.sh
  4. Run the script: ./get_job_templates.sh

The script will retrieve all job templates from Ansible Tower and export the data to a CSV file named job_templates.csv in the current directory.

How it works:

Notes:

Sources

gautam-y commented 6 months ago

#!/bin/bash

# Set your Ansible Tower URL and authentication credentials
TOWER_URL="https://your-ansible-tower-url/api/v2"
USERNAME="your_username"
PASSWORD="your_password"

# Output CSV file
OUTPUT_FILE="job_templates.csv"

# Initialize the CSV file with headers
echo "Job Template Name,Organization Name,Verbosity,Project Name,Project ID,Project Branch,Project SCM URL" > "$OUTPUT_FILE"

# Function to fetch job templates from Ansible Tower
fetch_job_templates() {
    page=$1
    curl -s -u $USERNAME:$PASSWORD "$TOWER_URL/job_templates/?page=$page" | jq -c '.results[]'
}

# Function to get the organization name by its ID
get_organization_name() {
    org_id=$1
    if [[ -n $org_id ]]; then
        curl -s -u $USERNAME:$PASSWORD "$TOWER_URL/organizations/$org_id/" | jq -r '.name'
    else
        echo ""
    fi
}

# Function to get project details by its ID
get_project_details() {
    project_id=$1
    if [[ -n $project_id ]]; then
        curl -s -u $USERNAME:$PASSWORD "$TOWER_URL/projects/$project_id/"
    else
        echo ""
    fi
}

# Iterate through all pages of job templates
for ((page=1; page<=2000; page++)); do
    job_templates=$(fetch_job_templates $page)
    if [[ -z "$job_templates" ]]; then
        break
    fi

    # Process each job template
    echo "$job_templates" | while IFS= read -r job_template; do
        name=$(echo $job_template | jq -r '.name')
        organization_id=$(echo $job_template | jq -r '.organization')
        verbosity=$(echo $job_template | jq -r '.verbosity')
        project_id=$(echo $job_template | jq -r '.project')

        organization_name=$(get_organization_name $organization_id)
        project_details=$(get_project_details $project_id)
        project_name=$(echo $project_details | jq -r '.name')
        project_branch=$(echo $project_details | jq -r '.scm_branch')
        project_scm_url=$(echo $project_details | jq -r '.scm_url')

        # Replace empty values with a placeholder
        [[ -z $name ]] && name="NULL"
        [[ -z $organization_name ]] && organization_name="NULL"
        [[ -z $verbosity ]] && verbosity="NULL"
        [[ -z $project_name ]] && project_name="NULL"
        [[ -z $project_id ]] && project_id="NULL"
        [[ -z $project_branch ]] && project_branch="NULL"
        [[ -z $project_scm_url ]] && project_scm_url="NULL"

        # Append the details to the CSV file
        echo "$name,$organization_name,$verbosity,$project_name,$project_id,$project_branch,$project_scm_url" >> "$OUTPUT_FILE"
    done
done

echo "Export completed. The job templates have been saved to $OUTPUT_FILE."
gautam-y commented 6 months ago

#!/bin/bash

# Set your Ansible Tower URL and authentication credentials
TOWER_URL="https://your-ansible-tower-url/api/v2"
USERNAME="your_username"
PASSWORD="your_password"

# Output CSV file
OUTPUT_FILE="job_templates.csv"

# Initialize the CSV file with headers
echo "Job Template Name,Organization Name,Verbosity,Project Name,Project ID,Project Branch,Project SCM URL" > "$OUTPUT_FILE"

# Function to fetch all job templates from Ansible Tower
fetch_all_job_templates() {
    page=1
    while :; do
        response=$(curl -s -u $USERNAME:$PASSWORD "$TOWER_URL/job_templates/?page=$page")
        job_templates=$(echo $response | jq -c '.results[]')
        [[ -z "$job_templates" ]] && break
        echo "$job_templates"
        page=$((page + 1))
    done
}

# Function to get organization name by ID
get_organization_name() {
    org_id=$1
    curl -s -u $USERNAME:$PASSWORD "$TOWER_URL/organizations/$org_id/" | jq -r '.name'
}

# Function to get project details by ID
get_project_details() {
    project_id=$1
    curl -s -u $USERNAME:$PASSWORD "$TOWER_URL/projects/$project_id/"
}

# Fetch and process all job templates
fetch_all_job_templates | while IFS= read -r job_template; do
    name=$(echo $job_template | jq -r '.name')
    organization_id=$(echo $job_template | jq -r '.organization')
    verbosity=$(echo $job_template | jq -r '.verbosity')
    project_id=$(echo $job_template | jq -r '.project')

    organization_name="NULL"
    project_name="NULL"
    project_branch="NULL"
    project_scm_url="NULL"

    if [[ -n $organization_id ]]; then
        organization_name=$(get_organization_name $organization_id)
    fi

    if [[ -n $project_id ]]; then
        project_details=$(get_project_details $project_id)
        project_name=$(echo $project_details | jq -r '.name')
        project_branch=$(echo $project_details | jq -r '.scm_branch')
        project_scm_url=$(echo $project_details | jq -r '.scm_url')
    fi

    [[ -z $name ]] && name="NULL"
    [[ -z $organization_name ]] && organization_name="NULL"
    [[ -z $verbosity ]] && verbosity="NULL"
    [[ -z $project_name ]] && project_name="NULL"
    [[ -z $project_id ]] && project_id="NULL"
    [[ -z $project_branch ]] && project_branch="NULL"
    [[ -z $project_scm_url ]] && project_scm_url="NULL"

    echo "$name,$organization_name,$verbosity,$project_name,$project_id,$project_branch,$project_scm_url" >> "$OUTPUT_FILE"
done

echo "Export completed. The job templates have been saved to $OUTPUT_FILE."
gautam-y commented 6 months ago

#!/bin/bash

# Set your Ansible Tower URL and authentication credentials
TOWER_URL="https://your-ansible-tower-url.com/api/v2"
USERNAME="your_username"
PASSWORD="your_password"
TEMPLATE_FILE="job_templates.txt"
OUTPUT_FILE="job_runs.csv"

# Check if the required files exist
if [[ ! -f "$TEMPLATE_FILE" ]]; then
  echo "Job templates file not found!"
  exit 1
fi

# Create or clear the output CSV file
echo "Job Template,Last Job ID,Last Job Status,Last Job Finished" > "$OUTPUT_FILE"

# Read each job template name from the file
while IFS= read -r template_name; do
  # Fetch job template details to get the ID
  response=$(curl -s -u "$USERNAME:$PASSWORD" "$TOWER_URL/job_templates/?name=$template_name")
  template_id=$(echo "$response" | jq -r '.results[0].id')

  if [[ "$template_id" != "null" ]]; then
    # Fetch the last job run details for the job template
    job_response=$(curl -s -u "$USERNAME:$PASSWORD" "$TOWER_URL/job_templates/$template_id/jobs/?order_by=-finished&limit=1")
    job_id=$(echo "$job_response" | jq -r '.results[0].id')
    job_status=$(echo "$job_response" | jq -r '.results[0].status')
    job_finished=$(echo "$job_response" | jq -r '.results[0].finished')

    # Write the details to the CSV file
    echo "$template_name,$job_id,$job_status,$job_finished" >> "$OUTPUT_FILE"
  else
    echo "$template_name,Template not found,N/A,N/A" >> "$OUTPUT_FILE"
  fi
done < "$TEMPLATE_FILE"

echo "Job details exported to $OUTPUT_FILE"