2012-04-30 21:27:57 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# (c) 2012, Stephen Fromm <sfromm@gmail.com>
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2013-07-23 17:28:12 +00:00
|
|
|
#
|
2012-10-05 21:31:13 +00:00
|
|
|
# ansible-pull is a script that runs ansible in local mode
|
2013-07-23 17:28:12 +00:00
|
|
|
# after checking out a playbooks directory from source repo. There is an
|
2012-10-05 21:31:13 +00:00
|
|
|
# example playbook to bootstrap this script in the examples/ dir which
|
|
|
|
# installs ansible and sets it up to run on cron.
|
|
|
|
|
|
|
|
# usage:
|
|
|
|
# ansible-pull -d /var/lib/ansible \
|
|
|
|
# -U http://example.net/content.git [-C production] \
|
|
|
|
# [path/playbook.yml]
|
|
|
|
#
|
|
|
|
# the -d and -U arguments are required; the -C argument is optional.
|
|
|
|
#
|
|
|
|
# ansible-pull accepts an optional argument to specify a playbook
|
2013-07-23 17:28:12 +00:00
|
|
|
# location underneath the workdir and then searches the source repo
|
2012-10-05 21:31:13 +00:00
|
|
|
# for playbooks in the following order, stopping at the first match:
|
|
|
|
#
|
|
|
|
# 1. $workdir/path/playbook.yml, if specified
|
2013-10-13 09:42:09 +00:00
|
|
|
# 2. $workdir/$fqdn.yml
|
|
|
|
# 3. $workdir/$hostname.yml
|
|
|
|
# 4. $workdir/local.yml
|
2012-10-05 21:31:13 +00:00
|
|
|
#
|
2013-07-23 17:28:12 +00:00
|
|
|
# the source repo must contain at least one of these playbooks.
|
2012-04-30 21:27:57 +00:00
|
|
|
|
|
|
|
import os
|
2012-10-12 14:53:56 +00:00
|
|
|
import shutil
|
2012-04-30 21:27:57 +00:00
|
|
|
import sys
|
2012-10-05 21:31:13 +00:00
|
|
|
import datetime
|
2012-09-13 06:42:54 +00:00
|
|
|
import socket
|
2014-01-22 08:01:51 +00:00
|
|
|
import random
|
|
|
|
import time
|
2013-07-23 17:28:12 +00:00
|
|
|
from ansible import utils
|
2014-01-10 16:18:02 +00:00
|
|
|
from ansible.utils import cmd_functions
|
2014-02-26 19:52:57 +00:00
|
|
|
from ansible import errors
|
2014-08-01 05:58:00 +00:00
|
|
|
from ansible import inventory
|
2012-04-30 21:27:57 +00:00
|
|
|
|
2013-07-23 17:28:12 +00:00
|
|
|
DEFAULT_REPO_TYPE = 'git'
|
2012-04-30 21:27:57 +00:00
|
|
|
DEFAULT_PLAYBOOK = 'local.yml'
|
2013-06-30 21:20:58 +00:00
|
|
|
PLAYBOOK_ERRORS = {1: 'File does not exist',
|
|
|
|
2: 'File is not readable'}
|
|
|
|
|
2014-01-10 16:18:02 +00:00
|
|
|
VERBOSITY=0
|
2012-04-30 21:27:57 +00:00
|
|
|
|
2014-01-10 16:18:02 +00:00
|
|
|
def increment_debug(option, opt, value, parser):
|
|
|
|
global VERBOSITY
|
|
|
|
VERBOSITY += 1
|
2012-05-02 16:48:15 +00:00
|
|
|
|
2012-10-12 14:35:21 +00:00
|
|
|
def try_playbook(path):
|
|
|
|
if not os.path.exists(path):
|
|
|
|
return 1
|
|
|
|
if not os.access(path, os.R_OK):
|
|
|
|
return 2
|
|
|
|
return 0
|
|
|
|
|
2013-06-30 21:20:58 +00:00
|
|
|
|
2012-10-12 14:35:21 +00:00
|
|
|
def select_playbook(path, args):
|
|
|
|
playbook = None
|
|
|
|
if len(args) > 0 and args[0] is not None:
|
|
|
|
playbook = "%s/%s" % (path, args[0])
|
|
|
|
rc = try_playbook(playbook)
|
|
|
|
if rc != 0:
|
|
|
|
print >>sys.stderr, "%s: %s" % (playbook, PLAYBOOK_ERRORS[rc])
|
|
|
|
return None
|
|
|
|
return playbook
|
|
|
|
else:
|
2013-10-13 09:20:47 +00:00
|
|
|
fqdn = socket.getfqdn()
|
|
|
|
hostpb = "%s/%s.yml" % (path, fqdn)
|
|
|
|
shorthostpb = "%s/%s.yml" % (path, fqdn.split('.')[0])
|
2012-10-12 14:35:21 +00:00
|
|
|
localpb = "%s/%s" % (path, DEFAULT_PLAYBOOK)
|
|
|
|
errors = []
|
2013-10-13 09:20:47 +00:00
|
|
|
for pb in [hostpb, shorthostpb, localpb]:
|
2012-10-12 14:35:21 +00:00
|
|
|
rc = try_playbook(pb)
|
|
|
|
if rc == 0:
|
|
|
|
playbook = pb
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
errors.append("%s: %s" % (pb, PLAYBOOK_ERRORS[rc]))
|
|
|
|
if playbook is None:
|
|
|
|
print >>sys.stderr, "\n".join(errors)
|
|
|
|
return playbook
|
|
|
|
|
2013-06-30 21:20:58 +00:00
|
|
|
|
2012-04-30 21:27:57 +00:00
|
|
|
def main(args):
|
|
|
|
""" Set up and run a local playbook """
|
2012-10-12 14:35:21 +00:00
|
|
|
usage = "%prog [options] [playbook.yml]"
|
2013-07-23 17:28:12 +00:00
|
|
|
parser = utils.SortedOptParser(usage=usage)
|
2012-10-12 14:53:56 +00:00
|
|
|
parser.add_option('--purge', default=False, action='store_true',
|
2013-07-23 17:28:12 +00:00
|
|
|
help='purge checkout after playbook run')
|
2013-06-30 21:20:58 +00:00
|
|
|
parser.add_option('-o', '--only-if-changed', dest='ifchanged', default=False, action='store_true',
|
2013-06-30 23:31:37 +00:00
|
|
|
help='only run the playbook if the repository has been updated')
|
2014-01-22 08:01:51 +00:00
|
|
|
parser.add_option('-s', '--sleep', dest='sleep', default=None,
|
|
|
|
help='sleep for random interval (between 0 and n number of seconds) before starting. this is a useful way to disperse git requests')
|
2013-09-03 20:39:19 +00:00
|
|
|
parser.add_option('-f', '--force', dest='force', default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='run the playbook even if the repository could '
|
|
|
|
'not be updated')
|
2012-04-30 21:27:57 +00:00
|
|
|
parser.add_option('-d', '--directory', dest='dest', default=None,
|
2013-07-23 17:28:12 +00:00
|
|
|
help='directory to checkout repository to')
|
2014-01-10 16:18:02 +00:00
|
|
|
#parser.add_option('-l', '--live', default=True, action='store_live',
|
|
|
|
# help='Print the ansible-playbook output while running')
|
2012-10-05 21:31:13 +00:00
|
|
|
parser.add_option('-U', '--url', dest='url', default=None,
|
2013-07-23 17:28:12 +00:00
|
|
|
help='URL of the playbook repository')
|
2012-05-02 16:48:15 +00:00
|
|
|
parser.add_option('-C', '--checkout', dest='checkout',
|
2013-07-23 17:28:12 +00:00
|
|
|
help='branch/tag/commit to checkout. '
|
|
|
|
'Defaults to behavior of repository module.')
|
2013-05-21 05:25:01 +00:00
|
|
|
parser.add_option('-i', '--inventory-file', dest='inventory',
|
2013-06-27 01:57:31 +00:00
|
|
|
help="location of the inventory host file")
|
2014-02-05 18:53:37 +00:00
|
|
|
parser.add_option('-e', '--extra-vars', dest="extra_vars", action="append",
|
|
|
|
help="set additional variables as key=value or YAML/JSON", default=[])
|
2014-01-10 16:18:02 +00:00
|
|
|
parser.add_option('-v', '--verbose', default=False, action="callback",
|
|
|
|
callback=increment_debug,
|
|
|
|
help='Pass -vvvv to ansible-playbook')
|
2013-07-23 17:28:12 +00:00
|
|
|
parser.add_option('-m', '--module-name', dest='module_name',
|
|
|
|
default=DEFAULT_REPO_TYPE,
|
|
|
|
help='Module name used to check out repository. '
|
|
|
|
'Default is %s.' % DEFAULT_REPO_TYPE)
|
2014-02-26 19:52:57 +00:00
|
|
|
parser.add_option('--vault-password-file', dest='vault_password_file',
|
|
|
|
help="vault password file")
|
2014-02-03 20:20:04 +00:00
|
|
|
parser.add_option('-K', '--ask-sudo-pass', default=False, dest='ask_sudo_pass', action='store_true',
|
|
|
|
help='ask for sudo password')
|
2014-11-10 21:56:38 +00:00
|
|
|
parser.add_option('-t', '--tags', dest='tags', default=False,
|
|
|
|
help='only run plays and tasks tagged with these values')
|
2014-03-23 20:23:02 +00:00
|
|
|
parser.add_option('--accept-host-key', default=False, dest='accept_host_key', action='store_true',
|
|
|
|
help='adds the hostkey for the repo url if not already added')
|
|
|
|
parser.add_option('--key-file', dest='key_file',
|
|
|
|
help="Pass '-i <key_file>' to the SSH arguments used by git.")
|
2012-04-30 21:27:57 +00:00
|
|
|
options, args = parser.parse_args(args)
|
|
|
|
|
2013-10-16 07:03:43 +00:00
|
|
|
hostname = socket.getfqdn()
|
2012-10-05 21:31:13 +00:00
|
|
|
if not options.dest:
|
2013-10-16 07:03:43 +00:00
|
|
|
# use a hostname dependent directory, in case of $HOME on nfs
|
|
|
|
options.dest = utils.prepare_writeable_dir('~/.ansible/pull/%s' % hostname)
|
2012-10-05 21:31:13 +00:00
|
|
|
|
2013-07-05 17:57:01 +00:00
|
|
|
options.dest = os.path.abspath(options.dest)
|
|
|
|
|
2012-10-05 21:31:13 +00:00
|
|
|
if not options.url:
|
2013-07-23 17:28:12 +00:00
|
|
|
parser.error("URL for repository not specified, use -h for help")
|
2012-10-12 14:35:21 +00:00
|
|
|
return 1
|
2012-10-05 21:31:13 +00:00
|
|
|
|
|
|
|
now = datetime.datetime.now()
|
2015-01-30 14:55:37 +00:00
|
|
|
print now.strftime("Starting ansible-pull at %F %T")
|
2012-10-05 21:31:13 +00:00
|
|
|
|
2014-08-01 05:58:00 +00:00
|
|
|
# Attempt to use the inventory passed in as an argument
|
|
|
|
# It might not yet have been downloaded so use localhost if note
|
|
|
|
if not options.inventory or not os.path.exists(options.inventory):
|
2014-06-04 21:37:41 +00:00
|
|
|
inv_opts = 'localhost,'
|
|
|
|
else:
|
|
|
|
inv_opts = options.inventory
|
2013-10-16 07:03:43 +00:00
|
|
|
limit_opts = 'localhost:%s:127.0.0.1' % hostname
|
2013-07-23 17:28:12 +00:00
|
|
|
repo_opts = "name=%s dest=%s" % (options.url, options.dest)
|
2014-01-10 16:18:02 +00:00
|
|
|
|
|
|
|
if VERBOSITY == 0:
|
|
|
|
base_opts = '-c local --limit "%s"' % limit_opts
|
|
|
|
elif VERBOSITY > 0:
|
|
|
|
debug_level = ''.join([ "v" for x in range(0, VERBOSITY) ])
|
|
|
|
base_opts = '-%s -c local --limit "%s"' % (debug_level, limit_opts)
|
|
|
|
|
2013-07-23 17:28:12 +00:00
|
|
|
if options.checkout:
|
|
|
|
repo_opts += ' version=%s' % options.checkout
|
2014-03-23 20:23:02 +00:00
|
|
|
|
|
|
|
# Only git module is supported
|
|
|
|
if options.module_name == DEFAULT_REPO_TYPE:
|
|
|
|
if options.accept_host_key:
|
|
|
|
repo_opts += ' accept_hostkey=yes'
|
|
|
|
|
|
|
|
if options.key_file:
|
|
|
|
repo_opts += ' key_file=%s' % options.key_file
|
|
|
|
|
2013-07-23 17:28:12 +00:00
|
|
|
path = utils.plugins.module_finder.find_plugin(options.module_name)
|
|
|
|
if path is None:
|
|
|
|
sys.stderr.write("module '%s' not found.\n" % options.module_name)
|
|
|
|
return 1
|
2015-03-17 21:25:45 +00:00
|
|
|
|
|
|
|
bin_path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
cmd = '%s/ansible localhost -i "%s" %s -m %s -a "%s"' % (
|
|
|
|
bin_path, inv_opts, base_opts, options.module_name, repo_opts
|
2013-05-21 05:25:01 +00:00
|
|
|
)
|
2015-03-17 21:25:45 +00:00
|
|
|
|
2014-09-18 20:27:09 +00:00
|
|
|
for ev in options.extra_vars:
|
|
|
|
cmd += ' -e "%s"' % ev
|
2014-01-10 16:18:02 +00:00
|
|
|
|
2014-01-22 08:01:51 +00:00
|
|
|
if options.sleep:
|
|
|
|
try:
|
|
|
|
secs = random.randint(0,int(options.sleep));
|
|
|
|
except ValueError:
|
|
|
|
parser.error("%s is not a number." % options.sleep)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
print >>sys.stderr, "Sleeping for %d seconds..." % secs
|
|
|
|
time.sleep(secs);
|
|
|
|
|
|
|
|
|
|
|
|
# RUN THe CHECKOUT COMMAND
|
2014-01-10 16:18:02 +00:00
|
|
|
rc, out, err = cmd_functions.run_cmd(cmd, live=True)
|
|
|
|
|
2012-09-13 00:07:02 +00:00
|
|
|
if rc != 0:
|
2013-09-03 20:39:19 +00:00
|
|
|
if options.force:
|
2015-01-30 14:55:37 +00:00
|
|
|
print >>sys.stderr, "Unable to update repository. Continuing with (forced) run of playbook."
|
2013-09-03 20:39:19 +00:00
|
|
|
else:
|
|
|
|
return rc
|
2013-06-30 21:20:58 +00:00
|
|
|
elif options.ifchanged and '"changed": true' not in out:
|
|
|
|
print "Repository has not changed, quitting."
|
|
|
|
return 0
|
2012-09-13 00:07:02 +00:00
|
|
|
|
2012-10-12 14:35:21 +00:00
|
|
|
playbook = select_playbook(options.dest, args)
|
|
|
|
|
|
|
|
if playbook is None:
|
|
|
|
print >>sys.stderr, "Could not find a playbook to run."
|
|
|
|
return 1
|
2012-10-05 21:31:13 +00:00
|
|
|
|
2015-03-17 21:25:45 +00:00
|
|
|
cmd = '%s/ansible-playbook %s %s' % (bin_path, base_opts, playbook)
|
2014-02-26 19:52:57 +00:00
|
|
|
if options.vault_password_file:
|
|
|
|
cmd += " --vault-password-file=%s" % options.vault_password_file
|
2013-05-21 05:25:01 +00:00
|
|
|
if options.inventory:
|
|
|
|
cmd += ' -i "%s"' % options.inventory
|
2014-02-05 18:53:37 +00:00
|
|
|
for ev in options.extra_vars:
|
|
|
|
cmd += ' -e "%s"' % ev
|
2014-02-03 20:20:04 +00:00
|
|
|
if options.ask_sudo_pass:
|
|
|
|
cmd += ' -K'
|
2014-11-10 21:56:38 +00:00
|
|
|
if options.tags:
|
|
|
|
cmd += ' -t "%s"' % options.tags
|
2012-04-30 21:27:57 +00:00
|
|
|
os.chdir(options.dest)
|
2014-01-10 16:18:02 +00:00
|
|
|
|
|
|
|
# RUN THE PLAYBOOK COMMAND
|
|
|
|
rc, out, err = cmd_functions.run_cmd(cmd, live=True)
|
2012-10-12 14:53:56 +00:00
|
|
|
|
|
|
|
if options.purge:
|
|
|
|
os.chdir('/')
|
|
|
|
try:
|
|
|
|
shutil.rmtree(options.dest)
|
|
|
|
except Exception, e:
|
|
|
|
print >>sys.stderr, "Failed to remove %s: %s" % (options.dest, str(e))
|
|
|
|
|
2012-05-02 16:48:15 +00:00
|
|
|
return rc
|
2012-04-30 21:27:57 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
sys.exit(main(sys.argv[1:]))
|
2012-05-02 16:48:15 +00:00
|
|
|
except KeyboardInterrupt, e:
|
|
|
|
print >>sys.stderr, "Exit on user request.\n"
|
2012-04-30 21:27:57 +00:00
|
|
|
sys.exit(1)
|