5

You know that on EC2, there is no password associated with "ubuntu" user. With the following lines, if I try to run :

fab development install_dir

I get :

[ec2-46-51-132-252.eu-west-1.compute.amazonaws.com] sudo: chown -R webadmin:webadmin /var/www [ec2-46-51-132-252.eu-west-1.compute.amazonaws.com] Login password:

I tried to add shell=False to sudo method (according to Can I prevent fabric from prompting me for a sudo password?), but it doesn't change anything

Any idea ? Thanks a lot !

def development():
    env.envname = 'development'
    env.user = 'ubuntu'
    env.group = 'ubuntu'
    env.chuser = 'webadmin'
    env.chgroup = 'webadmin'
    env.hosts = ['ec2-***.eu-west-1.compute.amazonaws.com']
    env.envname_abriev = 'dev'
    env.key_filename = '/home/xx/.ssh/xx.pem'

    env.postgresql_version = '9.0'

def install_dir():
    if not exists('/var/www'):
        sudo('mkdir /var/www')
    sudo('chown -R %s:%s /var/www' % (env.chuser, env.chgroup))
Community
  • 1
  • 1
Tom
  • 257
  • 1
  • 3
  • 9
  • 1
    What does it do if you just use `run` like this: `run('sudo chown -R ...')`? – guidoism Sep 12 '11 at 17:30
  • i get this with a regular run() as well, not just sudo(). Setting shell and pty to False does not help at all. – dave Apr 26 '12 at 21:17
  • turns out my issue was i had since changed my pem file key and not updated the fabfile! all good now. – dave Apr 27 '12 at 16:37
  • 1
    I have this same problem. Very frustrating. I can manually login to the shell and sudo without a password, but for some reason Fabric cannot. – Cerin Jun 14 '13 at 21:28

2 Answers2

4

Download (or create) a keypair file from aws as shown belowDownload keypair from the highlighted section

Create a file called fabfile.py and set its contents as follows:

from fabric.context_managers import cd
from fabric.operations import sudo
from fabric.api import run, env
import os

HOME = os.getenv('HOME')

env.user = 'ubuntu'
env.hosts = ['PUBLICDNS.ap-southeast-1.compute.amazonaws.com','ANOTHERSERVER.compute.amazonaws.com'] #can add multiple instances
env.key_filename = [
'%s/<your-keypair-file>.pem'%HOME
     ] #assuming keypair file is located in HOME

#example code we want to run on remote machine
def update():
    with cd('/var/www'):
            sudo('svn update')
                 with cd ('/var/www/cache'):
                       run('rm -rf *')
    sudo('service lighttpd restart')

To run the file, type fab update in the terminal.

chochim
  • 1,710
  • 5
  • 17
  • 30
  • What does this have to do with the OP's question? He can login to the server, so he already has his key pair. But he's getting asked for a password when he runs sudo, which won't be solved by generating a new key pair... – Cerin Jun 14 '13 at 21:14
  • Cerin - OP seems to have embedded everything in the development() function. This function is not being called by default when fabric install_dir is executed on the shell. I reckon while answering the question, I did not pay attention towards the development() function. I wrote what worked for me. – chochim Aug 23 '13 at 09:26
1

You need to specify keypair file name associated with your EC2 instance while running fab command.

Usage: fab [options] <command>[:arg1,arg2=val2,host=foo,hosts='h1;h2',...] ...

Options:

-R ROLES, --roles=ROLES
                    comma-separated list of roles to operate on

-i KEY_FILENAME       path to SSH private key file. May be repeated.
stema
  • 90,351
  • 20
  • 107
  • 135
devendram
  • 257
  • 1
  • 5