0

the following API example , will stop the kafka service in ambari

export service=kafka

curl -u admin:admin -i -H 'X-Requested-By: ambari' -X PUT -d '{"RequestInfo":{"context":"_PARSE_.STOP.$service","operation_level":{"level":"SERVICE","cluster_name":"$CLUSTER_NAME","service_name":"$service"}},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}' http://$HOST:8080/api/v1/clusters/$CLUSTER_NAME/services/$service 

the problem is about the syntax - _PARSE_.STOP.$service

and we see that actually service value - kafka not set in - PARSE.STOP.$service

so ambri saw the name as - PARSE.STOP.$service and not PARSE.STOP.kafka

any idea how we can set the value kafka inside the json syntax?

jessica
  • 2,426
  • 24
  • 66
  • 1
    Does this answer your question? [Expansion of variables inside single quotes in a command in Bash](https://stackoverflow.com/questions/13799789/expansion-of-variables-inside-single-quotes-in-a-command-in-bash) – Maxim Sagaydachny Feb 25 '20 at 07:59

1 Answers1

0

Variables are not getting substituted when single quote type literals are used for strings so you need to break your string into parts to be able to insert some values into resulting string.

export service=kafka

curl -u admin:admin -i \
-H 'X-Requested-By: ambari' \
-X PUT \
-d '{"RequestInfo":{"context":"_PARSE_.STOP.'$service'","operation_level":{"level":"SERVICE","cluster_name":"'$CLUSTER_NAME'","service_name":"$service"}},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}' \
http://$HOST:8080/api/v1/clusters/$CLUSTER_NAME/services/$service

here is an simplified example to make it clear:

#!/bin/bash
service=XXX

json='{"A":"$service"}'
echo "wrong JSON: $json"

json='{"A":"'$service'"}' #this string consist of 3 parts '{"A":"' + $service + '"}'
echo "good JSON: $json"

output:

wrong JSON: {"A":"$service"}

good JSON: {"A":"XXX"}

Maxim Sagaydachny
  • 2,098
  • 3
  • 11
  • 22