Fabric运行请求sudo密码的bash脚本 - 如何发送此密码

时间:2016-09-08 20:10:22

标签: python bash fabric

我想使用fabric在远程计算机上部署一些应用程序。为此,我使用fabric从VCS(bitbucket或github)检索bash脚本并执行它。但是,我的脚本的第一步是将当前用户添加到sudoers,因此我被要求输入密码。

是否可以在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')

1 个答案:

答案 0 :(得分:0)

使用面料&#34; sudo &#34;功能而不是&#34; 运行&#34;功能。脚本不会提示输入密码,因为它将以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')

希望它有所帮助!!