diff --git a/bin/ansible-pull b/bin/ansible-pull new file mode 100755 index 0000000000..b75c4631e4 --- /dev/null +++ b/bin/ansible-pull @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +# (c) 2012, Stephen Fromm +# +# 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 . + +import os +import subprocess +import sys +from optparse import OptionParser + +DEFAULT_PLAYBOOK = 'local.yml' + +def _run(cmd): + cmd = subprocess.Popen(cmd, shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (out, err) = cmd.communicate() + print out + if cmd.returncode != 0: + print err + return cmd.returncode + +def main(args): + """ Set up and run a local playbook """ + usage = "%prog [options]" + parser = OptionParser() + parser.add_option('-d', '--directory', dest='dest', default=None, + help='Directory to checkout git repository') + parser.add_option('-U', '--url', dest='url', + default=None, + help='URL of git repository') + parser.add_option('-C', '--checkout', dest='checkout', + default="HEAD", + help='Branch/Tag/Commit to checkout. Defaults to HEAD.') + options, args = parser.parse_args(args) + + git_opts = "repo=%s dest=%s version=%s" % (options.url, options.dest, options.checkout) + cmd = 'ansible all -c local -m git -a "%s"' % git_opts + rc = _run(cmd) + if rc != 0: + return rc + + os.chdir(options.dest) + cmd = 'ansible-playbook -c local %s' % DEFAULT_PLAYBOOK + rc = _run(cmd) + return rc + +if __name__ == '__main__': + try: + sys.exit(main(sys.argv[1:])) + except KeyboardInterrupt, e: + print >>sys.stderr, "Exit on user request.\n" + sys.exit(1) diff --git a/examples/playbooks/ansible_pull.yml b/examples/playbooks/ansible_pull.yml new file mode 100644 index 0000000000..8a7e61daf4 --- /dev/null +++ b/examples/playbooks/ansible_pull.yml @@ -0,0 +1,22 @@ +--- +- hosts: all + user: root + vars: + # schdule is fed directly to cron + schedule: '*/15 * * * *' + # User to run ansible-pull as from cron + cron_user: root + # Directory to where repository will be cloned + workdir: /var/lib/ansible/local + # Repository to check out + repo_url: git://github.com/sfromm/ansible-playbooks.git + tasks: + - name: Install ansible + action: yum pkg=ansible state=installed + - name: Create local directory to work from + action: file path=$workdir state=directory owner=root group=root mode=0751 + - name: Copy ansible inventory file to client + action: copy src=/etc/ansible/hosts dest=/etc/ansible/hosts + owner=root group=root mode=0644 + - name: Create crontab entry to clone/pull git repository + action: template src=templates/ansible-pull.j2 dest=/etc/cron.d/ansible-pull owner=root group=root mode=0644 diff --git a/examples/playbooks/templates/ansible-pull.j2 b/examples/playbooks/templates/ansible-pull.j2 new file mode 100644 index 0000000000..bab3f3f73d --- /dev/null +++ b/examples/playbooks/templates/ansible-pull.j2 @@ -0,0 +1,2 @@ +# Cron job to git clone/pull a repo and then run locally +{{ schedule }} {{ cron_user }} ansible-pull -d {{ workdir }} -U {{ repo_url }}