Skip to content

Instantly share code, notes, and snippets.

@jctanner
Last active January 2, 2016 01:18
Show Gist options
  • Save jctanner/8229044 to your computer and use it in GitHub Desktop.
Save jctanner/8229044 to your computer and use it in GitHub Desktop.
CELERY + REDIS EXAMPLE
#!/usr/bin/python
####################
# ansiblecaller.py
####################
import os
import sys
import subprocess
import shlex
import tempfile
from celery import Celery
from ansible.playbook import PlayBook
from ansible.runner import Runner
from ansible.inventory import Inventory
from ansible.callbacks import PlaybookRunnerCallbacks
from ansible.callbacks import AggregateStats
from ansible.callbacks import PlaybookCallbacks
from ansible.errors import AnsibleError
from ansible.color import ANSIBLE_COLOR, stringc
from ansible import utils
BROKER_URL = 'redis://localhost:6379/0'
BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600} # 1 hour.
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
app = Celery('tasks', broker=BROKER_URL, backend=BROKER_URL)
@app.task
def ansible_playbook(inventory_file, playbook_file):
#return "inv=%s book=%s" % (inventory_file, playbook_file)
cmdargs = "ansible-playbook -vvvv -i %s %s" % (inventory_file, playbook_file)
cmdargs = shlex.split(cmdargs)
p = subprocess.Popen(cmdargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
return stdout, stderr
def ansible_lib_play(inventory_file, playbook_file):
#from ansible.playbook import Playbook
#from ansible.runner import Runner
#from ansible.inventory import Inventory
#from ansible.callbacks import PlaybookRunnerCallbacks
inventory = Inventory(inventory_file)
inventory.set_playbook_basedir(os.path.dirname(playbook_file))
stats = AggregateStats()
playbook_cb = PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
pb = PlayBook(
playbook=playbook_file,
inventory=inventory,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
stats=stats
)
failed_hosts = []
unreachable_hosts = []
try:
pb.run()
hosts = sorted(pb.stats.processed.keys())
#display(callbacks.banner("PLAY RECAP"))
playbook_cb.on_stats(pb.stats)
for h in hosts:
t = pb.stats.summarize(h)
if t['failures'] > 0:
failed_hosts.append(h)
if t['unreachable'] > 0:
unreachable_hosts.append(h)
displaylines = []
stderr = ""
#return "".join(displaylines), stderr
return "foo", "bar"
except AnsibleError, e:
return stdout, e
#=========================================================================#
#!/usr/bin/python
#######################
# runplaybook.py
#######################
import epdb
from ansiblecaller import ansible_playbook
from ansiblecaller import ansible_lib_play
print "##################################"
print "# USE SUBPROCESS #"
print "##################################"
x = ansible_playbook.delay('/etc/ansible/hosts', 'site.yml')
x.get()
stdout = x.result[0]
stderr = x.result[1]
print stdout,stderr
print "##################################"
print "# USE LIBS DIRECTLY #"
print "##################################"
y = ansible_lib_play('/etc/ansible/hosts', 'site.yml')
epdb.st()
y.get()
stdout = y.result[0]
stderr = y.result[1]
print stdout,stderr
#=============================================================#
# site.yml
- hosts: localhost
connection: local
gather_facts: False
tasks:
- debug: msg="hello world"
- shell: rm -rf /tmp/foo /tmp/bar
ignore_errors: True
- shell: touch /tmp/foo
- copy: src=/tmp/foo dest=/tmp/bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment