Exemple #1
0
    def run(self, extra_vars=None):
        """
        run ansible playbook, only surport relational path.
        Args:
            extra_vars: playbook extra variables.
        """
        task_results = []
        stats = CustomAggregateStats(task_results)
        callbacks.display = self.display
        playbook_cb = CustomePlaybookCallbacks(verbose=utils.VERBOSITY,
                                               task_results=task_results)
        runner_cb = PlaybookRunnerCallbacks(stats=stats,
                                            verbose=utils.VERBOSITY)
        playbook_path = os.path.join(PLAYBOOK_DIR, self.playbook_path)

        pb = PlayBook(playbook=playbook_path,
                      stats=stats,
                      callbacks=playbook_cb,
                      runner_callbacks=runner_cb,
                      inventory=self.inventory,
                      extra_vars=extra_vars,
                      module_path=C.DEFAULT_MODULE_PATH,
                      check=False)

        self.results_raw = pb.run()
        self.count += 1
        self.save(self.results_raw)
        return self.results_raw
Exemple #2
0
def aws(phase):
   #parse args
   with open("keys/rootkey.csv","r") as keyfile:
      lines = keyfile.readlines()
      aws_access_key = lines[0].split('=')[1].strip(' ').rstrip()
      aws_secret_key = lines[1].split('=')[1].strip(' ').rstrip()

   os.environ['AWS_ACCESS_KEY_ID']= aws_access_key
   os.environ['AWS_SECRET_ACCESS_KEY']= aws_secret_key
   
   d = deployment()
   red = redis.StrictRedis(host='localhost', port=6379, db=0)
   if phase == 0:
      print "Clean up stale reservations...*****************\n"
      d.destroy_aws_instance()
   if phase == 1:
      d.create_aws_instance()
      d.create_aws_instance()
      print "\nCheck AWS instance status...******************"
      aws_ip = d.get_aws_reservation()
      while aws_ip == None or len(aws_ip) < 2:
         print "AWS Instance not ready, retry after 30 sec"
         time.sleep(30)
         aws_ip = d.get_aws_reservation()
      canary = aws_ip[0]
      production = aws_ip[1]
      print "AWS Canary Instance =" + canary
      print "AWS Production Instance =" + production
      print "Update Redis"
      red.set('canary',"http://"+canary+":3000")
      red.set('production', "http://"+production+":3000")
      red.set('production0', "http://"+production+":3000")
      red.set('instances',1);
      print red.get('canary')
      print red.get('production')
      print "\nWriting Inventory...**************************"
      aws_inv_can = "canary ansible_ssh_host="+canary+" ansible_ssh_user=ubuntu ansible_ssh_private_key_file=./keys/devops.pem\n"
      with open("inventory_canary","w") as f:
         f.write(aws_inv_can)
      aws_inv_prod = "production ansible_ssh_host="+production+" ansible_ssh_user=ubuntu ansible_ssh_private_key_file=./keys/devops.pem\n"
      with open("inventory_production","w") as f:
         f.write(aws_inv_prod)
      with open("inventory", "w") as f:
         f.write(aws_inv_can+"\n")
         f.write(aws_inv_prod)
   if phase == 2:
      os.environ['ANSIBLE_HOST_KEY_CHECKING']="false"
      utils.VERBOSITY = 0
      playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
      stats = callbacks.AggregateStats()
      runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
      inventory = Inventory('inventory')
      print "\nRun Ansible PlayBook...**********************"
      pb = PlayBook(playbook='server_play.yml',
              inventory=inventory,
              callbacks=playbook_cb,
              runner_callbacks=runner_cb,
              stats=stats
           )
      pb.run()	
def RunRecepie(inventory, playbook_file):
    """
    Run playbook

    :param hosts:
    :return:
    """
    # Boilerplace callbacks for stdout/stderr and log output
    utils.VERBOSITY = 0
    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    stats = callbacks.AggregateStats()
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

    pb = PlayBook(
        playbook=playbook_file,
        inventory=inventory,
        callbacks=playbook_cb,
        runner_callbacks=runner_cb,
        stats=stats,
    )

    results = pb.run()

    # Ensure on_stats callback is called
    # for callback modules
    playbook_cb.on_stats(pb.stats)

    return results
Exemple #4
0
def gather_facts(host, inventory=None):
    if inventory is None:
        inventory = get_inventory()

    # Gather facts
    try:

        # ... temporary playbook file
        playbook_file = tempfile.NamedTemporaryFile()
        playbook_file.write(SETUP_PLAYBOOK.format(host=host))
        playbook_file.seek(0)

        # ... run setup module
        stats = ansible.callbacks.AggregateStats()
        # callbacks = ansible.callbacks.PlaybookCallbacks(verbose=VERBOSITY)
        # runner_callbacks = ansible.callbacks.PlaybookRunnerCallbacks(
        #     stats, verbose=VERBOSITY)
        playbook = PlayBook(
            playbook=playbook_file.name,
            inventory=inventory,
            callbacks=Callbacks(),
            runner_callbacks=Callbacks(),
            stats=stats,
        )
        playbook.run()

    finally:
        playbook_file.close()

    return playbook.SETUP_CACHE
def run_playbook(dir, extra_vars):
    utils.VERBOSITY = 0
    playbook_cb = MyPlaybookCallbacks(verbose=utils.VERBOSITY)
    stats = callbacks.AggregateStats()
    runner_cb = MyPlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

    inventory = """
[localhost]
localhost ansible_connection=local
"""

    # Create a temporary file and write the template string to it
    hosts = tempfile.NamedTemporaryFile(delete=False, dir=dir)
    hosts.write(inventory)
    hosts.close()

    pb = PlayBook(
        playbook=os.path.join(dir, "playbook.yml"),
        host_list=hosts.name,
        callbacks=playbook_cb,
        runner_callbacks=runner_cb,
        extra_vars=extra_vars,
        stats=stats
    )

    results = pb.run()

    # Ensure on_stats callback is called
    # for callback modules
    playbook_cb.on_stats(pb.stats)
    logger().info(results)
def gather_facts(host, inventory=None, user=None):
    if inventory is None:
        inventory = get_inventory()

    # Gather facts
    try:

        # ... temporary playbook file
        playbook_file = tempfile.NamedTemporaryFile()
        playbook_file.write(SETUP_PLAYBOOK.format(host=host))
        playbook_file.seek(0)

        # ... run setup module
        stats = ansible.callbacks.AggregateStats()
        playbook = PlayBook(
            playbook=playbook_file.name,
            inventory=inventory,
            callbacks=Callbacks(),
            runner_callbacks=Callbacks(),
            remote_user=user or C.DEFAULT_REMOTE_USER,
            stats=stats,
        )
        results = playbook.run()

        # ... notify the user of failures
        for host, result in results.iteritems():
            if result.get('unreachable') or result.get('failures'):
                yellow('Unable to gather facts for host "{}"'.format(host))

    finally:
        playbook_file.close()

    return playbook.SETUP_CACHE
Exemple #7
0
def runPlayBook(path):
    stats = callbacks.AggregateStats()
    inven = Inventory(host_list=path["hosts"]);
    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
    pb = PlayBook(inventory=inven,playbook=path["playbook"],stats=stats,callbacks=playbook_cb,runner_callbacks=runner_cb,)
    pb.run()
Exemple #8
0
def run_installer(user_list, package_list, sudo_password):
    """
    Runs the playbook `installer.yml` with the supplied parameters
    """

    # Create the inventory
    controller = Host(name="localhost")
    controller.set_variable('users', user_list)
    controller.set_variable('apt_packages', package_list)
    local_inventory = Inventory([])
    local_inventory.get_group('all').add_host(controller)

    # Boilerplate for callbacks setup
    utils.VERBOSITY = 0
    # Output callbacks setup
    output_callbacks = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    # API callbacks setup
    stats = callbacks.AggregateStats()
    api_callbacks = callbacks.PlaybookRunnerCallbacks(stats,
                                                      verbose=utils.VERBOSITY)

    provision_playbook = PlayBook(playbook="installer.yml",
                                  stats=stats,
                                  callbacks=output_callbacks,
                                  runner_callbacks=api_callbacks,
                                  inventory=local_inventory,
                                  transport="local",
                                  become_pass=sudo_password)
    playbook_result = provision_playbook.run()
    return playbook_result
def gather_facts(host, inventory=None):
    if inventory is None:
        inventory = get_inventory()

    # Gather facts
    try:

        # ... temporary playbook file
        playbook_file = tempfile.NamedTemporaryFile()
        playbook_file.write(SETUP_PLAYBOOK.format(host=host))
        playbook_file.seek(0)

        # ... run setup module
        stats = ansible.callbacks.AggregateStats()
        # callbacks = ansible.callbacks.PlaybookCallbacks(verbose=VERBOSITY)
        # runner_callbacks = ansible.callbacks.PlaybookRunnerCallbacks(
        #     stats, verbose=VERBOSITY)
        playbook = PlayBook(
            playbook=playbook_file.name,
            inventory=inventory,
            callbacks=Callbacks(),
            runner_callbacks=Callbacks(),
            stats=stats,
        )
        playbook.run()

    finally:
        playbook_file.close()

    return playbook.SETUP_CACHE
def ansible_book(db, pb, args):
    playbook_cp = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    stats = callbacks.AggregateStats()
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats,
                                                  verbose=utils.VERBOSITY)

    playbook = PlayBook(playbook=pb,
                        extra_vars=args,
                        callbacks=playbook_cp,
                        stats=stats,
                        runner_callbacks=runner_cb,
                        inventory=autils.get_inventory(db))

    try:
        results = playbook.run()
    except:
        logger.error(traceback.format_exc())
        results = {
            'error': {
                'unreachable': 0,
                'skipped': 0,
                'ok': 0,
                'changed': 0,
                'failures': 1
            }
        }
    return results
def run_installer(user_list, package_list, sudo_password):
    """
    Runs the playbook `installer.yml` with the supplied parameters
    """

    # Create the inventory
    controller = Host(name="localhost")
    controller.set_variable("users", user_list)
    controller.set_variable("apt_packages", package_list)
    local_inventory = Inventory([])
    local_inventory.get_group("all").add_host(controller)

    # Boilerplate for callbacks setup
    utils.VERBOSITY = 0
    # Output callbacks setup
    output_callbacks = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    # API callbacks setup
    stats = callbacks.AggregateStats()
    api_callbacks = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

    provision_playbook = PlayBook(
        playbook="installer.yml",
        stats=stats,
        callbacks=output_callbacks,
        runner_callbacks=api_callbacks,
        inventory=local_inventory,
        transport="local",
        become_pass=sudo_password,
    )
    playbook_result = provision_playbook.run()
    return playbook_result
Exemple #12
0
def install_stuff(ip_list, sshpass, req_list):
    task_id = str(install_stuff.request.id)
    db.write_in_progress(req_list, ip_list, task_id)
    pb_name = helper.write_playbook(req_list)

    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    stats = callbacks.AggregateStats()
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

    pb = PlayBook(remote_user='******',
        remote_pass=sshpass,
        playbook=pb_name,
        callbacks=playbook_cb,
        runner_callbacks=runner_cb,
        stats=stats,
        host_list=ip_list,
    )
    results = pb.run()
    if results[ip_list[:-1]]['unreachable'] != 0:
        os.remove(pb_name)
        db.del_in_progress(task_id)
        raise RuntimeError('df//' + ip_list[:-1] + '//' + '1' + '//fd')

    if results[ip_list[:-1]]['failures'] != 0:
        os.remove(pb_name)
        db.del_in_progress(task_id)
        raise RuntimeError('df//' + ip_list[:-1] + '//' + '1' + '//fd')

    os.remove(pb_name)
    db.del_in_progress(task_id)
    ans = '//' + ip_list[:-1] + '//0//'
    return ans
Exemple #13
0
def install_stuff(ip_list, sshpass, req_list):
    task_id = str(install_stuff.request.id)
    db.write_in_progress(req_list, ip_list, task_id)
    pb_name = helper.write_playbook(req_list)

    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    stats = callbacks.AggregateStats()
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats,
                                                  verbose=utils.VERBOSITY)

    pb = PlayBook(
        remote_user='******',
        remote_pass=sshpass,
        playbook=pb_name,
        callbacks=playbook_cb,
        runner_callbacks=runner_cb,
        stats=stats,
        host_list=ip_list,
    )
    results = pb.run()
    if results[ip_list[:-1]]['unreachable'] != 0:
        os.remove(pb_name)
        db.del_in_progress(task_id)
        raise RuntimeError('df//' + ip_list[:-1] + '//' + '1' + '//fd')

    if results[ip_list[:-1]]['failures'] != 0:
        os.remove(pb_name)
        db.del_in_progress(task_id)
        raise RuntimeError('df//' + ip_list[:-1] + '//' + '1' + '//fd')

    os.remove(pb_name)
    db.del_in_progress(task_id)
    ans = '//' + ip_list[:-1] + '//0//'
    return ans
Exemple #14
0
def aws(phase):
    #parse args
    with open("keys/rootkey.csv", "r") as keyfile:
        lines = keyfile.readlines()
        aws_access_key = lines[0].split('=')[1].strip(' ').rstrip()
        aws_secret_key = lines[1].split('=')[1].strip(' ').rstrip()

    os.environ['AWS_ACCESS_KEY_ID'] = aws_access_key
    os.environ['AWS_SECRET_ACCESS_KEY'] = aws_secret_key

    d = deployment()
    red = redis.StrictRedis(host='localhost', port=6379, db=0)
    if phase == 0:
        print "Clean up stale reservations...*****************\n"
        d.destroy_aws_instance()
    if phase == 1:
        d.create_aws_instance()
        d.create_aws_instance()
        print "\nCheck AWS instance status...******************"
        aws_ip = d.get_aws_reservation()
        while aws_ip == None or len(aws_ip) < 2:
            print "AWS Instance not ready, retry after 30 sec"
            time.sleep(30)
            aws_ip = d.get_aws_reservation()
        canary = aws_ip[0]
        production = aws_ip[1]
        print "AWS Canary Instance =" + canary
        print "AWS Production Instance =" + production
        print "Update Redis"
        red.set('canary', "http://" + canary + ":3000")
        red.set('production', "http://" + production + ":3000")
        red.set('production0', "http://" + production + ":3000")
        red.set('instances', 1)
        print red.get('canary')
        print red.get('production')
        print "\nWriting Inventory...**************************"
        aws_inv_can = "canary ansible_ssh_host=" + canary + " ansible_ssh_user=ubuntu ansible_ssh_private_key_file=./keys/devops.pem\n"
        with open("inventory_canary", "w") as f:
            f.write(aws_inv_can)
        aws_inv_prod = "production ansible_ssh_host=" + production + " ansible_ssh_user=ubuntu ansible_ssh_private_key_file=./keys/devops.pem\n"
        with open("inventory_production", "w") as f:
            f.write(aws_inv_prod)
        with open("inventory", "w") as f:
            f.write(aws_inv_can + "\n")
            f.write(aws_inv_prod)
    if phase == 2:
        os.environ['ANSIBLE_HOST_KEY_CHECKING'] = "false"
        utils.VERBOSITY = 0
        playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        stats = callbacks.AggregateStats()
        runner_cb = callbacks.PlaybookRunnerCallbacks(stats,
                                                      verbose=utils.VERBOSITY)
        inventory = Inventory('inventory')
        print "\nRun Ansible PlayBook...**********************"
        pb = PlayBook(playbook='server_play.yml',
                      inventory=inventory,
                      callbacks=playbook_cb,
                      runner_callbacks=runner_cb,
                      stats=stats)
        pb.run()
def adduser(ips, users):
    inventory = """
    {% for i in hosts -%}
    {{ i }}
    {% endfor %}
    """
    inventory_template = jinja2.Template(inventory)
    rendered_inventory = inventory_template.render({'hosts': ips})
    hosts = NamedTemporaryFile(delete=False, suffix='tmp', dir='/tmp/ansible/')
    hosts.write(rendered_inventory)
    hosts.close()
    inventory = Inventory(hosts.name)
    stats = callbacks.AggregateStats()
    playbook_cb = callbacks.PlaybookCallbacks()
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats)
    vars = {}
    vars['users'] = users
    results = PlayBook(playbook='user.yaml',
                       callbacks=playbook_cb,
                       runner_callbacks=runner_cb,
                       stats=stats,
                       inventory=inventory,
                       extra_vars=vars)
    res = results.run()
    logs = []
    logs.append("finish playbook\n")
    logs.append(str(res))
    return logs
Exemple #16
0
 def Test(self,host):
     playbook = PlayBook(playbook='/Users/ezeng/Development/mesos-poc/vm_booting/playbook.yml',
                         # inventory=Inventory('/Users/ezeng/Development/mesos-poc/vm_booting/ansible/inventory'),
                         host_list=host.split(","),
                         callbacks=playbook_cb,
                         runner_callbacks=runner_cb,
                         stats=stats)
     playbook.run()
Exemple #17
0
def deploy_service(username, passwd, hosts, extras):

	ruta = os.path.join(ROLESDIR, 'ansible-role-mailserver/site.yml')
	
	pb = PlayBook(playbook=ruta, sudo=True, sudo_pass=passwd, host_list=hosts,
		remote_user=username, extra_vars=extras, callbacks=playbook_cb,
		runner_callbacks=runner_cb, stats=stats)
	
	pb.run()
Exemple #18
0
def execute(taskexecution_id):
    from ansible.playbook import PlayBook
    from ansible.inventory import Inventory
    from ansible import callbacks
    from ansible import utils

    utils.VERBOSITY = 0
    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    stats = callbacks.AggregateStats()
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

    time.sleep(5)

    taskexecution = TaskExecution.objects.get(pk=taskexecution_id)
    taskexecution.start()
    taskexecution.save()

    inventory = "[local]\n\
localhost   ansible_connection=local\n\
\n\
[tasker]\n\
localhost\n\
\n\
[tasker:vars]\n\
taskexecution_id={}".format(taskexecution_id)

    hosts = NamedTemporaryFile(delete=False)
    hosts.write(inventory)
    hosts.close()

    pb = PlayBook(
        playbook='/tmp/main.yml',
        host_list=hosts.name,
        # remote_user='******',
        callbacks=playbook_cb,
        runner_callbacks=runner_cb,
        stats=stats,
        # private_key_file='/path/to/key.pem'
    )

    results = pb.run()

    playbook_cb.on_stats(pb.stats)

    os.remove(hosts.name)

    status = results.get('localhost')

    if status['failures'] > 0 or status['unreachable'] > 0:
        taskexecution.failed()
        taskexecution.save()
        return False

    taskexecution.succeed()
    taskexecution.save()
    return True
Exemple #19
0
 def Test(self, host):
     playbook = PlayBook(
         playbook=
         '/Users/ezeng/Development/mesos-poc/vm_booting/playbook.yml',
         # inventory=Inventory('/Users/ezeng/Development/mesos-poc/vm_booting/ansible/inventory'),
         host_list=host.split(","),
         callbacks=playbook_cb,
         runner_callbacks=runner_cb,
         stats=stats)
     playbook.run()
Exemple #20
0
    def handle_task(self):
        # Run a Ansible playbook here.
        inventory_template = jinja2.Template(inventory)
        print self.domain_name
        print self.public_ip_address
        rendered_inventory = inventory_template.render({
            'public_ip_address':
            self.public_ip_address,
            'domain_name':
            self.domain_name,
            'customer_id':
            'bobdylan',
            'customer_name':
            'BobDylan',
            'customer_email':
            '*****@*****.**',
            'customer_CSMangementIP':
            self.public_ip_address
            # and the rest of our variables
        })

        # Create a temporary file and write the template string to it
        hosts = NamedTemporaryFile(delete=False)
        hosts.write(rendered_inventory)
        hosts.close()

        # First we will test install/uninstall the ntp server on its server.
        extra_vars_managementIP = {
            "vars": {
                "CSManagement": {
                    "ManagementIP": self.public_ip_address
                }
            }
        }
        pb = PlayBook(
            playbook=self.playbook,
            host_list=hosts.name,  # Our hosts, the rendered inventory file
            remote_user='******',
            callbacks=playbook_cb,
            runner_callbacks=runner_cb,
            stats=stats,
            extra_vars=extra_vars_managementIP,
            private_key_file='/root/.ssh/id_rsa')
        results = pb.run()

        # Ensure on_stats callback is called
        # for callback modules
        playbook_cb.on_stats(pb.stats)

        os.remove(hosts.name)
        print "***************************************************************"
        print "See this means you have finished the Playbook running in Thread"
        print "***************************************************************"
        global FinishDeploying
        FinishDeploying = 1
Exemple #21
0
 def run(self):
     pb = PlayBook(playbook=self.playbook,
                   host_list=self.host_list,
                   module_path=self.module_path,
                   callbacks=self.playbook_cb,
                   runner_callbacks=self.runner_cb,
                   stats=self.stats
                   )
     results = pb.run()
     return results
     print results
Exemple #22
0
    def __init__(self, *args, **kwargs):
        self.show_colors = kwargs.pop('show_colors', False)
        self.show_facts = kwargs.pop('show_facts', False)

        self.results = self.resultlist_loader(self, self.show_colors)
        self.callbacks = PlaybookCallbacks()
        self.runner_callbacks = PlaybookRunnerCallbacks(self.results)

        kwargs['callbacks'] = self.callbacks
        kwargs['runner_callbacks'] = self.runner_callbacks
        kwargs['stats'] = self.results
        PlayBook.__init__(self, *args, **kwargs)
def db_replication_Install():
        playbook = PlayBook(remote_user='******',
                            remote_pass=passwd,
                            playbook='./ansible/play-books/mysql-replication-setup.yml',
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            host_list='ansible/hosts'
                            )

        playoutput = playbook.run()
        for l in playoutput:print l,playoutput[l]
def management_Install():
        playbook = PlayBook(remote_user='******',
                            remote_pass=passwd,
                            playbook='./ansible/play-books/cldstk-mgmt_deploy.yml',
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            host_list='ansible/hosts'
                            )

        playoutput = playbook.run()
        for l in playoutput:print l,playoutput[l]
def runfileupdates():
        playbook = PlayBook(remote_user='******',
                            remote_pass=passwd,
                            playbook='./ansible/play-books/cldstk-files-update.yml',
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            host_list='ansible/hosts'
                            )

        playoutput = playbook.run()
        for l in playoutput:print l,playoutput[l]
def startall_in_one():
        playbook = PlayBook(remote_user='******',
                            remote_pass=passwd,
                            playbook='./ansible/play-books/all-in-one.yml',
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            host_list='ansible/hosts'
                            )

        playoutput = playbook.run()
        for l in playoutput:print l,playoutput[l]
def runPlaybook(play):
    playbook = PlayBook(remote_user='******',
                        remote_pass=passwd,
                        playbook=play,
                        callbacks=playbook_cb,
                        runner_callbacks=runner_cb,
                        stats=stats,
                        host_list='ansible/hosts'
                        )

    playoutput = playbook.run()
    for l in playoutput:print l,playoutput[l]
    def action(self, resource, action):
        # This would require to put this file to remote and execute it (mostly)

        ssh_props = find_named_transport(resource, 'ssh')

        remote_user = ssh_props['user']
        private_key_file = ssh_props.get('key')
        ssh_password = ssh_props.get('password')

        action_file = os.path.join(
            resource.db_obj.actions_path,
            resource.actions[action])

        variables = resource.args
        if 'roles' in variables:
            self.download_roles(variables['roles'])

        host = resource.ip()
        transport = C.DEFAULT_TRANSPORT

        C.HOST_KEY_CHECKING = False

        stats = callbacks.AggregateStats()
        playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = callbacks.PlaybookRunnerCallbacks(
            stats, verbose=utils.VERBOSITY)

        opts = dict(
            playbook=action_file,
            remote_user=remote_user,
            host_list=[host],
            extra_vars=variables,
            callbacks=playbook_cb,
            runner_callbacks=runner_cb,
            stats=stats,
            transport=transport)

        if ssh_password:
            opts['remote_pass'] = ssh_password
        elif private_key_file:
            opts['private_key_file'] = private_key_file
        else:
            raise Exception("No key and no password given")

        play = PlayBook(**opts)

        play.run()
        summary = stats.summarize(host)

        if summary.get('unreachable') or summary.get('failures'):
            raise errors.SolarError(
                'Ansible playbook %s failed with next summary %s',
                action_file, summary)
def systemtemplate_Install():
        playbook = PlayBook(remote_user='******',
                            remote_pass=passwd,
                            playbook='./ansible/play-books/cldstk-preseed-kvm-systmpl.yml',
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            host_list='ansible/hosts'
                            )

        playoutput = playbook.run()
        for l in playoutput:print l,playoutput[l]
def db_replication_Install():
        playbook = PlayBook(remote_user='******',
                            remote_pass=passwd,
                            playbook='./ansible/play-books/mysql-replication-setup.yml',
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            host_list='ansible/hosts'
                            )

        playoutput = playbook.run()
        for l in playoutput:print l,playoutput[l]
def systemtemplate_Install():
        playbook = PlayBook(remote_user='******',
                            remote_pass=passwd,
                            playbook='./ansible/play-books/cldstk-preseed-kvm-systmpl.yml',
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            host_list='ansible/hosts'
                            )

        playoutput = playbook.run()
        for l in playoutput:print l,playoutput[l]
def runfileupdates():
        playbook = PlayBook(remote_user='******',
                            remote_pass=passwd,
                            playbook='./ansible/play-books/cldstk-files-update.yml',
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            host_list='ansible/hosts'
                            )

        playoutput = playbook.run()
        for l in playoutput:print l,playoutput[l]
def runPlaybook(play):
    playbook = PlayBook(remote_user='******',
                        remote_pass=passwd,
                        playbook=play,
                        callbacks=playbook_cb,
                        runner_callbacks=runner_cb,
                        stats=stats,
                        host_list='ansible/hosts'
                        )

    playoutput = playbook.run()
    for l in playoutput:print l,playoutput[l]
def startall_in_one():
        playbook = PlayBook(remote_user='******',
                            remote_pass=passwd,
                            playbook='./ansible/play-books/all-in-one.yml',
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            host_list='ansible/hosts'
                            )

        playoutput = playbook.run()
        for l in playoutput:print l,playoutput[l]
def management_Install():
        playbook = PlayBook(remote_user='******',
                            remote_pass=passwd,
                            playbook='./ansible/play-books/cldstk-mgmt_deploy.yml',
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            host_list='ansible/hosts'
                            )

        playoutput = playbook.run()
        for l in playoutput:print l,playoutput[l]
Exemple #36
0
    def __init__(self, *args, **kwargs):
        self.show_colors = kwargs.pop('show_colors', False)
        self.show_facts = kwargs.pop('show_facts', False)

        self.results = self.resultlist_loader(self, self.show_colors)
        self.callbacks = PlaybookCallbacks()
        self.runner_callbacks = PlaybookRunnerCallbacks(self.results)

        kwargs['callbacks'] =self.callbacks
        kwargs['runner_callbacks'] = self.runner_callbacks
        kwargs['stats'] = self.results
        PlayBook.__init__(self, *args, **kwargs)
Exemple #37
0
def Playbook():
    vars={}
    inventory = Inventory(hostfile)
    stats =  callbacks.AggregateStats()
    playbook_cb =callbacks.PlaybookCallbacks()
    runner_cb   =callbacks.PlaybookRunnerCallbacks(stats)
    hosts=request.args.get('ip')
    task=request.args.get('playbook')
    vars['hosts'] = hosts
    play=task + '.yml'
    results = PlayBook(playbook=play,callbacks=playbook_cb,runner_callbacks=runner_cb,stats=stats,inventory=inventory,extra_vars=vars)
    res = results.run()
    return json.dumps(res,indent=4)
Exemple #38
0
    def __run_playbook(self, ip, playbook):
        pb_file = os.path.join('playbooks', playbook)
        pb = PlayBook(playbook=pb_file,
                      host_list=self.hosts.name,
                      callbacks=self.playbook_cb,
                      runner_callbacks=self.runner_cb,
                      stats=self.stats,
                      extra_vars=self.params)

        results = pb.run()
        self.playbook_cb.on_stats(pb.stats)

        print(results)
Exemple #39
0
 def run_playbook(self, playbook_file, tags=None):
     """
     Run the playbook_file using created inventory and tags specified
     :return:
     """
     stats = callbacks.AggregateStats()
     playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
     runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
     pb = PlayBook(playbook=playbook_file, inventory=self.ansible_inventory, stats=stats,
                   callbacks=playbook_cb,
                   runner_callbacks=runner_cb, only_tags=tags)
     playbook_result = pb.run()
     return playbook_result
 def playnow(self):
     inventory = Inventory(self.host_dir)
     stats = callbacks.AggregateStats()
     playbook_cb = PlaybookCallbacks()
     runner_cb = PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
     results = PlayBook(playbook=self.yaml_dir,
                        stats=stats,
                        callbacks=playbook_cb,
                        runner_callbacks=runner_cb,
                        inventory=inventory,
                        forks=200,
                        extra_vars={"dir": self.getfile_path})
     res = results.run()
     playbook_cb.on_stats(results.stats)
Exemple #41
0
    def __init__(self, *args, **kwargs):
        self.show_colors = kwargs.pop('show_colors', False)
        self.show_facts = kwargs.pop('show_facts', False)

        self.results = self.resultlist_loader(self, self.show_colors)
        self.callbacks = PlaybookCallbacks()
        self.runner_callbacks = PlaybookRunnerCallbacks(self.results)

        for k in ( 'sudo_user', 'sudo_pass', 'sudo', 'su', 'su_user', 'su_pass', ):
            kwargs.pop(k, None)

        kwargs['callbacks'] =self.callbacks
        kwargs['runner_callbacks'] = self.runner_callbacks
        kwargs['stats'] = self.results
        PlayBook.__init__(self, *args, **kwargs)
Exemple #42
0
    def playbook_api(self, yml_fp):
        ret = None

        stats = callbacks.AggregateStats()
        playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = callbacks.PlaybookRunnerCallbacks(stats,
                                                      verbose=utils.VERBOSITY)

        pb = PlayBook(playbook=yml_fp,
                      host_list=self.ansible_host_list,
                      stats=stats,
                      callbacks=playbook_cb,
                      runner_callbacks=runner_cb)

        ret = pb.run()
    def configure_hosts(self, reservation_obj, playbook):
        inven = Inventory(host_list=reservation_obj.keys())
        for host in inven.get_hosts():
            for key, value in reservation_obj[host.name].items():
                host.set_variable(key, value)

        stats = callbacks.AggregateStats()
        playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
        pb = PlayBook(inventory=inven,
                      playbook=playbook,
                      stats=stats,
                      callbacks=playbook_cb,
                      runner_callbacks=runner_cb)
        pb.run()
    def __run_playbook(self, ip, playbook):
        pb_file = os.path.join('playbooks', playbook)
        pb = PlayBook(
            playbook=pb_file,
            host_list=self.hosts.name,
            callbacks=self.playbook_cb,
            runner_callbacks=self.runner_cb,
            stats=self.stats,
            extra_vars=self.params
        )

        results = pb.run()
        self.playbook_cb.on_stats(pb.stats)

        print(results)
Exemple #45
0
def run_playbook_call_callback(playbook_path, inventory_path, subset, extra_vars, event_callback):
    callbacks_object = EmitterCallbacks(event_callback)
    stats = callbacks.AggregateStats()
    pb = PlayBook(
        playbook         =   playbook_path,
        host_list        =   inventory_path,
        callbacks        =   callbacks_object,
        runner_callbacks =   callbacks_object,
        stats            =   stats,
        extra_vars       =   extra_vars,
        subset           =   subset
    )
    results = pb.run()
    # TODO: use the result of AggregateStats - must be converted to a dict object
    callbacks_object.on_complete()
Exemple #46
0
def run_playbook(
    remote_user=None,
    remote_pass=None,
    inventory=None,
    playbook_uri=None,
    extra_vars=None,
    only_tags=None,
    playbook_args='',
):
    stats = callbacks.AggregateStats()
    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats,
                                                  verbose=utils.VERBOSITY)

    if os.path.isfile(playbook_uri):
        return PlayBook(
            callbacks=playbook_cb,
            runner_callbacks=runner_cb,
            stats=stats,
            playbook=playbook_uri,
            remote_user=remote_user,
            remote_pass=remote_pass,
            inventory=inventory,
            extra_vars=extra_vars,
            only_tags=only_tags,
        ).run()
    else:
        print '"{}" is an invalid file.'.format(playbook_uri)
Exemple #47
0
    def _run(self):
        stats = callbacks.AggregateStats()
        playbook_callbacks = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_callbacks = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

        playbook = PlayBook(
            callbacks=playbook_callbacks,
            any_errors_fatal=True,
            host_list=self.host_list,
            playbook=self.playbook,
            runner_callbacks=runner_callbacks,
            stats=stats,
        )

        results = playbook.run()
        return results
Exemple #48
0
    def base_playbook(self, cmd, custom=None):
        binv = ansible.inventory.Inventory('hosts')
        #binv.subset('*' + self.host['host'])
        stats = ansible.callbacks.AggregateStats()
        pb_cb = ansible.callbacks.PlaybookCallbacks(
            verbose=ansible.utils.VERBOSITY)
        rn_cb = ansible.callbacks.PlaybookRunnerCallbacks(
            stats, verbose=ansible.utils.VERBOSITY)

        if custom is None:
            custom = {}
        opts = self.opts.copy()
        opts.update(custom)

        pb = PlayBook(playbook=self.playbook % cmd,
                      inventory=binv,
                      forks=self.forks,
                      callbacks=pb_cb,
                      runner_callbacks=rn_cb,
                      stats=stats,
                      timeout=self.timeout,
                      any_errors_fatal=True,
                      extra_vars=opts)

        return ansible_run(pb)
Exemple #49
0
def playbook():
    inventory = """
    [initial]
    {% for i in hosts %}
    {{ i }}
    {% endfor %}
    """

    inventory_template = jinja2.Template(inventory)
    data = json.loads(request.get_data())
    inst_ip = data["ips"]
    rendered_inventory = inventory_template.render({'hosts': inst_ip})
    hosts = NamedTemporaryFile(delete=False, suffix='tmp', dir='/tmp/ansible/')
    hosts.write(rendered_inventory)
    hosts.close()
    '''
    前端传递过来的Json数据,在Flask里面用了jinja2渲染成Ansible能识别的格式,并以临时文件的形式存在于/tmp/ansible/目录下
    /tmp/ansible/目录可以提前建立
    '''
    inventory = Inventory(hosts.name)
    vars = {}
    stats = callbacks.AggregateStats()
    playbook_cb = callbacks.PlaybookCallbacks()
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats)
    pb = PlayBook(playbook='/root/ansible/mytest.yml', callbacks=playbook_cb, runner_callbacks=runner_cb, stats=stats,
                  inventory=inventory, extra_vars=vars)
    job = q.enqueue_call(pb.run, result_ttl=5000, timeout=2000)
    jid = job.get_id()
    if jid:
        app.logger.info("Job Succesfully Queued with JobID: %s" % jid)
    else:
        app.logger.error("Failed to Queue the Job")
    return jid
Exemple #50
0
 def playbook_api(self,yml_fp):
     ret = None
     
     stats = callbacks.AggregateStats()
     playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
     runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
     
     pb = PlayBook(
         playbook=yml_fp,
         host_list=self.ansible_host_list,
         stats=stats,
         callbacks=playbook_cb,
         runner_callbacks=runner_cb
     )
 
     ret = pb.run()
Exemple #51
0
def runplaybook(playbook, subset='*', check=False, forks=10,
                debug_output=False):
    from ansible.playbook import PlayBook
    from ansible import callbacks
    from ansible import utils
    import sys
    from cStringIO import StringIO

    # ansible-playbook dumps output to stderr during execution.  We want to
    # capture that so we can show the user the output.
    output = None
    old_stdout = sys.stdout
    redirected_ouput = sys.stdout = StringIO()

    # Boilerplace callbacks for stdout/stderr and log output
    utils.VERBOSITY = 0
    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    stats = callbacks.AggregateStats()
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats,
                                                  verbose=utils.VERBOSITY)

    pb = PlayBook(
        playbook=c.ANSIBLE_PATH + '/' + playbook,
        host_list=c.HOSTS_FILE,
        subset=subset,
        remote_user='******',
        callbacks=playbook_cb,
        runner_callbacks=runner_cb,
        stats=stats,
        check=check,
        forks=forks,
    )

    results = pb.run()

    # Ensure on_stats callback is called
    # for callback modules
    playbook_cb.on_stats(pb.stats)

    output = redirected_ouput.getvalue()
    sys.stdout = old_stdout

    # This is really for running it from the python shell direct to debug
    if debug_output:
        return results, output

    return results
Exemple #52
0
def playbook_run(inventory,
                 playbook,
                 default_user=None,
                 default_port=None,
                 default_pri_key_path=None):
    stats = callbacks.AggregateStats()
    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats,
                                                  verbose=utils.VERBOSITY)
    # run the playbook
    print(default_user, default_port, default_pri_key_path, inventory,
          playbook)
    if default_user and default_port and default_pri_key_path:
        playbook = PlayBook(host_list=inventory,
                            playbook=playbook,
                            forks=5,
                            remote_user=default_user,
                            remote_port=default_port,
                            private_key_file=default_pri_key_path,
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            become=True,
                            become_user='******')
    else:
        playbook = PlayBook(host_list=inventory,
                            playbook=playbook,
                            forks=5,
                            callbacks=playbook_cb,
                            runner_callbacks=runner_cb,
                            stats=stats,
                            become=True,
                            become_user='******')
    results = playbook.run()
    print(results)
    results_r = {'unreachable': [], 'failures': [], 'success': []}
    for hostname, result in list(results.items()):
        if result.get('unreachable', 2):
            results_r['unreachable'].append(hostname)
            print("%s >>> unreachable" % hostname)
        elif result.get('failures', 2):
            results_r['failures'].append(hostname)
            print("%s >>> Failed" % hostname)
        else:
            results_r['success'].append(hostname)
            print("%s >>> Success" % hostname)
    return results_r
Exemple #53
0
    def run(self):
        """Run playbook

        Runs playbook and collects output to self.results

        """
        stats = PlayBook.run(self)
        return self.process_results(self.results)
Exemple #54
0
def _playbook(params, play_file):
    stats = callbacks.AggregateStats()
    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    runner_cb = PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

    res = PlayBook(playbook=play_file,
                   stats=stats,
                   callbacks=playbook_cb,
                   check=False,
                   runner_callbacks=runner_cb,
                   extra_vars=eval(params),
                   forks=200)

    result = res.run()
    # print '-----', result
    res = playbook_cb.on_stats(res.stats)
    return res
Exemple #55
0
    def run(self):
        """Run playbook

        Runs playbook and collects output to self.results

        """
        stats = PlayBook.run(self)
        return self.process_results(self.results)