1

I have a fabric file where I set the env.password as I read about on its docs:

from fabric.api import *


env.password = "mypassword"

print env.user
print env.password
def update():
    local("sudo apt-get update")

Yet when I run the file, it's still prompting me for a password:

cchilders@cchilders-Dell-Precision-M3800:~$ fab -f fab_setup_new_linux_box.py update
cchilders
mypassword
[localhost] local: sudo apt-get update
[sudo] password for cchilders: 

I thought that was the idea for setting env.password? When I use sudo or run, it says:

"No hosts found. Please specify (single) host string for connection: "

How do I make it run things using the password I set? Thank you

codyc4321
  • 9,014
  • 22
  • 92
  • 165
  • check http://stackoverflow.com/questions/2326797/how-to-set-target-hosts-in-fabric-file – xiº Nov 26 '15 at 04:19
  • that didn't help me, I just want this fabfile to run on my local machine. setting `env.hosts = ['localhost']` didn't help either – codyc4321 Nov 26 '15 at 04:42
  • What you see there is the `sudo` password prompt. You can not preset it. To prevent this prompt you have to run as root or adapt you sudoers file to allow sudo without password for this command. – Klaus D. Nov 26 '15 at 05:50
  • even after setting `env.user = "root"`, I get prompted – codyc4321 Nov 26 '15 at 09:17

1 Answers1

1

I got it to work. Its not pretty.

from fabric.api import *


env.password = "mypassword"

print env.user
print env.password
def update():
    local("echo {} | sudo -S apt-get update".format(env.password))

Yes, i don't like it either. Here is the output: (yes i created your user on my virtualbox)

$ fab update
cchilders
mypassword
[localhost] local: echo mypassword | sudo -S apt-get update
Hit http://ppa.launchpad.net precise InRelease
Hit http://ppa.launchpad.net precise/main Sources                                                                                   
Hit http://ppa.launchpad.net precise/main amd64 Packages                                                    
Hit http://ppa.launchpad.net precise/main i386 Packages              
Hit http://ppa.launchpad.net precise/main TranslationIndex           
Hit http://security.ubuntu.com precise-security InRelease            
Hit http://ppa.launchpad.net precise/main Translation-en                
Ign http://us.archive.ubuntu.com precise InRelease
Hit http://us.archive.ubuntu.com precise-updates InRelease
Hit http://us.archive.ubuntu.com precise-backports InRelease
Get:1 http://security.ubuntu.com precise-security/main Sources [136 kB] 
Get:2 http://us.archive.ubuntu.com precise Release.gpg [198 B]
Get:3 http://us.archive.ubuntu.com precise-updates/main Sources [494 kB]
Get:4 http://security.ubuntu.com precise-security/restricted Sources [4,476 B]
Get:5 http://security.ubuntu.com precise-security/universe Sources [43.8 kB] 
Get:6 http://security.ubuntu.com precise-security/multiverse Sources [2,198 B]        
Get:7 http://security.ubuntu.com precise-security/main amd64 Packages [563 kB]
...
Javier Buzzi
  • 6,296
  • 36
  • 50
  • you're serious? I love it! I never knew that was even possible, but I just did echo mypw | sudo -S apt-get update and you can see it blip it right in (if you watch close). That's the coolest thing I've ever seen in bash and it doesn't even require fabric, which I learned is more for setting up my VMs (I've only got one live project, and can use this script for it) – codyc4321 Nov 28 '15 at 23:25
  • Do you have any technical idea of what it's doing? As in, when you pipe, you pass something to the next command, but I thought echo was going to stdout (what you can see, not what you type to shell) I wanna try this on different types of prompts, but do you know exactly what that echo is doin? Thanks Javier – codyc4321 Nov 28 '15 at 23:26
  • actually I think it's this that solved it? -S, --stdin Write the prompt to the standard error and read the password from the standard input instead of using the terminal device. The password must be followed by a newline character. – codyc4321 Nov 29 '15 at 00:17
  • 1
    @codyc4321 Yes, this is all bash, no fabric. I dont like the solution, seems "hacky". I would like a better way of doing it thats all fabric. I'll keep playing around with it. Yes like you said, the `-S` is for stdin. – Javier Buzzi Nov 30 '15 at 02:26