Blacksmith-shop / pdsh

Automatically exported from code.google.com/p/pdsh
GNU General Public License v2.0
0 stars 0 forks source link

Support user defined variables in pdsh #37

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi!

It would be nice having user defined variables per host in pdsh.

This could be the first "execute different command by host (I see a wishlist 
bug here talking about)" implementation:

Like %h is hostname.

We could define:

$ cat ~/.pdsh/vars/host01:
ports="8080 8081"
$ cat ~/.pdsh/vars/host02:
ports="8080"

Then:

pdsh -w ^filewithhosts "
for port in %ports
do
   wget -O - --user user --password pass localhost:\$port/manager/list
done
"

Do you think it could be possible in the future ?

Thank you developping this awesome tool !

Original issue reported on code.google.com by javibarr...@gmail.com on 9 Sep 2011 at 6:32

GoogleCodeExporter commented 8 years ago
please change from defect to wishlist, I don't know where can I do that now :(

Original comment by javibarr...@gmail.com on 9 Sep 2011 at 6:33

GoogleCodeExporter commented 8 years ago
Just to get a better idea of the feature request, could you come up with some 
other example use cases? Would it also satisfy your request if there was some 
way to run a
different command per target (host)? That might be a more general purpose 
solution.

One problem with your example above is that the code for each host would be
executed in parallel, but the wget for each port is executed serially. You could
actually get what you want with a bit of scripting I think. If you instead ran
something like

 pdsh -w host01:[8080-8081],host02:8080 -Rexec 'wget -O- %h'

Then all host/port combinations would be run in parallel, e.g.:

pdsh -w exec:host01:[8080-8081],exec:host02:8080 echo wget -O- %h
host01:8080: wget -O- host01:8080
host02:8080: wget -O- host02:8080
host01:8081: wget -O- host01:8081

(Unfortunately, a ':' character is used in -w to specify the rcmd method to
use, so we have to explicitly specify that for each host. (I think pdsh should
have a way to disable the rcmd:user@host syntax for -w, so I'll try to fix that
in the future)

An alternative would be to use a double-colon, then pick apart the host and port
in a script by splitting on '::'.

Original comment by mark.gro...@gmail.com on 9 Sep 2011 at 2:11

GoogleCodeExporter commented 8 years ago
Maybe there are different use:

a) you have machines where commands are not identical (may be ps from solaris 
vs ps linux or another more useful example which now I don't know), so:

.pdsh/vars/host01
PS="ps aux"
.pdsh/vars/host02
PS="ps -ef"

b) you have different apache logs location, because older admins installed one 
in /usr/local, others in /opt and others with apt / rpm, so in /var/log

.pdsh/vars/apache01
LOG=/var/log/apache/access_log
.pdsh/vars/apache02
LOG=/usr/local/apache/logs/access_log
.pdsh/vars/apache03
LOG=/opt/apache2/logs/access_log

But, yes, I didn't know about exec statement, and I always was thinking about 
%h like a hostname uniquely, I can use your trick for do my work!

Thank you very much !

Original comment by javibarr...@gmail.com on 9 Sep 2011 at 3:55

GoogleCodeExporter commented 8 years ago
Ok, those usages make sense. I think that a pdsh module could probably be
used to create a different environment per target host, but right now there
is no easy way in pdsh to export those new environment variables to the remote
commands for every rcmd type. Methods such as rsh have no way to export
environment variables, except by prepending the command with VAR1=VAL VAR2=VAL2 
...
PDSH already does this with PATH, so I guess this could be extended for
arbitrary environment variables.

Another way you could accomplish this would be to source a per-host variable on 
the
remote nodes, e.g.:

 pdsh -w ^hostfile -Rexec ssh -2 -l %u %h '. ~/.pdsh/vars/%h; command'

where ~/.pdsh/vars/HOSTNAME would set environment variables as you've shown 
above.
However, this assumes a shared filesystem on all hosts

I will see if it is possible to create a pdsh module that would do this
automatically, and on the host running pdsh instead of all remote hosts.
However, I think there may be some core pdsh changes needed in order to
allow arbitrary env vars to be set for all rcmd methods, so I don't think
I'll be able to accomplish this quickly.

Original comment by mark.gro...@gmail.com on 9 Sep 2011 at 4:17

GoogleCodeExporter commented 8 years ago
In the meanwhile, I filed a debian bug to has your lastest version (Debian 
version seems to not have implemented -w ^file correctly in the case which I 
need, but if I compile the latest it works!

If it is insteresting for you, I'm working with 147 systems which has not got 
any filesystem shared, so I could not apply your proposal. And I'm developing a 
shell script which is user independent (any user in the machine will exec my 
script, and vars will be in /etc/pdsh/vars or similar from bastion host.

Thank you very much!

Original comment by javibarr...@gmail.com on 9 Sep 2011 at 5:36