Skip to content

Commit 32ae8f2

Browse files
author
Claudio Fahey
committed
updated documentation
1 parent a53b888 commit 32ae8f2

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.rst

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
===================================
3+
DevOps Scripts
4+
===================================
5+
6+
7+
8+
This repository contains various Python scripts that can be used to automate various devops tasks.
9+
10+
- configue-ssh.py: configure password-less SSH to remote hosts
11+
12+
- mount-nfs.py: permanently mount an NFS export
13+
14+
- prepare-data-disks.py: partition, format, and mount each of the specified disks.
15+
16+
See the help in each script for usage and examples.
17+
18+
19+
Simple Command Execution on Multiple Hosts using Xargs
20+
------------------------------------------------------
21+
22+
There are many tools for executing commands on multiple hosts. However most such tools require
23+
additional packages to be installed. For quick and dirty tasks, xargs can easily be used and shown below.
24+
25+
Setup is extremely easily. Create a text file where each line is the FQDN (fully qualified domain name),
26+
hostname without domain, or the IP address
27+
of each host in your cluster. If you have different groups of nodes, then you may want to create
28+
a different file for each group. For instance, the file master.txt may contain the following:
29+
30+
.. parsed-literal::
31+
32+
mycluster1-master-1.example.com
33+
mycluster1-master-2.example.com
34+
mycluster1-master-3.example.com
35+
36+
You would then create another file named worker.txt with the FQDN of your worker hosts.
37+
Now you are ready to automate some tasks as shown in the below examples.
38+
39+
First a quick test. We'll just print the name of each FQDN prefixed with "Hello".
40+
The **-n 1** option tells xargs to run the supplied command on each line instead of grouping
41+
them into batches. The **-i** option tells xargs to replace the string **{}** with the
42+
contents of the line which is our FQDN.
43+
44+
.. parsed-literal::
45+
46+
$ cat master.txt worker.txt | xargs -n 1 -i echo Hello {}.
47+
Hello mycluster1-master-1.example.com.
48+
Hello mycluster1-master-2.example.com.
49+
Hello mycluster1-master-3.example.com.
50+
Hello mycluster1-worker-1.example.com.
51+
...
52+
53+
To run a command on each host using SSH, use this command.
54+
Note that you'll need password-less SSH configured or you will be prompted for a password on each connection.
55+
If you haven't configured password-less SSH, see the script configure-ssh.py in this repository.
56+
57+
.. parsed-literal::
58+
59+
$ cat master.txt worker.txt | xargs -n 1 -i ssh root@{} free -m
60+
61+
To run a compound command on each host:
62+
63+
.. parsed-literal::
64+
65+
$ cat master.txt worker.txt | xargs -n 1 -i ssh root@{} "hostname ; swapon -s ; free -m"
66+
67+
You can easily filter the list of hosts to run commands on. This is useful for testing on one host before
68+
running on all hosts.
69+
70+
.. parsed-literal::
71+
72+
$ cat worker.txt | head -1 | xargs -n 1 -i ssh root@{} free -m
73+
74+
By default, xargs will run the commands sequentially, one host at a time.
75+
If any command returns an error, xargs will stop and will not run additional commands.
76+
You can run all commands concurrently with the **-P 0** option. This will also effectively
77+
ignore any errors.
78+
79+
.. parsed-literal::
80+
81+
$ cat worker.txt | head -1 | xargs -n 1 -i ssh root@{} yum install -y big_package
82+
83+
You can also use SCP to copy files between the local host and the remote hosts.
84+
85+
.. parsed-literal::
86+
87+
$ cat master.txt worker.txt | xargs -n 1 -i scp /etc/hosts root@{}:/etc/hosts
88+
89+
Sometimes, you will need to execute sudo on the remote host. When sudo executes on certain
90+
versions of CentOS or RedHat, it requires a TTY. This can be faked with the ssh **-tt** option.
91+
92+
.. parsed-literal::
93+
94+
$ cat master.txt worker.txt | xargs -n 1 -i ssh -tt centos@{} sudo mount -a

configure-ssh.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
%prog -u root host1 host2
1414
Same as above but it will repeat for host1 and host2.
1515
16+
%prog -u root $(cat master.txt worker.txt)
17+
Same as above but it will repeat for each host listed in the files master.txt and worker.txt.
18+
1619
%prog -u root -p mysecretpassword host1 host2
1720
To avoid being prompted for a password, specify it with the -p option.
1821
You must have sshpass installed for this to work.

0 commit comments

Comments
 (0)