Ansible Tower: 'extra_vars' with multiple variables are ignored when running with curl in cmd - ansible

Can some one tell how to pass multiple extra_vars variables from the command line which will run the Job Template in Tower?
I've followed Ansible documentaion https://docs.ansible.com/ansible-tower/latest/html/userguide/job_templates.html#passing-extra-variables-to-provisioning-callbacks
curl -f -H 'Content-Type: application/json' -XPOST \
-d '{"host_config_key": "efref3d9-740f-429c-43r2-15t326b76", "extra_vars": "{\"Job_ID\": \"24\"},{\"job_templates\": \"test99\"}"}' \
https://tower-ansible.com:443/api/v2/job_templates/822/callback/ -k
For single variable it's working. Below curl command works with single extra variable
curl -f -H 'Content-Type: application/json' -XPOST -d '{"host_config_key": "efref3d9-740f-429c-43r2-15t323b76", "extra_vars": "{\"job_id\": \"24\"}"}' https://tower-ansible.com:443/api/v2/job_templates/822/callback/ -k

According the documentation about Passing Extra Variables to Provisioning Callbacks you have referenced you may
Use the following JSON format as an example when adding your own extra_vars to be passed
"extra_vars": {"variable1":"value1","variable2":"value2",...}
instead of as in your provided example
"extra_vars": {"variable1":"value1"},{"variable2":"value2"}

Related

bash script to convert string value to json and then return json

I am very new to bash scripting, please can someone point me in the right direction on how to accomplish my task?
I have a curl call which returns a string, which I want to convert to json.
My curl statement -
curl --insecure -X POST 'https://url/api/IPAM/GetIP' --header 'Content-Type: application/json' --header -d '{"key1": "value1"}'
This curl statement returns a string ,for example: 10.100.100.100
I want to fetch this string and return the output in json format:
{"IP":"10.100.100.100"}
I don't want to use jquery or python to do this because this entire script will be run by a wrapper that only understands bash.
You can use jq to process your IP string into a JSON string and package it into a JSON object of your choice.
ip="10.100.100.100"
jq --arg ip "$ip" -cn '{"IP":$ip}'
Result:
{"IP":"10.100.100.100"}
Now if working with the result of your example curl POST request:
rawip_string=$(curl --insecure -X POST 'https://url/api/IPAM/GetIP' --header 'Content-Type: application/json' --header -d '{"key1": "value1"}')
jq --arg ip "$rawip_string" -cn '{"IP":$ip}'
One way to not rely on external tools like jq, you can get the output ip attribute that to a variable and concatenate it.
$ return=$(echo 10.100.100.100)
$ echo "{\"IP\":\"${return}\"}"
{"IP":"10.100.100.100"}
Like this
printf '{"IP":"%s"}' "$(curl --insecure -X POST 'https://url/api/IPAM/GetIP' --header 'Content-Type: application/json' --header -d '{"key1": "value1"}')"

Calling curl with an environment variable within the parameters

I am trying to execute a curl command using an environment variable.
curl -u {user}: -X POST --header 'Content-Type: application/json' -d '{"my-var": " '"$ENV_VAR"' "}' https://url
However, this is not correctly fetching the output of $ENV_VAR, and instead passes in the string $ENV_VAR
The above implementation was taken from this question.
How do I correctly pass the value of the environment variable into the curl command?

Executing curl command with variables in bash

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.
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"

Nested cURL call

I've one cron task to update my DDNS with my current ip address and do it through a cURL call.
The problem is one of the parameters to pass in the call is the CURRENT IP and in order to discover ir i need to do another cURL call.
I would like to know if is possible to nest two cURL calls in one single script in order to make my cron task avoiding extra scripts
example:
to get my current ip I use
curl ipinfo.io/ip
to update my ddns i need to do:
curl -X PUT "https://api.cloudflare.com/client/v4/zones/2wertyh/dns_records/23ertghj" \
-H "X-Auth-Email: tomatechines#gmail.com" \
-H "X-Auth-Key: 123ertgyh" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"qwsdfg.com.br","content":"MY-CURRENT-IP","ttl":1800,"proxied":false}'
how can i fit this two calls together in order to make my cron task
Use command substitution, like this:
curl -X PUT "https://api.cloudflare.com/client/v4/zones/2wertyh/dns_records/23ertghj" \
-H "X-Auth-Email: tomatechines#gmail.com" \
-H "X-Auth-Key: 123ertgyh" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"qwsdfg.com.br","content":"'"$(curl ipinfo.io/ip)"'","ttl":1800,"proxied":false}'
String argument for --data is composed from three concatenated parts, 'beginning' "$(curl ...)" 'ending' (more details see in this answer).

< was unexpected at this time. from curl command line when posting input data as an xml to rest service

when i post a raw string as an input to JSON REST Service call it is executing ex:
curl -d "{\"input1\": \"as\", \"input2\": \"ad\"}" -i -X POST -H "Content-Type:application/json" http://localhost/rtygies/Service1.svc/rest/receivedata1
But when i am posting as an xml as input it is giveng error as below:
curl -d "{\"input1\": \"<xml></xml>\", \"input2\": \"<xml></xml>\"}" -i -X POST -H "Content-Type:application/json" http://localhost/rtygies/Service1.svc/rest/receivedata1
Error: < was unexpected at this time
I am using curl in windows.
can any one say how to post xml as a string input to Rest service in JSON format from curl
Actually, I don't know what is the case,
but I got an error said < is unexpected this time
when I tried the following command (on windows):
curl -u admin:password -XPOST -H 'Content-type: text/xml' -d '<namespace><prefix>newWorkspace</prefix><uri>http://geoserver.org</uri></namespace>' http://localhost:8080/geoserver/rest/namespaces
Then I changed the single quote into double quotes and it worked.
Redirection symbols can be escaped by "
Try using
curl -d "{\"input1\": \""<xml></xml>"\", \"input2\": \""<xml></xml>"\"}" -i -X POST -H "Content-Type:application/json" http://localhost/rtygies/Service1.svc/rest/receivedata1
I had no luck with double-quotes, however escaping < and > with ^ worked for me.
So your curl becomes..
curl -d "{\"input1\": \"^<xml^>^</xml^>\", \"input2\": \"^<xml^>^</xml^>\"}" -i -X POST -H "Content-Type:application/json" http://localhost/rtygies/Service1.svc/rest/receivedata1

Resources