Search code examples
pythonsshbotofabric

How to call my fabfile within my Python script


I am struggling to launch my fabfile within my Python script. I have looked at similar posts on Stack Overflow regarding this but they don't solve my problem... Or maybe they do but I am not understanding them...Not sure.

My script writes to the fab file depending on what the user wants to run on the remote host. Here is an example of the fabfile:

[root@ip-50-50-50-50 bakery]# cat fabfile.py
from fabric.api import run
def deploy():
    run('wget -P /tmp https://s3.amazonaws.com/MyBucket/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')

I then need to run the fabfile from my script. If I run the following manually form the Command Line, then it works fine:

fab -f fabfile.py -u ec2-user -i id_rsa -H 10.10.15.150 deploy

1) How do I run that from inside my script with all of the options? 2) The IP address is a variable called "bakery_internalip". How do I call that variable as part of the fab line?


Solution

  • Try with subprocess :

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

    should do the trick