curl POST -o downloads a zip file but ansible does not? - ansible

I'm executing below commands -
JOINER_CONFIG=`curl -s -S -X GET -H "Accept: application/xml" http://dbsrd3485:8001/admin/v1/server-config`;curl -s -S --digest --user admin:NPgE3sFE -X POST -o cluster-config.zip -d "group=Default" --data-urlencode "server-config=${JOINER_CONFIG}" -H "Content-type: application/x-www-form-urlencoded" http://dbsrd3510:8001/admin/v1/cluster-config
The output is cluster-config.zip. How can I use ansible to perform the same job. I tried below ansible command but it doesn't return the zip file -
ansible dbsrd3485 -m shell -a 'JOINER_CONFIG=`curl -s -S -X GET -H "Accept: application/xml" http://dbsrd3485:8001/admin/v1/server-config`;curl -s -S --digest --user admin:NPgE3sFE -X POST -o cluster-config.zip -d "group=Default" --data-urlencode "server-config=${JOINER_CONFIG}" -H "Content-type: application/x-www-form-urlencoded" http://dbsrd3510:8001/admin/v1/cluster-config'
The output is -
dbsrd3485 | SUCCESS | rc=0 >>
I would appreciate if you someone can provide a yml script to execute using ansible-playbook.

Related

How to get curl response code in a variable along with output as a file, multiple outputs needs to be appended

I am trying to get the response code of the curl command to a variable and output of the same curl command in a file. Another curl command within a different function will run and the output should append to the same output file.
response=$(curl -i -H "content-type: application/json" -w "%{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{'<input>'}' >> out.txt
This is giving me neither the response nor the curl output in the file.
Below is working and gives me response as the desired http code though.
response=$(curl -i -H "content-type: application/json" -w "{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{'<input>'}'
Echo $response

How to resolve curl: (6) Couldn't resolve host [duplicate]

This question already has answers here:
Variable interpolation in the shell
(3 answers)
Closed 2 years ago.
I am trying to invoke rest api using curl in a loop as shown below. It doesn't work and throws error curl: (6) Couldn't resolve host '$SERVERPROTOCOL:'
However, if replace all environment variables and execute command then it works just fine.
Direct command which works:
curl -X DELETE -k -H 'Content-Type: application/xml' -H 'Accept: application/xml' -u 'testuser:test123' -i 'https://nonprodhost:443/process/cancel/pvm:0a126'
Curl in loop which throws - curl: (6) Couldn't resolve host '$SERVERPROTOCOL:'
for pi in $(cat $halted_pid);do
# write PID to console so user knows script is working
echo
echo "cacnelling process instance - $pi"
# 2>&1 to include any output on stderr
curl -X DELETE -k -H 'Content-Type: application/xml' -H 'Accept: application/xml' -u '$USERNAME:USERPASS' -i '$SERVERPROTOCOL://$SERVERHOST:$SERVERPORT/process/cancel/$pi' 2>&1 | tee -a $halted_pi_cancellation_logfile
done
Why I think issue is single quote? Try replacing single quotes with double quotes
(reference: https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double )
curl -X DELETE -k -H 'Content-Type: application/xml' -H 'Accept: application/xml' -u '$USERNAME:USERPASS' -i '$SERVERPROTOCOL://$SERVERHOST:$SERVERPORT/process/cancel/$pi' 2>&1 | tee -a $halted_pi_cancellation_logfile
convert to:
curl -X DELETE -k -H "Content-Type: application/xml" -H "Accept: application/xml" -u "$USERNAME:USERPASS" -i "$SERVERPROTOCOL://$SERVERHOST:$SERVERPORT/process/cancel/$pi" 2>&1 | tee -a $halted_pi_cancellation_logfile

How to set Quiet option for Elastic Search when doing a Bulk Insert?

I can do this:
curl -s -XPOST 1.2.3.4:9200/my_index/my_index_type/_bulk -H "Content-Type: application/x-ndjson" --data-binary #/home/modified.json
But this fails:
curl -s -XPOST 1.2.3.4:9200/my_index/my_index_type/_bulk -H "Content-Type: application/x-ndjson" --data-binary #/home/modified.json --quiet
How to set 'quiet'?
Thanks.
Seems you want to Disallows non-log STDOUT output with --quiet. Let's try this way-
curl -s --quiet -XPOST 1.2.3.4:9200/my_index/my_index_type/_bulk -H "Content-Type: application/x-ndjson" --data-binary #/home/modified.json
According to the doc of --quiet,
This flag must come before any command.
If it doesn't do the job then you can use the -o switch and send the output to dev/null instead of using --quiet
curl -s -o /dev/null -XPOST 1.2.3.4:9200/my_index/my_index_type/_bulk -H "Content-Type: application/x-ndjson" --data-binary #/home/modified.json

Bash: Curl multiple lines, write output to file

I have a simple bash script that call cURL with several params.
I need to write the output in a file (also overwriting). But I cannot do.
The call itself works, but I have only an empty file (and the answer is a json, I can read on the shell with that echo)
Thank you in advance for your help
1st try
curl -X POST "https://www.example.com"\
-H "X-Auth-Email: $email"\
-H "X-Auth-Key: $auth_key"\
-H "Content-Type: application/json"\
--data '{"name":"'$name'","surname":"'$surname'"}'
>> id.txt
echo
2nd try
curl -X POST "https://www.example.com"\
-H "X-Auth-Email: $email"\
-H "X-Auth-Key: $auth_key"\
-H "Content-Type: application/json"\
--data '{"name":"'$name'","surname":"'$surname'"}'
o id.txt
echo
curl -X POST "https://www.example.com"\
-H "X-Auth-Email: $email"\
-H "X-Auth-Key: $auth_key"\
-H "Content-Type: application/json"\
--data '{"name":"'$name'","surname":"'$surname'"}'\
-o id.txt

Get curl http code

I have the following cURL request. I want to get the http_code, but I want in a different variable, because otherwise it messes with parsing the JSON response from the GET call.
Is there anyway to do this?
curl --write-out %{http_code} --silent --output GET --header "Accept: application/json" --header "URL"
Just use command substitution to store status code in a variable:
status=$(curl --write-out %{http_code} --silent --output tmp.out GET --header "Accept: application/json" --header "URL")
data=$(<tmp.out)
# check status now
declare -p status
# check data
declare -p data
curl -i -H "Accept: application/json" "server:5050/a/c/getName{"param0":"Arvind "}"
curl -w 'RESP_CODE:%{response_code}' -s -X POST --data '{"asda":"asd"}' http://example.com --header "Content-Type:application/json"|grep -o 'RESP_CODE:[1-4][0-9][0-9]'
response=$(curl -sb -H "Accept: application/json" "http://host:8080/some/resource") For response just try $response
Try this following may be this could help you to find the solution https://gist.github.com/sgykfjsm/1dd9a8eee1f70a7068c9

Resources