Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --only-if-changed option. #3386

Merged
merged 1 commit into from
Jun 30, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions bin/ansible-pull
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand All @@ -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('/')
Expand Down