user3507094 user3507094 - 2 years ago 105
Python Question

How to call my fabfile within my Python script

Good Day all,

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
from fabric.api import run
def deploy():
run('wget -P /tmp')
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 -u ec2-user -i id_rsa -H 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?

Thanks in advance. I really appreciate any help I get!


Answer Source

Try with subprocess :

import subprocess['fab', '-f', '', '-u ec2-user', '-i', 'id_rsa', '-H', bakery_internalip, 'deploy'])

should do the trick

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download