Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

How can i write a script to automate stop of all ambari server

avatar
Rising Star

Hello,

I work actualy with HDP 2.5 and my cluster have 25 nodes. I try to automate all my script in partuclar a script to start and stop all ambari agent.

My cluster are not install with root but with username ambari.

i try ssh with this command

hosts=$(curl -s -u admin:admin -H 'X-Requested-By:ambari' -i -k -X GET ${AMBARI_HSTNAME}:8080/api/v1/clusters/muclusterName/hosts | grep '"host_name"' | cut -d'"' -f4)
print $hosts

for host in $hosts
do
     ssh ambari2@$host "systemctl stop ambari-agent.service"
     print DONE for $host ...............................
done

10155-ambariagent-error.png

But i got this error below.

Please can you help me? 
Thanks
1 ACCEPTED SOLUTION

avatar
Super Collaborator
hide-solution

This problem has been solved!

Want to get a detailed solution you have to login/registered on the community

Register/Login
9 REPLIES 9

avatar
Expert Contributor

@Baruch AMOUSSOU DJANGBAN

Assuming ambari2 user can stop the services,

Why don't you try using

ambari-agent stop

instead of

systemctl stop ambari-agent.service	

avatar
Super Collaborator

you have ambari2 as the username, may be you need to use ambari. Also, has ambari user been setup with password less ssh to the other nodes.

avatar

@Baruch AMOUSSOU DJANGBAN

Are you running ambari-agent as "root" user or "ambari2" user?

avatar
Rising Star

@Karthik Narayanan i run ambari agent with root. Ambari2 user have no privilige to do that

avatar
Super Collaborator

in your ssh command ssh ambari2@$host, you have ambari2 and in your question you say you installed as ambari, so just wanted to confirm ambari2 is correct.

so if you are able to ssh correctly, you have two issue,

i think ambari-agent will run as root, so you need to do a sudo.

so u have to pass -t to you ssh command. ssh -t ambari2@host (alternately you can disable requiretty in /etc/sudoers ).

So in /etc/sudoers of each remote machine, you have to enable ambari to be able to run command without password.

like this

ambari2 ALL=(ALL) NOPASSWD: ALL

avatar
Super Collaborator

you have ambari2 as the username, may be you need to use ambari. Also, has ambari user been setup with password less ssh to the other nodes.

avatar
Rising Star

no it's a mistake. It's ambari2 user not ambari

avatar
Super Collaborator
hide-solution

This problem has been solved!

Want to get a detailed solution you have to login/registered on the community

Register/Login

avatar

@Baruch AMOUSSOU DJANGBAN

you can do this as well.If you have installed the Cluster Shell in cluster, we can perform below simple steps to stop and start

-----------------------------------

#!/bin/sh

clush -g all ambari-agent restart

-------------------------------------------

Refer below link for more info about open source Cluster shell:

https://github.com/cea-hpc/clustershell/downloads