Skip to content

Instantly share code, notes, and snippets.

@seanorama
Last active December 6, 2022 13:51
Show Gist options
  • Save seanorama/6e2958f847cee2132792 to your computer and use it in GitHub Desktop.
Save seanorama/6e2958f847cee2132792 to your computer and use it in GitHub Desktop.
ambari api
host_new=ip-10-0-0-162.ec2.internal
curl -u admin:admin -i -H X-Requested-By:API -X POST http://${ambari_server}:8080/api/v1/clusters/${cluster_name}/hosts/${host_new}
curl -u admin:admin -i -H X-Requested-By:API -X GET http://${ambari_server}:8080/api/v1/clusters/${cluster_name}/hosts/${host_new}
worker_services="NODEMANAGER JOURNALNODE HDFS_CLIENT DATANODE FLUME_HANDLER METRICS_MONITOR"
for service in ${worker_services}; do
curl -u admin:admin -i -H X-Requested-By:API -X POST http://${ambari_server}:8080/api/v1/clusters/${cluster_name}/hosts/${host_new}/host_components/${service}
done
for service in ${worker_services}; do
curl -u admin:admin -i -H X-Requested-By:API -X PUT -d '{"HostRoles": {"state": "INSTALLED"}}' http://${ambari_server}:8080/api/v1/clusters/${cluster_name}/hosts/${host_new}/host_components/${service}
done
## todo: add a wait state here
for service in ${worker_services}; do
curl -u admin:admin -i -H X-Requested-By:API -X PUT -d '{"HostRoles": {"state": "STARTED"}}' http://${ambari_server}:8080/api/v1/clusters/${cluster_name}/hosts/${host_new}/host_components/${service}
done

PUT /api/v1/users/admin

{
  "Users": {
    "user_name": "admin",
    "password": "newadminpassword",
    "old_password": "admin"
  }
}

How to update configurations from the Ambari API

What

This will show you how to fetch & update configurations using the Ambari API.

In this example, we will be working with the 'hive-site' configuration.

Requirements

  • jq for JSON manipulation.
  1. First set these variables
ambari_server=172.28.128.3  ## IP or hostname of your Ambari Server
cluster_name=Sandbox        ## your cluster's name
pass=admin                  ## your Ambari password
  1. Next, get the current configuration 'tag'. This identifies which 'configuration' is currently active.
curl -u admin:${pass} \
  http://${ambari_server}:8080/api/v1/clusters/${cluster_name}?fields=Clusters/desired_configs/hive-site

Sample result:

{
"href" : "http://172.28.128.3:8080/api/v1/clusters/Sandbox?fields=Clusters/desired_configs/hive-site",
"Clusters" : {
  "cluster_name" : "Sandbox",
  "version" : "HDP-2.2",
  "desired_configs" : {
    "hive-site" : {
      "tag" : "version1418758302218100734",
      "user" : "admin",
      "version" : 2
    }
  }
}
}
  1. To make this easier, load the configuration into a variable
tag=$(curl -u admin:${pass} \
  "http://${ambari_server}:8080/api/v1/clusters/${cluster_name}?fields=Clusters/desired_configs/hive-site" \
  | jq -r '.Clusters.desired_configs."hive-site".tag')
  1. Now you can see the current configuration:
curl -u admin:${pass} \
  "http://${ambari_server}:8080/api/v1/clusters/${cluster_name}/configurations?type=hive-site&tag=${tag}"

Sample result:

{
"href" : "http://172.28.128.3:8080/api/v1/clusters/Sandbox/configurations?type=hive-site&tag=version1418758302218100734",
"items" : [
  {
    "href" : "http://172.28.128.3:8080/api/v1/clusters/Sandbox/configurations?type=hive-site&tag=version1418758302218100734",
    "tag" : "version1418758302218100734",
    "type" : "hive-site",
    "version" : 2,
    "Config" : {
      "cluster_name" : "Sandbox"
    },
    "properties" : {
      "ambari.hive.db.schema.name" : "hive",
      "datanucleus.cache.level2.type" : "none",
    [... snipped the rest of the output ...]
  1. Lets put that into a variable which we will modify later with any configuration changes:
oldconfig="$(curl -u admin:${pass} \
  "http://${ambari_server}:8080/api/v1/clusters/${cluster_name}/configurations?type=hive-site&tag=${tag}")"
  1. Next we will update the configuration JSON with our changes and modify it's format to fit what Ambari requires.
newconfig="$(echo ${oldconfig} \
  | jq \
    --arg newtag $(echo version$(date +%s%N)) '.items[]
    | del(.["href", "version", "Config"])
    | .tag |= $newtag
    | .properties."hive.heapsize" |= "251"
    | {"Clusters": {"desired_config": .}}')"

All of that explained: - --arg newtag ... is setting the new configuration version in the same format as Ambari Web uses - del( is removing fields we do not need - .tag |= is setting the tag to the variable we set before - .properties."hive.heapsize" is updating that configuration - You would repeat with |.properties."the.config" |= "something" for each item you wish to change - {"Clusters ... is adding the parent keys which Ambari expects

If you'd like to see the result, do: echo ${newconfig} | jq '.'

  1. Finally, we send the new configuration:
echo ${newconfig} | curl -u admin:${pass} \
  -X PUT -H X-Requested-By:Sean -d @- \
  http://${ambari_server}:8080/api/v1/clusters/${cluster_name}
{
"resources" : [
  {
    "href" : "http://172.28.128.3:8080/api/v1/clusters/Sandbox/configurations/service_config_versions?service_name=HIVE&service_config_version=6",
    "configurations" : [
      {
        "clusterName" : "Sandbox",
        "type" : "hive-site",
        "versionTag" : "version1425390949695165000",
        "version" : 5,
        "serviceConfigVersions" : null,
        "configs" : {
          "hive.security.authenticator.manager" : "org.apache.hadoop.hive.ql.security.ProxyUserAuthenticator",
          "datanucleus.cache.level2.type" : "none",
          "javax.jdo.option.ConnectionPassword" : "hive",
          [... SNIPPED THE REST OF THE OUTPUT ...]
          "hive.heapsize" : "251",
          "hive.optimize.constant.propagation" : "true",
          "hive.exec.submitviachild" : "false"
        },
        "configAttributes" : { }
      }
    ],
    "group_id" : null,
    "group_name" : null,
    "service_config_version" : 6,
    "service_config_version_note" : null,
    "service_name" : "HIVE"
  }
]
}
  1. After that, Ambari will show a new configuration version and the affected services will need to be restarted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment