diff --git a/bin/ansible-pull b/bin/ansible-pull index 2c0e4395e25..06ed41d0d69 100755 --- a/bin/ansible-pull +++ b/bin/ansible-pull @@ -45,8 +45,9 @@ import socket from optparse import OptionParser DEFAULT_PLAYBOOK = 'local.yml' -PLAYBOOK_ERRORS = { 1: 'File does not exist', - 2: 'File is not readable' } +PLAYBOOK_ERRORS = {1: 'File does not exist', + 2: 'File is not readable'} + def _run(cmd): print >>sys.stderr, "Running: '%s'" % cmd @@ -56,7 +57,8 @@ def _run(cmd): print out if cmd.returncode != 0: print >>sys.stderr, err - return cmd.returncode + return cmd.returncode, out + def try_playbook(path): if not os.path.exists(path): @@ -65,6 +67,7 @@ def try_playbook(path): return 2 return 0 + def select_playbook(path, args): playbook = None if len(args) > 0 and args[0] is not None: @@ -89,12 +92,15 @@ def select_playbook(path, args): print >>sys.stderr, "\n".join(errors) return playbook + def main(args): """ Set up and run a local playbook """ usage = "%prog [options] [playbook.yml]" parser = OptionParser(usage=usage) parser.add_option('--purge', default=False, action='store_true', help='Purge git checkout after playbook run') + 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') parser.add_option('-d', '--directory', dest='dest', default=None, help='Directory to clone git repository to') parser.add_option('-U', '--url', dest='url', default=None, @@ -123,9 +129,12 @@ def main(args): cmd = 'ansible all -c local -i "%s" --limit "%s" -m git -a "%s"' % ( inv_opts, limit_opts, git_opts ) - rc = _run(cmd) + rc, out = _run(cmd) if rc != 0: return rc + elif options.ifchanged and '"changed": true' not in out: + print "Repository has not changed, quitting." + return 0 playbook = select_playbook(options.dest, args) @@ -137,7 +146,7 @@ def main(args): if options.inventory: cmd += ' -i "%s"' % options.inventory os.chdir(options.dest) - rc = _run(cmd) + rc, out = _run(cmd) if options.purge: os.chdir('/')