Skip to content

Instantly share code, notes, and snippets.

@rajasaur
Created January 3, 2014 04:03
Show Gist options
  • Save rajasaur/8232504 to your computer and use it in GitHub Desktop.
Save rajasaur/8232504 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#################################
# test.py (Playbook Execute Task)
#################################
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 import utils
app = Celery('test', backend='amqp', broker='amqp://')
@app.task
def run_pb(playbook_file):
inventory = Inventory(['localhost'])
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())
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
###############################
# runner.py (The celery runner)
###############################
from test import run_pb
output = run_pb.delay('site.yml')
output.get()
print output.result[0], output.result[1]
##########
# 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
# NOTE: I use AMQP as the broker, so had to invoke celery like "celery worker -A test"
# On Executing python runner.py, I get the output as "foo bar", but on the Celery process get this error (Using Ansible Master)
[2014-01-03 09:28:08,136: WARNING/MainProcess] celery@amethyst ready.
[2014-01-03 09:28:08,414: WARNING/Worker-4] PLAY [localhost] **************************************************************
[2014-01-03 09:28:08,415: WARNING/Worker-4] TASK: [debug msg="hello world"] ***********************************************
[2014-01-03 09:28:08,417: WARNING/Worker-4] ok: [localhost] => {
"msg": "hello world"
}
[2014-01-03 09:28:08,417: WARNING/Worker-4] TASK: [shell rm -rf /tmp/foo /tmp/bar] ****************************************
[2014-01-03 09:28:08,479: WARNING/Worker-4] changed: [localhost]
[2014-01-03 09:28:08,481: WARNING/Worker-4] TASK: [shell touch /tmp/foo] **************************************************
[2014-01-03 09:28:08,546: WARNING/Worker-4] changed: [localhost]
[2014-01-03 09:28:08,548: WARNING/Worker-4] TASK: [copy src=/tmp/foo dest=/tmp/bar] ***************************************
fatal: [localhost] => Traceback (most recent call last):
File "/sources/projects/virtualenvs/ansiblebug/ansible/lib/ansible/runner/__init__.py", line 409, in _executor
self._new_stdin = os.fdopen(os.dup(sys.stdin.fileno()))
ValueError: I/O operation on closed file
FATAL: all hosts have already failed -- aborting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment