#!/usr/bin/env python """ jane.py - a tool for capturing a servers configuration and generating an Ansible playbook. """ import sys import argparse import platform from subprocess import check_output def make_playbook(packages): """ Makes an Ansible playbook based on a list of packages. (v 0.1) packages - an array of tuples in the format [(package_manager, package_name, package_version)] package_manager - apt or rpm package_name - the name of the package used by the package manager. package_version - usually 'latest', but can be the specific version number in the format used by the package manager. returns a string containing the playbook. """ playbook = '- hosts: all\n' playbook += 'user: root\n' playbook += 'sudo: no\n' playbook += 'tasks:\n' for item in packages: playbook += '- name: install ' playbook += item playbook += '\naction: apt pkg=' playbook += item playbook += ' state=latest\nnotify:\n- start ' playbook += item playbook += '\n' return playbook def get_installed_packages(): """ Retrieves a list of installed packages. (v 0.1) Returns an array, packages, intended to be input for make_playbook packages - an array of tuples in the format [(package_manager, package_name, package_version)] package_manager - apt or rpm package_name - the name of the package used by the package manager. package_version - usually 'latest', but can be the specific version number in the format used by the package manager. """ os_system = platform.system() if os_system == 'Darwin': try: check_output('brew') except: pass elif os_system == 'Linux': try: installed_packages = check_output(["dpkg", "--get-selections"]) packages = [] for line in installed_packages.split('\n')[:-1]: packages += [line.split()[0]] except: pass try: check_output('yum') except: pass return packages if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("-a", "--automagic", "--automatic", action="store_true", help="Generate playbook named jane.yml for current machine.") parser.add_argument("-b", "--buildplaybook", action="store_true", help="Output playbook instead of raw data.") parser.add_argument("-c", "--getconfig", action="store_true", help="Collect configuration data.") parser.add_argument("-m", "--machine", help="Machine to run against. Default current machine.") parser.add_argument("-i", "--infile", type=argparse.FileType('r'), default=sys.stdin, help="Input file. Default is STDIN.") parser.add_argument("-o", "--outfile", type=argparse.FileType('w'), default=sys.stdout, help="output file. Defaul to STDOUT.") parser.add_argument("-p", "--packages", action="store_true", help="Retrieve packages from machine instead of from input file.") parser.add_argument("-v", "--verbose", action='count', help="increase output verbosity") parser.add_argument('--version', action='version', version='%(prog)s 0.1') args = parser.parse_args() output = '' if args.packages: packages = get_installed_packages() if args.buildplaybook: output = make_playbook(packages) args.outfile.write(output) print platform.node() print platform.system() print platform.python_version() print platform.release() print platform.machine() print platform.processor()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question