async def handler_package_ws_ansible_run(request):
    ws = web.WebSocketResponse()
    cmdb = request.app['cmdb']
    (await ws.prepare(request))
    async for msg in ws:
        if (msg.type == aiohttp.WSMsgType.TEXT):
            if (msg.data == 'close'):
                (await ws.close())
            else:
                msg_data = json.loads(msg.data)
                params = []
                query_data = json.loads(msg_data['query'])
                for i in query_data['nodeDataArray']:
                    if (i.get('role', None) and i.get('data', None)):
                        params.append(i.get('data'))
                (await ws.send_str('>> {} \r\n'.format(json.dumps({'$or': params}))))
                import pprint
                pprint.pprint({'$or': params})
                result = cmdb.assets.find({'$or': params})
                hosts = []
                for host in (await result.to_list(length=1000)):
                    hosts.append(host)
                hosts_file = get_ansible_hosts_data(hosts)
                work_path = os.path.dirname(os.path.abspath(msg_data['path']))
                command = 'ansible -i {} all -m ping'.format(hosts_file)
                (await ws.send_str('>> {} \r\n'.format(command)))
                (await ws.send_str('>> {} \r\n'.format('result:')))
                import time
                time.sleep(1)
                p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=work_path)
                while (p.poll() == None):
                    out = p.stdout.readline().strip()
                    err = p.stderr
                    if err:
                        (await ws.send_str(err.decode(encoding='utf-8', errors='strict')))
                    if out:
                        (await ws.send_str(out.decode(encoding='utf-8', errors='strict')))
                (await ws.send_str('return code: {} '.format(p.returncode)))
    return ws
async def handler_package_ws_ansible_run(request):
    ws = web.WebSocketResponse()
    cmdb = request.app['cmdb']
    (await ws.prepare(request))
    async for msg in ws:
        if (msg.type == aiohttp.WSMsgType.TEXT):
            if (msg.data == 'close'):
                (await ws.close())
            else:
                msg_data = json.loads(msg.data)
                params = []
                query_data = json.loads(msg_data['query'])
                for i in query_data['nodeDataArray']:
                    if (i.get('role', None) and i.get('data', None)):
                        params.append(i.get('data'))
                (await ws.send_str('>> {} \r\n'.format(json.dumps({'$or': params}))))
                import pprint
                pprint.pprint({'$or': params})
                result = cmdb.assets.find({'$or': params})
                hosts = []
                for host in (await result.to_list(length=1000)):
                    hosts.append(host)
                hosts_file = get_ansible_hosts_data(hosts)
                work_path = os.path.dirname(os.path.abspath(msg_data['path']))
                command = 'ansible -i {} all -m ping'.format(hosts_file)
                (await ws.send_str('>> {} \r\n'.format(command)))
                (await ws.send_str('>> {} \r\n'.format('result:')))
                import time
                time.sleep(1)
                p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=work_path)
                while (p.poll() == None):
                    out = p.stdout.readline().strip()
                    err = p.stderr
                    if err:
                        (await ws.send_str(err.decode(encoding='utf-8', errors='strict')))
                    if out:
                        (await ws.send_str(out.decode(encoding='utf-8', errors='strict')))
                (await ws.send_str('return code: {} '.format(p.returncode)))
    return ws