-
-
Save curioswati/6c26f9bbb74497a65b7863037f3f2168 to your computer and use it in GitHub Desktop.
A python script to install Ansible from source on a Debian based operating system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Yes, the CPython (the python we use) interpreter is written in C.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, thank you for writing this code. I'd like to leverage your code to automate the installation of the Ansible Control Node latest version (ie 2.8). The host OS will be RHEL 7.7 vs. Debian. Question - it appears for Python to run a GCC is required - is this true?