Fabric runs bash script that ask for sudo password - How to send this password
<p>I want to use fabric to deploy some application on remote machines. For this, I use fabric to retrieve a bash script from a VCS (bitbucket or github) and execute it. However, the first step of my script is to add the current user to the sudoers, so I am requested for a password.</p>

<p>Is it possible to send this password in the fabfile or within the fab command or.... ?</p>

<p>A portion of code:</p>

<p><strong>bash</strong></p>

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

<p><strong>fabfile</strong></p>

<pre><code>def deploy():
    env.hosts = ['192.168.100.160']
    source_folder = '/home/username/src'
    branch = 'dev'
    puts('Pulling changes from branch &lt;{}&gt;'.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')
</code></pre>

<p>Use fabric's "<strong>sudo</strong>" function instead of "<strong>run</strong>" function. Script won't prompt for password since it will be running with sudo privilege.</p>

<pre><code>def deploy():
   env.hosts = ['192.168.100.160']
   source_folder = '/home/username/src'
   branch = 'dev'
   puts('Pulling changes from branch &lt;{}&gt;'.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')
</code></pre>

<p>Hope it helps !!</p>

python bash fabric