-1

I am using AWX curl for update template in bash script. Somehow this curl command is not able to run

curl --insecure -v -X PATCH https://somedomain.corp.com/api/v2/job_templates/\"$test_2_template\"/ -H 'Authorization: Basic keys==' -H 'Content-Type: application/json' -d '{"extra_vars":"{\"module_name\": \"$module_name\", \"env\": \"$appenv\", \"host_group_name\": \"$host_group_name\", \"rolename\": \"$rolename\", \"app_artifact_url\": \"$app_artifact_url\"}"}'

Looks like there is some issue in passing variables to curl command via bash

However if i run this from my terminal , it's working fine.

curl --insecure -v -X PATCH https://somedomain.com/api/v2/job_templates/279/ -H 'Authorization: Basic keys==' -H 'Content-Type: application/json' -d '{"extra_vars":"{\"module_name\": \"myservice\", \"env\": \"test\", \"host_group_name\": \"env2\", \"rolename\": \"myservice\", \"artifact_version\": \"2.1.0-SNAPSHOT\"}"}'

Please suggest some solution.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
ruminy
  • 1

1 Answers1

2

You can't substitute variables inside a single quoted string.

For terrible long commands like this, breaking them up into pieces is essential for readability and maintainability:

data=$(printf \
    '{"extra_vars": "{\"module_name\": \"%s\", \"env\": \"%s\", \"host_group_name\": \"%s\", \"rolename\": \"%s\", \"app_artifact_url\": \"%s\"}"}' \
    "$module_name" "$appenv" "$host_group_name" "$rolename" "$app_artifact_url"
)
curl_args=(
    --insecure 
    -v 
    -X PATCH  
    -H 'Authorization: Basic keys==' 
    -H 'Content-Type: application/json' 
    -d "$data"
)
url="https://somedomain.example.com/api/v2/job_templates/$test_2_template/"

curl "${curl_args[@]}" "$url"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352