Search code examples
pythonsshfabric

When I run my fabfile, I am being asked for password although I specified a key. Why?


I have a python script which runs a fabfile. My issue is that I am asked for a password whenever I run the fabfile from my script. However, the login works fine with the specified key when I run the fabfile manually from the command line even though I am using the same fab parameters. Here is the contents of my fabfile:

[root@ip-10-10-20-82 bakery]# cat fabfile.py
from fabric.api import run

def deploy():
    run('wget -P /tmp https://s3.amazonaws.com/LinuxBakery/httpd-2.2.26-1.1.amzn1.x86_64.rpm')
    run('sudo yum localinstall /tmp/httpd-2.2.26-1.1.amzn1.x86_64.rpm')

Here is the syntax I use on the command line that works successfully:

fab -u ec2-user -i id_rsa -H 10.10.15.185 deploy

Here is the bit of python code which for some reason is prompting for a password instead of using the key:

import subprocess
subprocess.call(['fab', '-f', '/home/myhome/scripts/bakery/fabfile.py', '-u ec2-user', '-i', '/home/myhome/scripts/bakery/id_rsa', '-H', bakery_internalip, 'deploy'])

Here is what happens when I run it:

[10.10.15.185] Executing task 'deploy'
[10.10.15.185] run: wget -P /tmp https://s3.amazonaws.com/LinuxBakery/httpd-2.2.26-1.1.amzn1.x86_64.rpm
[10.10.15.185] Login password for ' ec2-user':

Solution

  • I was being asked for a password even though I had specified a key because there was an extra space between the "u" and "ec2-user". Here is the snippet before:

    '-u ec2-user'
    

    And here it is after:

    '-uec2-user'
    

    The extra space meant that fab was trying to authenticate with " ec2-user" instead of "ec2-user".