# SSH connection closes after contextmanager is finished self.command_user.append(self.user) self.user = user try: yield finally: self.command_user.pop() self.close() context = CliConnection(host=host,user="******") class Taskset(object): """ NOTE: This should be added to the repo """ def __init__(self, context): self.context = context def run(self, cmd, echo=True, **kwargs): self.context.run(cmd, echo=echo, **kwargs) class CliTaskset(Taskset): @task def which_user(self): self.run("whoami") # bar with self.context.update_config(user="******"): self.run("whoami") # root ns = Collection() ns.add_collection(Collection.from_class(CliTaskset(context)), name="clitaskset")