2016-09-08 54 views
0

我想使用fabric在遠程機器上部署某些應用程序。爲此,我使用結構從VCS(bitbucket或github)檢索bash腳本並執行它。但是,我的腳本的第一步是將當前用戶添加到sudoers,所以我需要輸入密碼。Fabric運行bash腳本請求sudo密碼 - 如何發送此密碼

是否可以在fabfile或fab命令或....內發送此密碼?

的代碼的一部分:

的bash

sudo tee /etc/sudoers.d/$USER <<END 
END 
file=/usr/share/MyCompagny/mybashscript.sh 
sudo touch $file 
sudo echo 'blablabla' >> $file 
sudo /bin/rm /etc/sudoers.d/$USER 
sudo -k 

fabfile

def deploy(): 
    env.hosts = ['192.168.100.160'] 
    source_folder = '/home/username/src' 
    branch = 'dev' 
    puts('Pulling changes from branch <{}>'.format(branch)) 
    projects = ['data', 'report'] 
    for project in projects: 
     current_path = os.path.join(source_folder, 'package.{}'.format(project)) 
     with cd(current_path): 
      puts('Current path: {}'.format(current_path)) 
      # Discard all pending changes 
      run('git checkout -- .') 
      # Checkout the right branch 
      run('git checkout {}'.format(branch)) 
      # Pull changes 
      run('git pull origin_ssh {}'.format(branch)) 
    puts('Install with bash script') 
    with cd(source_folder): 
     run('./mybashscript.sh') 

回答

0

使用織物的 「須藤」 功能,而不是 「運行」 功能。腳本不會提示輸入密碼,因爲它將以sudo特權運行。

def deploy(): 
    env.hosts = ['192.168.100.160'] 
    source_folder = '/home/username/src' 
    branch = 'dev' 
    puts('Pulling changes from branch <{}>'.format(branch)) 
    projects = ['data', 'report'] 
    for project in projects: 
     current_path = os.path.join(source_folder, 'package.{}'.format(project)) 
     with cd(current_path): 
     puts('Current path: {}'.format(current_path)) 
     # Discard all pending changes 
     sudo('git checkout -- .') 
     # Checkout the right branch 
     sudo('git checkout {}'.format(branch)) 
     # Pull changes 
     sudo('git pull origin_ssh {}'.format(branch)) 
     puts('Install with bash script') 
     with cd(source_folder): 
     sudo('./mybashscript.sh') 

希望它有幫助!