Example #1
0
    def post(self, request, *args, **kwargs):
        job_name = request.POST.get('job_name', '')
        job_pattern = request.POST.get('job_pattern', '')
        template_id = request.POST.get('template_id', '')
        start_time = timezone.now()

        if request.is_ajax():
            pass
        else:
            job = AnsibleJob.objects.create(job_name=job_name, job_pattern=job_pattern, start_time=start_time,
                                            ansible_playbook_id=template_id,auth_user_id=request.user.id)
            for host in playbook_list_hosts(job.job_pattern):
                AnsibleJobTask.objects.bulk_create([
                                                       AnsibleJobTask(task_name=task.task_name,
                                                                      task_host=host,
                                                                      ansible_job_id=job.id,
                                                                      start_time=start_time)
                                                       for task in
                                                       job.ansible_playbook.ansibleplaybooktask_set.all()
                                                       ]
                                                   )

        inventory_path = model_to_inventory(AnsibleGroup.objects.all())
        playbook_template = AnsiblePlaybook.objects.get(id=template_id)
        kwargs = dict(
            instance_type=AnsibleJob.__name__,
            task_id=job.id,
            hosts=job.job_pattern,
            inventory_path=inventory_path
        )

        run_job.delay(playbook_generate_yaml(playbook_template), **kwargs)
        return HttpResponseRedirect('/job/%d' % job.id)
Example #2
0
    def post(self, request, *args, **kwargs):
        adhoc_name = request.POST.get('adhoc_name', '')
        adhoc_pattern = request.POST.get('adhoc_pattern', '')
        adhoc_args = request.POST.get('adhoc_args', '')
        module_id = request.POST.get('module_id', '')
        adhoc_id = request.POST.get('adhoc_id', '')
        start_time = timezone.now()

        if request.is_ajax():
            adhoc_old = AnsibleAdhoc.objects.get(id=adhoc_id)
            adhoc = AnsibleAdhoc.objects.create(adhoc_name=adhoc_old.adhoc_name, adhoc_pattern=adhoc_old.adhoc_pattern,
                                                adhoc_args=adhoc_old.adhoc_args,
                                                ansible_module=adhoc_old.ansible_module, start_time=start_time,
                                                auth_user_id=request.user.id)

        else:
            adhoc = AnsibleAdhoc.objects.create(adhoc_name=adhoc_name, adhoc_pattern=adhoc_pattern,
                                                adhoc_args=adhoc_args,
                                                ansible_module_id=module_id, start_time=start_time,
                                                auth_user_id=request.user.id)

        inventory_path = model_to_inventory(AnsibleGroup.objects.all())
        # insert adhoc task
        for host in playbook_list_hosts(adhoc.adhoc_pattern, inventory_path):
            AnsibleAdhocTask.objects.create(task_host=host, ansible_adhoc_id=adhoc.id, start_time=timezone.now())

        # use celery to execute an adhoc async
        kwargs = dict(
            instance_type=AnsibleAdhoc.__name__,
            task_id=adhoc.id,
            hosts=adhoc.adhoc_pattern,
            module_args=adhoc.adhoc_args,
            module_name=str(adhoc.ansible_module.module_name),
            inventory_path=inventory_path
        )

        run_adhoc.delay(**kwargs)

        if request.is_ajax():
            res_ctx = {}
            res_ctx.update(adhoc_id=adhoc.id)
            return HttpResponse(simplejson.dumps({'result': res_ctx, 'status': 0, 'finish': adhoc.finish}),
                                content_type='application/json')
        else:
            return HttpResponseRedirect('/adhoc/%d' % adhoc.id)