0

I want to run a method on several remote hosts in parallel through Python script. I have their credential (ip,usr,pass). in order to do it I decorate the method with @parallel, and called it via execute() where I gave all hosts. my question is how do I set env.usr, env.password for each task? here is an example of my code:

class deployment()

    __init__():
        self.hosts = read_ips_from_csv

    def do_something(self)
       run(remote_command)

    def run_remote(self,func):
       execute(func,hosts = self.hosts) 

    def deploy(self):
       run_remote(self.do_something)

main():

my_deploy = deployment()
my_deploy.deploy()

the question is how to set the env parameters for each host in do_something()

thanks a lot for your answers!!

Stan
  • 8,710
  • 2
  • 29
  • 31
Nuvi
  • 627
  • 1
  • 5
  • 8

2 Answers2

2

OK, so this is how i solve it (found it in another question https://stackoverflow.com/a/5568219/3216763 ): i added env.hosts and env password before calling the execute like this:

def run_remote(self,func):
    env.hosts = ['user1@host1:port1', 'user2@host2.port2']
    env.passwords = {'user1@host1:port1': 'password1', 'user2@host2.port2': 'password2'}
    execute(func)  

spent quite time on it so maybe it will help others.

Community
  • 1
  • 1
Nuvi
  • 627
  • 1
  • 5
  • 8
0
  • At least for the login, this can be sent along with the hostname as explained in How to set target hosts in Fabric file.
  • I haven't found a simple way to do that for passwords. This is certainly monkey-patchable.

Note that the preferred way to do that is to use SSH keys with help you get rid of all this in a very simple way

Community
  • 1
  • 1
Oct
  • 1,505
  • 13
  • 18
  • Note that the preferred way to do that is to use SSH keys with help you get rid of all this in a very simple way. can you clarify? – Nuvi Jan 21 '14 at 07:32
  • SSH keys allows you to log on several password protected ssh account using a same password you decide. Usage of a ssh keys boils down to generating a ssh key, putting its public version on each server and adding `env.key_filename = "~/.ssh/id_rsa"` to your fabfile. For more information about ssh keys, Google is your friend (this box feels largely too small for such a discussion :))... – Oct Jan 21 '14 at 07:53
  • thanks! i solved it this time but will sure look at your suggestion. – Nuvi Jan 21 '14 at 18:21