<commit_before>#!/usr/bin/env python
# curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
# python get-pip.py

import argparse
import pprint
import sys
import os
import json
from ansible.playbook import PlayBook
import ansible.inventory
from ansible import callbacks
from ansible import utils
import re

#logging.basicConfig()
#LOG = logging.getLogger(__name__)
#LOG.setLevel(logging.DEBUG)  # JPEELER: change to INFO later

def argParser():
    parser = argparse.ArgumentParser(description='Clapper')

    # parser.add_argument('-h', '--help',
    #                     help='List help',
    #                     action='store_true')

    return vars(parser.parse_args())

output_data = {}


class CustomHandler(ansible.callbacks.PlaybookRunnerCallbacks):
    def __init__(self, stats=None):
        print 'init'
        super(CustomHandler, self).__init__(stats=stats)

    def on_ok(self, host, host_result):
        global output_data
        #print('host: %s' % host)
        #print('host_result: %s' % host_result)
        if host not in output_data:
            output_data[host] = []
        output_data[host].append(host_result)
        output_data['asdf'] = 1
        #print 'output_data: %s' % output_data


def main():
    args = argParser()

    #group = ansible.inventory.group.Group(name='nodes')

    hostfile = open('hosts', 'w')
    output = os.popen('nova list').readlines()
    for line in output:
        m = re.search('\d+\.\d+\.\d+\.\d+', line)
        if m:
            ip = m.group(0)
            hostfile.write(ip)
            hostfile.write('\n')

    hostfile.close()

    stats = ansible.callbacks.AggregateStats()
    pb_callback = ansible.callbacks.PlaybookCallbacks()
    run_callback = CustomHandler(stats=stats)
    #run_callback = ansible.callbacks.PlaybookRunnerCallbacks(stats=stats)

    p = ansible.playbook.PlayBook(playbook='playbook.yml',
                                  remote_user='heat-admin',
                                  host_list='hosts',
                                  stats=stats,
                                  callbacks=pb_callback,
                                  runner_callbacks=run_callback)
    output = p.run()

    f = open('ansible_run.json', 'w')
    f.write(json.dumps(output, sort_keys=True, indent=4))<commit_msg>Capture the task output correctly<commit_after>#!/usr/bin/env python
# curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
# python get-pip.py

import argparse
import pprint
import sys
import os
import json
from ansible.playbook import PlayBook
import ansible.inventory
from ansible import callbacks
from ansible import utils
import re

#logging.basicConfig()
#LOG = logging.getLogger(__name__)
#LOG.setLevel(logging.DEBUG)  # JPEELER: change to INFO later

def argParser():
    parser = argparse.ArgumentParser(description='Clapper')

    # parser.add_argument('-h', '--help',
    #                     help='List help',
    #                     action='store_true')

    return vars(parser.parse_args())

output_data = {}


class CustomHandler(ansible.callbacks.PlaybookRunnerCallbacks):
    def __init__(self, stats=None):
        print 'init'
        super(CustomHandler, self).__init__(stats=stats)

    def on_ok(self, host, host_result):
        global output_data
        #print('host: %s' % host)
        #print('host_result: %s' % host_result)
        if host not in output_data:
            output_data[host] = []
        output_data[host].append(host_result)
        #output_data['asdf'] = 1
        #print 'output_data: %s' % output_data


def main():
    args = argParser()

    #group = ansible.inventory.group.Group(name='nodes')

    hostfile = open('hosts', 'w')
    output = os.popen('nova list').readlines()
    for line in output:
        m = re.search('\d+\.\d+\.\d+\.\d+', line)
        if m:
            ip = m.group(0)
            hostfile.write(ip)
            hostfile.write('\n')

    hostfile.close()

    stats = ansible.callbacks.AggregateStats()
    pb_callback = ansible.callbacks.PlaybookCallbacks()
    run_callback = CustomHandler(stats=stats)
    #run_callback = ansible.callbacks.PlaybookRunnerCallbacks(stats=stats)

    p = ansible.playbook.PlayBook(playbook='playbook.yml',
                                  remote_user='heat-admin',
                                  host_list='hosts',
                                  forks=1,
                                  stats=stats,
                                  callbacks=pb_callback,
                                  runner_callbacks=run_callback)
    output = p.run()

    f = open('ansible_run.json', 'w')
    f.write(json.dumps(output, sort_keys=True, indent=4))
{'fraction_non_alphanumeric': 0.12360754070265638, 'fraction_numerical': 0.0014995715509854327, 'mean_word_length': 2.335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7450737', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1411, 'n_words': 280}