fixed ansible-pull broken options

* sudo was not working, now it supports full become
* now default checkout dir works, not only when specifying
* paths for checkout dir get expanded
* fixed limit options for playbook
* added verbose and debug info
pull/13420/merge
Brian Coca 9 years ago
parent e1c62fb5af
commit d5446f9804

@ -210,7 +210,7 @@ class CLI(object):
@staticmethod @staticmethod
def base_parser(usage="", output_opts=False, runas_opts=False, meta_opts=False, runtask_opts=False, vault_opts=False, module_opts=False, def base_parser(usage="", output_opts=False, runas_opts=False, meta_opts=False, runtask_opts=False, vault_opts=False, module_opts=False,
async_opts=False, connect_opts=False, subset_opts=False, check_opts=False, inventory_opts=False, epilog=None, fork_opts=False): async_opts=False, connect_opts=False, subset_opts=False, check_opts=False, inventory_opts=False, epilog=None, fork_opts=False, runas_prompt_opts=False):
''' create an options parser for most ansible scripts ''' ''' create an options parser for most ansible scripts '''
# TODO: implement epilog parsing # TODO: implement epilog parsing
@ -267,10 +267,6 @@ class CLI(object):
if runas_opts: if runas_opts:
# priv user defaults to root later on to enable detecting when this option was given here # priv user defaults to root later on to enable detecting when this option was given here
parser.add_option('-K', '--ask-sudo-pass', default=C.DEFAULT_ASK_SUDO_PASS, dest='ask_sudo_pass', action='store_true',
help='ask for sudo password (deprecated, use become)')
parser.add_option('--ask-su-pass', default=C.DEFAULT_ASK_SU_PASS, dest='ask_su_pass', action='store_true',
help='ask for su password (deprecated, use become)')
parser.add_option("-s", "--sudo", default=C.DEFAULT_SUDO, action="store_true", dest='sudo', parser.add_option("-s", "--sudo", default=C.DEFAULT_SUDO, action="store_true", dest='sudo',
help="run operations with sudo (nopasswd) (deprecated, use become)") help="run operations with sudo (nopasswd) (deprecated, use become)")
parser.add_option('-U', '--sudo-user', dest='sudo_user', default=None, parser.add_option('-U', '--sudo-user', dest='sudo_user', default=None,
@ -287,6 +283,12 @@ class CLI(object):
help="privilege escalation method to use (default=%s), valid choices: [ %s ]" % (C.DEFAULT_BECOME_METHOD, ' | '.join(C.BECOME_METHODS))) help="privilege escalation method to use (default=%s), valid choices: [ %s ]" % (C.DEFAULT_BECOME_METHOD, ' | '.join(C.BECOME_METHODS)))
parser.add_option('--become-user', default=None, dest='become_user', type='string', parser.add_option('--become-user', default=None, dest='become_user', type='string',
help='run operations as this user (default=%s)' % C.DEFAULT_BECOME_USER) help='run operations as this user (default=%s)' % C.DEFAULT_BECOME_USER)
if runas_opts or runas_prompt_opts:
parser.add_option('-K', '--ask-sudo-pass', default=C.DEFAULT_ASK_SUDO_PASS, dest='ask_sudo_pass', action='store_true',
help='ask for sudo password (deprecated, use become)')
parser.add_option('--ask-su-pass', default=C.DEFAULT_ASK_SU_PASS, dest='ask_su_pass', action='store_true',
help='ask for su password (deprecated, use become)')
parser.add_option('--ask-become-pass', default=False, dest='become_ask_pass', action='store_true', parser.add_option('--ask-become-pass', default=False, dest='become_ask_pass', action='store_true',
help='ask for privilege escalation password') help='ask for privilege escalation password')

@ -64,10 +64,12 @@ class PullCLI(CLI):
subset_opts=True, subset_opts=True,
inventory_opts=True, inventory_opts=True,
module_opts=True, module_opts=True,
runas_prompt_opts=True,
) )
# options unique to pull # options unique to pull
self.parser.add_option('--purge', default=False, action='store_true', help='purge checkout after playbook run') self.parser.add_option('--purge', default=False, action='store_true',
help='purge checkout after playbook run')
self.parser.add_option('-o', '--only-if-changed', dest='ifchanged', default=False, action='store_true', self.parser.add_option('-o', '--only-if-changed', dest='ifchanged', default=False, action='store_true',
help='only run the playbook if the repository has been updated') help='only run the playbook if the repository has been updated')
self.parser.add_option('-s', '--sleep', dest='sleep', default=None, self.parser.add_option('-s', '--sleep', dest='sleep', default=None,
@ -94,6 +96,7 @@ class PullCLI(CLI):
hostname = socket.getfqdn() hostname = socket.getfqdn()
# use a hostname dependent directory, in case of $HOME on nfs # use a hostname dependent directory, in case of $HOME on nfs
self.options.dest = os.path.join('~/.ansible/pull', hostname) self.options.dest = os.path.join('~/.ansible/pull', hostname)
self.options.dest = os.path.expandvars(os.path.expanduser(self.options.dest))
if self.options.sleep: if self.options.sleep:
try: try:
@ -126,7 +129,7 @@ class PullCLI(CLI):
node = platform.node() node = platform.node()
host = socket.getfqdn() host = socket.getfqdn()
limit_opts = 'localhost,%s,127.0.0.1' % ','.join(set([host, node, host.split('.')[0], node.split('.')[0]])) limit_opts = 'localhost,%s,127.0.0.1' % ','.join(set([host, node, host.split('.')[0], node.split('.')[0]]))
base_opts = '-c local "%s"' % limit_opts base_opts = '-c local '
if self.options.verbosity > 0: if self.options.verbosity > 0:
base_opts += ' -%s' % ''.join([ "v" for x in range(0, self.options.verbosity) ]) base_opts += ' -%s' % ''.join([ "v" for x in range(0, self.options.verbosity) ])
@ -137,7 +140,7 @@ class PullCLI(CLI):
else: else:
inv_opts = self.options.inventory inv_opts = self.options.inventory
#TODO: enable more repo modules hg/svn? #FIXME: enable more repo modules hg/svn?
if self.options.module_name == 'git': if self.options.module_name == 'git':
repo_opts = "name=%s dest=%s" % (self.options.url, self.options.dest) repo_opts = "name=%s dest=%s" % (self.options.url, self.options.dest)
if self.options.checkout: if self.options.checkout:
@ -157,8 +160,8 @@ class PullCLI(CLI):
raise AnsibleOptionsError(("module '%s' not found.\n" % self.options.module_name)) raise AnsibleOptionsError(("module '%s' not found.\n" % self.options.module_name))
bin_path = os.path.dirname(os.path.abspath(sys.argv[0])) bin_path = os.path.dirname(os.path.abspath(sys.argv[0]))
cmd = '%s/ansible -i "%s" %s -m %s -a "%s"' % ( cmd = '%s/ansible -i "%s" %s -m %s -a "%s" "%s"' % (
bin_path, inv_opts, base_opts, self.options.module_name, repo_opts bin_path, inv_opts, base_opts, self.options.module_name, repo_opts, limit_opts
) )
for ev in self.options.extra_vars: for ev in self.options.extra_vars:
@ -170,6 +173,8 @@ class PullCLI(CLI):
time.sleep(self.options.sleep) time.sleep(self.options.sleep)
# RUN the Checkout command # RUN the Checkout command
display.debug("running ansible with VCS module to checkout repo")
display.vvvv('EXEC: %s' % cmd)
rc, out, err = run_cmd(cmd, live=True) rc, out, err = run_cmd(cmd, live=True)
if rc != 0: if rc != 0:
@ -193,16 +198,18 @@ class PullCLI(CLI):
cmd += ' -i "%s"' % self.options.inventory cmd += ' -i "%s"' % self.options.inventory
for ev in self.options.extra_vars: for ev in self.options.extra_vars:
cmd += ' -e "%s"' % ev cmd += ' -e "%s"' % ev
if self.options.ask_sudo_pass: if self.options.ask_sudo_pass or self.options.ask_su_pass or self.options.become_ask_pass:
cmd += ' -K' cmd += ' --ask-become-pass'
if self.options.tags: if self.options.tags:
cmd += ' -t "%s"' % self.options.tags cmd += ' -t "%s"' % self.options.tags
if self.options.limit: if self.options.subset:
cmd += ' -l "%s"' % self.options.limit cmd += ' -l "%s"' % self.options.subset
os.chdir(self.options.dest) os.chdir(self.options.dest)
# RUN THE PLAYBOOK COMMAND # RUN THE PLAYBOOK COMMAND
display.debug("running ansible-playbook to do actual work")
display.debug('EXEC: %s' % cmd)
rc, out, err = run_cmd(cmd, live=True) rc, out, err = run_cmd(cmd, live=True)
if self.options.purge: if self.options.purge:

Loading…
Cancel
Save