Discussion:
[Fab-user] Keeping remote path (or remote context) for next command
Jörg Schneider
2015-02-13 13:36:26 UTC
Permalink
For managing some network devices I have to send something like „configure“ command at first before I can do the real magic with fab.
Does fabric „keep“ the remote context (or „path" if you see it as „cd something“)?

J. Schneider
Jeff Forcier
2015-02-13 23:00:53 UTC
Permalink
Executing commands over SSH doesn't keep a real shell around (this is
true for other SSH automation too - think of it as a series of "ssh
<hostname> <command>" calls in your shell).

So - typically you have to fake it by generating prefixed commands.
E.g. Fabric's cd() helper just updates run() calls within its block so
they all actually start with "cd <path> && ...".

You can do this with arbitrary commands by using the prefix() helper -
http://docs.fabfile.org/en/1.10/api/core/context_managers.html#fabric.context_managers.prefix

Best,
Jeff
Post by Jörg Schneider
For managing some network devices I have to send something like „configure“ command at first before I can do the real magic with fab.
Does fabric „keep“ the remote context (or „path" if you see it as „cd something“)?
J. Schneider
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
--
Jeff Forcier
Unix sysadmin; Python/Ruby engineer
http://bitprophet.org
Jeff Forcier
2015-02-16 17:23:08 UTC
Permalink
Thank you Jeff, this is a useful hint. But unfortunately the switches don’t allow concatenated commands like shells '&&’ :-/
Ah sorry, I totally flaked on that detail. Yes, prefix() is oriented
towards shells, so it won't work there.

If you need this command prefix most/all of the time for these hosts,
you could just do something like this (possibly even importing fab's
run() as a different name and defining this as the local name 'run',
if you wanted):

def netrun(command, **kwargs):
command = "configure {0}".format(command)
return run(command, **kwargs)

Basically, regular Python tricks for working around APIs that don't
quite fit your needs, should apply here.

Best,
Jeff
Post by Jeff Forcier
Executing commands over SSH doesn't keep a real shell around (this is
true for other SSH automation too - think of it as a series of "ssh
<hostname> <command>" calls in your shell).
So - typically you have to fake it by generating prefixed commands.
E.g. Fabric's cd() helper just updates run() calls within its block so
they all actually start with "cd <path> && ...".
You can do this with arbitrary commands by using the prefix() helper -
http://docs.fabfile.org/en/1.10/api/core/context_managers.html#fabric.context_managers.prefix
Best,
Jeff
Post by Jörg Schneider
For managing some network devices I have to send something like „configure“ command at first before I can do the real magic with fab.
Does fabric „keep“ the remote context (or „path" if you see it as „cd something“)?
J. Schneider
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
--
Jeff Forcier
Unix sysadmin; Python/Ruby engineer
http://bitprophet.org
--
Jeff Forcier
Unix sysadmin; Python/Ruby engineer
http://bitprophet.org
Loading...