How to set the working directory for a Fabric task?

后端 未结 2 1478
轻奢々
轻奢々 2021-01-01 11:20

Assuming I define a trivial task to list files on a remote server:

from fabric.api import run, env

env.use_ssh_config = True

def list_files():
    run(\'ls         


        
相关标签:
2条回答
  • 2021-01-01 11:35

    Use the Context Manager cd:

    from fabric.api import run, env
    from fabric.context_managers import cd
    
    env.use_ssh_config = True
    
    def list_files():
        with cd('/tmp'):
            run('ls')
    
    0 讨论(0)
  • 2021-01-01 11:37

    Answer for fabric 2.4.0 looks like this:

    from fabric import Connection
    
    conn = Connection(host=HOST_NAME, user=USER_NAME, connect_kwargs={'password': PASSWORD})
    
    with conn.cd('/tmp/'):
        conn.run('ls -la')
    

    This is not covered by the fabric documentation but by the invoke documentation.

    0 讨论(0)
提交回复
热议问题