Skip to content

Instantly share code, notes, and snippets.

@curioswati-zz
Created September 8, 2016 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save curioswati-zz/80d3bab8e83991c411b4635c04ba50b0 to your computer and use it in GitHub Desktop.
Save curioswati-zz/80d3bab8e83991c411b4635c04ba50b0 to your computer and use it in GitHub Desktop.
A python script to install Ansible from source on a Debian based operating system.
#!/usr/bin/env python
import subprocess
import optparse
import platform
# -------------------------------------Globals------------------------------------------------------
install = []
uninstall = ["sudo pip uninstall ansible", "sudo apt-get purge libffi-dev libssl-dev"]
PLATFORM = platform.system()
ARCHITECTURE = platform.architecture()[0]
# -------------------------------------Running commands--------------------
def run_commands(cmds):
"""
Function which run all commands one by one.
"""
for cmd in cmds:
try:
subprocess.call(cmd, shell=True)
except Exception as e:
print e
# ----------------------------------------Option parsing-------------------
def controller():
"""
Function to control option parsing.
"""
global install, VERBOSE
# Create instance of OptionParser Module, included in Standard Library
p = optparse.OptionParser(description='For installing ansible',
prog='install_ansible',
version='install_ansible 0.1',
usage='%prog [option]')
p.add_option('--install', '-i', action="store_true", help='install ansible from source.')
p.add_option('--uninstall', '-u', action="store_true",
help='uninstall ansible along with development libraries installed during the installation process.')
p.add_option('--verbose', '-v',
action='store_true',
help='prints verbosely',
default=False)
# Option Handling passes correct parameter to runBash
options, arguments = p.parse_args()
if options.verbose:
VERBOSE = True
if options.install:
if PLATFORM == "Linux":
# ----------------------------------------setting commands----------------------------
# Install gcc to compile the code
install.append("sudo apt-get install gcc")
# Installing python setuptools for installing pip
install.append("sudo apt-get install python-setuptools")
# Installing pip to get ansible source from pypi
install.append("sudo easy_install pip")
# Installing python-dev for python development libraries
install.append("sudo apt-get install python-dev")
# Installing development dependencies for ansible
install.append("sudo apt-get install libffi-dev")
install.append("sudo apt-get install libssl-dev")
# Install ansible
install.append("sudo pip install ansible")
else:
print "Wrong operating system detected."
value = run_commands(install)
elif options.uninstall:
value = run_commands(uninstall)
else:
p.print_help()
# Runs all the functions
def main():
controller()
# This idiom means the below code only runs when executed from command line
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment