From 2df6399176e0e6318e099e109d68ce9c4e13de6a Mon Sep 17 00:00:00 2001 From: Seth Vidal Date: Thu, 5 Apr 2012 13:28:56 -0400 Subject: [PATCH 1/3] make dist be ?dist so if it is not there you don't get any noise as per fedora usage guidelines https://fedoraproject.org/wiki/Packaging:DistTag --- ansible.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible.spec b/ansible.spec index 83b7e70c109..2a1d366d0db 100644 --- a/ansible.spec +++ b/ansible.spec @@ -1,7 +1,7 @@ %{!?python_sitelib: %global python_sitelib %(%{__python} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")} Name: ansible -Release: 1%{dist} +Release: 1%{?dist} Summary: Minimal SSH command and control Version: 0.0.2 From 254f98bdf52a6037544877d4a43e5be7347cf47f Mon Sep 17 00:00:00 2001 From: Seth Vidal Date: Thu, 5 Apr 2012 17:03:36 -0400 Subject: [PATCH 2/3] handle incorrect commands w/o '=' slightly better --- library/virt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/library/virt b/library/virt index 1922aefaaa3..c12ce9e21bd 100755 --- a/library/virt +++ b/library/virt @@ -194,9 +194,7 @@ class Virt(object): "nrVirtCpu" : data[3], "cpuTime" : str(data[4]), } - thisvm = self.conn.find_vm(vm) - if hasattr(thisvm, 'autostart'): - info[vm]["autostart"] = thisvm.autostart() + info[vm]["autostart"] = self.conn.get_autostart(vm) return info @@ -378,6 +376,10 @@ def main(): # command=[some command] [guest=name] params = {} + if '=' not in args: + msg = "No proper arguments provided to virt module: %s" % args + return VIRT_FAILED, msg + for x in items: (k, v) = x.split("=") params[k] = v From 7e50d170a8b89c9b451123e3b1a4c7eca7ea7021 Mon Sep 17 00:00:00 2001 From: Seth Vidal Date: Thu, 5 Apr 2012 17:06:23 -0400 Subject: [PATCH 3/3] move the bulk of the opt parsing out of ansible/ansible-playbook and into utils for other scripts to use. --- bin/ansible | 31 +++---------------------------- bin/ansible-playbook | 19 +++---------------- lib/ansible/utils.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 44 deletions(-) diff --git a/bin/ansible b/bin/ansible index b069103d556..268ad37f6e4 100755 --- a/bin/ansible +++ b/bin/ansible @@ -46,38 +46,13 @@ class Cli(object): def parse(self): ''' create an options parser for bin/ansible ''' - - parser = OptionParser(usage = 'ansible [options]') + + parser = utils.base_parser(constants=C, runas_opts=True, async_opts=True, + output_opts=True, usage='ansible [options]') parser.add_option('-a', '--args', dest='module_args', help="module arguments", default=C.DEFAULT_MODULE_ARGS) - parser.add_option('-B', '--background', dest='seconds', type='int', default=0, - help='run asynchronously, failing after X seconds') - parser.add_option('-D','--debug', default=False, action="store_true", - help='enable standard error debugging of modules.') - parser.add_option('-f','--forks', dest='forks', default=C.DEFAULT_FORKS, type='int', - help='number of parallel processes to use') - parser.add_option('-i', '--inventory-file', dest='inventory', - help='inventory host file', default=C.DEFAULT_HOST_LIST) - parser.add_option('-k', '--ask-pass', default=False, action='store_true', - help='ask for SSH password') - parser.add_option('-M', '--module-path', dest='module_path', - help="path to module library", default=C.DEFAULT_MODULE_PATH) parser.add_option('-m', '--module-name', dest='module_name', help="module name to execute", default=C.DEFAULT_MODULE_NAME) - parser.add_option('-o', '--one-line', dest='one_line', action='store_true', - help='condense output') - parser.add_option('-P', '--poll', default=C.DEFAULT_POLL_INTERVAL, type='int', - dest='poll_interval', help='set the poll interval if using -B') - parser.add_option("-s", "--sudo", default=False, action="store_true", - dest='sudo', help="run operations with sudo (nopasswd)") - parser.add_option('-t', '--tree', dest='tree', default=None, - help='log output to this directory') - parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int', - dest='timeout', help='set the SSH timeout in seconds') - parser.add_option('-u', '--user', default=C.DEFAULT_REMOTE_USER, - dest='remote_user', help='connect as this user') - parser.add_option('-p', '--port', default=C.DEFAULT_REMOTE_PORT, type='int', - dest='remote_port', help='set the remote ssh port') options, args = parser.parse_args() self.callbacks.options = options diff --git a/bin/ansible-playbook b/bin/ansible-playbook index 5e764324952..836b11578e4 100755 --- a/bin/ansible-playbook +++ b/bin/ansible-playbook @@ -26,31 +26,18 @@ import ansible.playbook import ansible.constants as C from ansible import errors from ansible import callbacks +from ansible import utils def main(args): ''' run ansible-playbook operations ''' # create parser for CLI options - parser = OptionParser() - parser.usage = "ans-playbook playbook.yml ..." - parser.add_option('-D','--debug', default=False, action="store_true", - help='enable standard error debugging of modules.') - parser.add_option('-f','--forks', dest='forks', default=C.DEFAULT_FORKS, type='int', - help='set the number of forks to start up') - parser.add_option("-i", "--inventory-file", dest="inventory", - help="inventory host file", default=C.DEFAULT_HOST_LIST) + usage = "ans-playbook playbook.yml" + parser = utils.base_parser(constants=C, usage=usage) parser.add_option('-e', '--extra-vars', dest='extra_vars', help='arguments to pass to the inventory script') - parser.add_option("-k", "--ask-pass", default=False, action="store_true", - help="ask for SSH password") - parser.add_option("-M", "--module-path", dest="module_path", - help="path to module library", default=C.DEFAULT_MODULE_PATH) parser.add_option('-O', '--override-hosts', dest="override_hosts", default=None, help="run playbook against these hosts regardless of inventory settings") - parser.add_option('-p', '--port', default=C.DEFAULT_REMOTE_PORT, type='int', - dest='remote_port', help='set the remote ssh port') - parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int', - dest='timeout', help="set the SSH timeout in seconds") options, args = parser.parse_args(args) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index e061d6d0f20..ca0b88c5aa2 100755 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -23,6 +23,8 @@ import shlex import re import jinja2 import yaml +from optparse import OptionParser + try: import json @@ -30,6 +32,8 @@ except ImportError: import simplejson as json from ansible import errors +import ansible.constants as C + ############################################################### # UTILITY FUNCTIONS FOR COMMAND LINE TOOLS @@ -270,3 +274,41 @@ def parse_kv(args): options[k]=v return options +def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, async_opts=False): + ''' create an options parser for any ansible script ''' + + parser = OptionParser(usage) + parser.add_option('-D','--debug', default=False, action="store_true", + help='enable standard error debugging of modules.') + parser.add_option('-f','--forks', dest='forks', default=constants.DEFAULT_FORKS, type='int', + help='number of parallel processes to use') + parser.add_option('-i', '--inventory-file', dest='inventory', + help='inventory host file', default=constants.DEFAULT_HOST_LIST) + parser.add_option('-k', '--ask-pass', default=False, action='store_true', + help='ask for SSH password') + parser.add_option('-M', '--module-path', dest='module_path', + help="path to module library", default=constants.DEFAULT_MODULE_PATH) + parser.add_option('-T', '--timeout', default=constants.DEFAULT_TIMEOUT, type='int', + dest='timeout', help='set the SSH timeout in seconds') + parser.add_option('-p', '--port', default=constants.DEFAULT_REMOTE_PORT, type='int', + dest='remote_port', help='set the remote ssh port') + + if output_opts: + parser.add_option('-o', '--one-line', dest='one_line', action='store_true', + help='condense output') + parser.add_option('-t', '--tree', dest='tree', default=None, + help='log output to this directory') + + if runas_opts: + parser.add_option("-s", "--sudo", default=False, action="store_true", + dest='sudo', help="run operations with sudo (nopasswd)") + parser.add_option('-u', '--user', default=constants.DEFAULT_REMOTE_USER, + dest='remote_user', help='connect as this user') + + if async_opts: + parser.add_option('-P', '--poll', default=constants.DEFAULT_POLL_INTERVAL, type='int', + dest='poll_interval', help='set the poll interval if using -B') + parser.add_option('-B', '--background', dest='seconds', type='int', default=0, + help='run asynchronously, failing after X seconds') + + return parser