Support Questions
Find answers, ask questions, and share your expertise
Alert: Please see the Cloudera blog for information on the Cloudera Response to CVE-2021-4428

add host to ambari cluster using python

Rising Star


I am trying to add new host to ambari host through python. I want to perform

curl -i -H "X-Requested-By: ambari" -u username:password -X POST -d @test.json http://dev-nifi01-ambari1:80/api/v1/clusters/test-nifi/hosts/hostname

with following code

import os
import urllib2
import base64
import json

username = "admin"
password = "admin"
hostname = os.getenv('HOSTNAME')
url = "http://dev-nifi01-ambari1:80/api/v1/clusters/test-nifi/hosts/" + hostname
data = {"blueprint" : "recommended","host_group" : "nifi-hg-1"}
req = urllib2.Request(url, data=json.dumps(data))
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
req.add_header("Authorization", "Basic %s" % base64string)
res = urllib2.urlopen(req)

But getting following error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/", line 154, in urlopen
    return, data, timeout)
  File "/usr/lib64/python2.7/", line 437, in open
    response = meth(req, response)
  File "/usr/lib64/python2.7/", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python2.7/", line 475, in error
    return self._call_chain(*args)
  File "/usr/lib64/python2.7/", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.7/", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request