示例#1
0
def task_run_module(hosts, group, data, user, user_agent, client, issuperuser=False):
    host_data = list()
    _hosts = ''
    for host in hosts:
        _hosts += '{}_{}_{}\n'.format(host['hostname'], host['ip'], host['username'])
        hostinfo = dict()
        hostinfo['hostname'] = host['hostname']
        hostinfo['ip'] = host['ip']
        hostinfo['port'] = host['port']
        hostinfo['username'] = host['username']
        hostinfo['password'] = host['password']
        if issuperuser:
            if host['superusername']:
                hostinfo['become'] = {
                    'method': 'su',
                    'user': host['superusername'],
                    'pass': host['superpassword']
                }
        host_data.append(hostinfo)
    inventory = BaseInventory(host_data)
    module = data.get('module', 'command')
    args = data.get('args', '')
    cmd = 'module: {0} args: {1}'.format(module, args)
    callback = ModuleCallbackModule(group, cmd=cmd, user=user, user_agent=user_agent, client=client, _hosts=_hosts)
    ansible_api = AnsibleAPI(
        dynamic_inventory=inventory,
        callback=callback
    )
    ansible_api.run_modules(cmds=args, module=module, hosts='all', group=group)
示例#2
0
文件: tasks.py 项目: qt-pay/devops-4
def task_run_cmd(hosts,
                 group,
                 cmd,
                 user,
                 user_agent,
                 client,
                 issuperuser=False):
    host_data = list()
    _hosts = ''
    for host in hosts:
        _hosts += '{}_{}_{}\n'.format(host['hostname'], host['ip'],
                                      host['username'])
        hostinfo = dict()
        hostinfo['hostname'] = host['hostname']
        hostinfo['ip'] = host['ip']
        hostinfo['port'] = host['port']
        hostinfo['username'] = host['username']
        hostinfo['password'] = host['password']
        if issuperuser:
            if host['superusername']:
                hostinfo['become'] = {
                    'method': 'su',
                    'user': host['superusername'],
                    'pass': host['superpassword']
                }
        host_data.append(hostinfo)
    inventory = BaseInventory(host_data)
    callback = ModuleCallbackModule(group,
                                    cmd=cmd,
                                    user=user,
                                    user_agent=user_agent,
                                    client=client,
                                    _hosts=_hosts)
    ansible_api = AnsibleAPI(dynamic_inventory=inventory, callback=callback)
    ansible_api.run_cmd(cmds=cmd, hosts='all', group=group)
示例#3
0
def task_run_script(hosts, group, data, user, user_agent, client, issuperuser=False):
    host_data = list()
    _hosts = ''
    for host in hosts:
        _hosts += '{}_{}_{}\n'.format(host['hostname'], host['ip'], host['username'])
        hostinfo = dict()
        hostinfo['hostname'] = host['hostname']
        hostinfo['ip'] = host['ip']
        hostinfo['port'] = host['port']
        hostinfo['username'] = host['username']
        hostinfo['password'] = host['password']
        if issuperuser:
            if host['superusername']:
                hostinfo['become'] = {
                    'method': 'su',
                    'user': host['superusername'],
                    'pass': host['superpassword']
                }
        host_data.append(hostinfo)
    inventory = BaseInventory(host_data)
    callback = ModuleCallbackModule(group, cmd=data['script_name'], user=user, user_agent=user_agent, client=client, _hosts=_hosts)
    ansible_api = AnsibleAPI(
        dynamic_inventory=inventory,
        callback=callback
    )
    path = data.get('path', '/tmp')
    if path == '':
        path = '/tmp'
    exec = data.get('exec', '')
    script_name = data.get('script_name', '')
    script = data.get('script', '')

    now = time.time()
    tmp_date = time.strftime("%Y-%m-%d", time.localtime(int(now)))
    if not os.path.isdir(os.path.join(settings.SCRIPT_ROOT, tmp_date)):
        os.makedirs(os.path.join(settings.SCRIPT_ROOT, tmp_date))
    script_file = settings.SCRIPT_DIR + '/' + tmp_date + '/' + gen_rand_char(16) + '_' + script_name
    res_file = settings.MEDIA_ROOT + '/' + script_file
    with open(res_file, 'w+') as f:
        f.write(script)
    cmds = '{}'.format(res_file)
    cmds = cmds + ' ' + 'chdir={}'.format(path)
    if exec:
        cmds = cmds + ' ' + 'executable={}'.format(exec)
    else:
        if script_name.split('.')[-1] == 'py':
            cmds = cmds + ' ' + 'executable=/usr/bin/python'
        elif script_name.split('.')[-1] == 'pl':
            cmds = cmds + ' ' + 'executable=/usr/bin/perl'
        elif script_name.split('.')[-1] == 'rb':
            cmds = cmds + ' ' + 'executable=/usr/bin/ruby'
    ansible_api.run_script(cmds=cmds, hosts='all', group=group, script=script_file)