Skip to content

Instantly share code, notes, and snippets.

@curioswati
Forked from curioswati-zz/install_ansible.py
Created July 25, 2017 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curioswati/6c26f9bbb74497a65b7863037f3f2168 to your computer and use it in GitHub Desktop.
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.
#!/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()
@uberfallen
Copy link

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?

@curioswati
Copy link
Author

curioswati commented Dec 16, 2019

Yes, the CPython (the python we use) interpreter is written in C.

source: https://stackoverflow.com/a/44632493/3860168

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment