我發現這兩個區別。
1:Fabric maintains an in-memory password
2:sudo accepts additional user and group arguments
首先,面料從緩存中使用sudo(時)將獲得的密碼,那麼你就需要輸入密碼。但是如果你使用run('sudo cmd'),你需要輸入每個'sudo cmd'的密碼。其次,如果你想執行一個不在根目錄下的命令,但是像www這樣的其他用戶組,你只需要設置env.sudo_user ='www'或者sudo('cmd',user ='www')。第一個將在www下執行每個sudo(),第二個將在www下執行這個單個cmd。但是使用run()命令時,需要編輯才能運行(「sudo -u」www'cmd「)。
from fabric.api import sudo, run, env
env.hosts = ['host_ip',]
env.user = 'user_name'
env.sudo_user = 'sudo_user'
def test_1():
run('sudo pwd')
def test_2():
sudo('pwd')
$ fab -I --show=debug test_1 test_2
Initial value for env.password: # enter password
Commands to run: test_1, test_2
Parallel tasks now using pool size of 1
[ip_address] Executing task 'test_1'
[ip_address] run: /bin/bash -l -c "sudo pwd"
[ip_address] out: [sudo] password for billy: # needs to enter password here
[ip_address] out: /home/billy
[ip_address] out:
Parallel tasks now using pool size of 1
[ip_address] Executing task 'test_2'
[ip_address] sudo: sudo -S -p 'sudo password:' -u "root" /bin/bash -l -c "pwd"
[ip_address] out: sudo password: # only prompt, do not need enter password
[ip_address] out: /home/billy
[ip_address] out:
Done.
Disconnecting from ip_address... done.