Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

add host to ambari cluster using python

Highlighted

add host to ambari cluster using python

Rising Star

Hi,

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('X-Requested-By','ambari')
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/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib64/python2.7/urllib2.py", line 437, in open
    response = meth(req, response)
  File "/usr/lib64/python2.7/urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python2.7/urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
Don't have an account?
Coming from Hortonworks? Activate your account here