I am trying to automate these steps so that I don’t need to do this on every machines manually. I need to install latest app server software (abc.tar.gz) on all the unix boxes.
I need to do this in around 12 machines – "machine1" ... "machine12". I have a master machine "machine0" which has abc.tar.gz file so I was thinking to run my script from this machine only and install abc.tar.gz software by following below steps in all those 12 machines one by one. My unix account id is david and my process runs as golden user.
This is the steps I follow if I am installing abc.tar.gz in “machine1”:
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="086c697e616c4865696b6061666d39">[email protected]</a>:~$ sudo scp <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4d0d5c2ddd0f4d9d5d7dcdddad184">[email protected]</a>:/home/david/abc.tar.gz . <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="036762756a67436e62606b6a6d6632">[email protected]</a>:~$ sudo stop test_server <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c080d1a05082c010d0f040502095d">[email protected]</a>:~$ sudo su - golden <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="03646c6f67666d436e62606b6a6d6632">[email protected]</a>:~$ cd /opt/process <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2a22212928230d202c2e252423287c">[email protected]</a>:/opt/process$ rm -rf * <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a0a8aba3a2a987aaa6a4afaea9a2f6">[email protected]</a>:/opt/process$ exit <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0a4a1b6a9a480ada1a3a8a9aea5f1">[email protected]</a>:~$ sudo cp abc.tar.gz /opt/process <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a3a6b1aea387aaa6a4afaea9a2f6">[email protected]</a>:~$ sudo chown -R golden /opt/process <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b1b4a3bcb195b8b4b6bdbcbbb0e4">[email protected]</a>:~$ sudo su - golden <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aacdc5c6cecfc4eac7cbc9c2c3c4cf9b">[email protected]</a>:~$ cd /opt/process <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e88f87848c8d86a885898b8081868dd9">[email protected]</a>:/opt/process$ tar -xvzf abc.tar.gz <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f5929a9991909bb59894969d9c9b90c4">[email protected]</a>:/opt/process$ exit <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e4a4f58474a6e434f4d4647404b1f">[email protected]</a>:~$ sudo start test_server
How can I automate this so that it can do the same steps in all 12 machines one bye one? I mean install abc.tar.gz in machine1 first, then install on machine2 and go on.. I want to run this script from machine0 only.
Is there any way to automate this?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I would recommend a provisioning tool such as Fabric or Ansible for this.
Both are fairly easy to set up, and will allow you to do more the just the command you need in this case, since you be able to build off you initial playbooks or fabfile depending on which tool you go with.
Docs for Fabric can be found here: http://docs.fabfile.org/en/1.10/
Docs for Ansible can be found here: http://docs.ansible.com/ansible/intro_getting_started.html
You’ll need some comfort with SSH keys and SSH configurations for both tools, which you can find in the docs as well.
Method 2
pdsh can do what you want. By default, it will operate on several machines at once in parallel, but you can use the -f option to limit it to running on each machine one at a time if you want.
e.g.
# copy abc.tar.gz to /home/david on machine1..machine12
pdcp -w machine[1-12] abc.tar.gz /home/david
# run a bunch of commands on machine1..machine12
# be careful of quoting
pdsh -w machine[1-12] 'sudo stop service ; do something ; do something else ;
sudo start service'
Alternatively, rather than put all the commands in single- or double-quotes on the pdsh command line, you can pdcp a shell script to the remote machines and run it with a single command.
pdcp -w machine[1-12] abc.tar.gz /home/david pdcp -w machine[1-12] ./my-script.sh /tmp pdsh -w machine[1-12] sh -c '/tmp/my-script.sh'
(use sh -c just in case /tmp is mounted noexec)
This is particularly useful if you want to avoid getting confused by having multiple levels of nested quotes (e.g. if you need to run some commands with sudo or su, or embed an awk or sed or whatever script). It’s often a lot simpler to just write a normal shell (or awk or perl or whatever) script, copy it to where it’s needed, and run it there.
pdsh also allows you to use a file called /etc/genders to define machines and what (arbitrary) attributes you assign to them. e.g.
machine0 master,all machine[1-12] machines,all web[1-4] webservers,all db[1-2] mysql,db,all
You can select the machines by attribute on the pdsh or pdcp command line with -g, e.g. pdcp -g machines abc.tar.gz /home/david or pdsh -g all uname -a or pdsh -g web,db,master uptime.
pdsh was written for use on HPC clusters, but i’ve found it to be equally useful for bulk-administering any group(s) of machines.
BTW, pdsh is a different kind of tool to puppet or ansible or cfengine etc – they’re for installing and maintaining a consistent environment on a group of machines (such as a cluster or a VM farm etc). pdsh is more for running one-off commands and/or copies (to or from) on that same group of machines.
Method 3
In terms of a script, you might be looking at something like:
MACHINES = (
'machine1'
'machine2'
...
)
for machine in "${MACHINES[@]}"; do
# insert sequence of setup steps here.
done
This is a quick and dirty way to have a deploy script across multiple machines. Of course, there are other more “professional” ways to do this task, but often this is sufficient and maintainable.
Method 4
I prefer to use xargs for such tasks.
As an example
#/bin/sh cat serverlist.txt | xargs -I % sh -c 'ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="394b56564d79">[email protected]</a>% 'date';'echo test''
And serverlist.txt contains IP/hostnames newline separated.
For a greater server park of cause it’s better to use tools like ansible.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0