def exec_setup(): '''获取主机详情''' runner2 = Runner(module_name="setup", module_args="", remote_user="******", pattern="all", hosts="172.16.0.101") result2 = runner2.run() print(result2)
def exec_shell(): runner1 = Runner(module_name="shell", module_args="uptime", remote_user="******", pattern="all", hosts="1.1.1.1") result1 = runner1.run() print(result1) if result1['dark']: print(result1['dark'])
def exec_setup(user, host): '''获取主机详情''' runner = Runner(module_name="setup", module_args="", remote_user=user, pattern="all", hosts=host) result = runner.run() print(result) if result['dark']: return False else: return result
def run(self, module_name="shell", module_args='', hosts='', remote_user="******", timeout=10, forks=10): '''Ansible运行函数''' runner = Runner( module_name=module_name, module_args=module_args, remote_user=remote_user, pattern="all", hosts=hosts, forks=forks, timeout=timeout, ) result = runner.run() return result
def copy_file(self): '''复制脚本到主机''' runner = Runner(module_name="copy", module_args="src=%s dest=/tmp/ backup=yes" % self.file_path, remote_user="******", pattern="all", hosts=self.host_list) result = runner.run() if result['dark']: for err_host in result['dark']: self.Error_host.append(err_host) self.msg = '[Error] copy file faild => %s' % self.Error_host self.status = False
def exec_file(self): '''执行脚本并接收结果''' runner = Runner(module_name="shell", module_args="/usr/bin/python /tmp/%s" % self.file_name, remote_user="******", pattern="all", hosts=self.host_list) result = runner.run() if result['dark']: for err_host in result['dark']: self.Error_host.append(err_host) self.msg = '[Error] exec sysinfo faild => %s' % self.Error_host self.status = False else: for k, v in result['contacted'].items(): self.data[k] = json.loads(v['stdout'])
def exec_shell(): host_dict = { "host": '172.16.0.101', "port": '2222', } runner1 = Runner( module_name="shell", module_args="uptime", remote_user="******", pattern="all", forks=5, hosts= host_dict ) result1 = runner1.run() print(result1) if result1['dark']: print(result1['dark'])
def get_host_info(server_list): if not isinstance(server_list, list): raise ValueError() for host in server_list: ip = host[0] user = host[2] host_dict = { "host": host[0], "port": host[1], "user": host[2], } # print(ip,user) runner = Runner( module_name="setup", module_args="", remote_user=user, # remote_port=2222, pattern="all", hosts=host_dict, timeout=10, ) result = runner.run() if result['dark']: msg = { ip: { 'status': False, 'msg': result['dark'][ip]['msg'] } } return msg else: asset_data = { ip: { 'status': True, 'msg': '获取资产成功', } } # ip为key 数据为value # print(result['contacted'][ip]) # 资产信息 # SN # print(result['contacted'][ip]) try: sn = result['contacted'][ip]['ansible_facts']['ansible_product_serial'] except KeyError: sn = 'Null' # 主机名 try: #这个带.没办法获取到 #host_name = result['contacted'][ip]['ansible_facts']['ansible_hostname'] host_name = result['contacted'][ip]['ansible_facts']['ansible_fqdn'] #centos7.6版本下获取hostname fqdn会出现全显示localhost6.localdomain6问题 if host_name == "localhost6.localdomain6": host_name = result['contacted'][ip]['ansible_facts']['ansible_hostname'] except KeyError: host_name = 'Null' # cpu型号 try: cpu = result['contacted'][ip]['ansible_facts']['ansible_processor'][-1] except KeyError: cpu = 'Null' # CPU核心数 try: cpu_cores = result['contacted'][ip]['ansible_facts']['ansible_processor_vcpus'] except KeyError: cpu_cores = 'Null' # 物理内存容量 try: memory = result['contacted'][ip]['ansible_facts']['ansible_memtotal_mb'] except KeyError: memory = 'Null' # 磁盘容量 try: disk = sum([int(result['contacted'][ip]['ansible_facts']["ansible_devices"][i]["sectors"]) * \ int(result['contacted'][ip]['ansible_facts']["ansible_devices"][i][ "sectorsize"]) / 1024 / 1024 / 1024 \ for i in result['contacted'][ip]['ansible_facts']["ansible_devices"] if i[0:2] in ("sd", "ss", "vd", "xv")]) except KeyError: disk = 'Null' # 磁盘mount # disk_mount = str( # [{"mount": i["mount"], "size": i["size_total"] / 1024 / 1024 / 1024} for i in result['contacted'][ip]['ansible_facts']["ansible_mounts"]]) # # print(disk_mount) # 服务器类型 try: os_type = " ".join((result['contacted'][ip]['ansible_facts']["ansible_distribution"], result['contacted'][ip]['ansible_facts']["ansible_distribution_version"])) except KeyError: os_type = 'Null' try: os_kernel = result['contacted'][ip]['ansible_facts']['ansible_kernel'] except KeyError: os_kernel = 'Null' asset_data[ip]['sn'] = sn asset_data[ip]['host_name'] = host_name asset_data[ip]['cpu'] = cpu asset_data[ip]['cpu_cores'] = cpu_cores asset_data[ip]['memory'] = M2human(memory) asset_data[ip]['disk'] = disk asset_data[ip]['os_type'] = os_type asset_data[ip]['os_kernel'] = os_kernel # print(asset_data) # print('ok') return asset_data
def get_host_info(server_list): if not isinstance(server_list, list): raise ValueError() for host in server_list: ip = host[0] user = host[2] runner = Runner(module_name="setup", module_args="", remote_user=user, pattern="all", hosts=ip) result = runner.run() if result['dark']: msg = {ip: {'status': False, 'msg': result['dark'][ip]['msg']}} return msg else: asset_data = { ip: { 'status': True, 'msg': '获取资产成功', } } # ip为key 数据为value # print(result['contacted'][ip]) # 资产信息 # SN try: sn = result['contacted'][ip]['ansible_facts'][ 'ansible_product_serial'] except KeyError: sn = 'Null' # 主机名 try: host_name = result['contacted'][ip]['ansible_facts'][ 'ansible_hostname'] except KeyError: host_name = 'Null' # cpu型号 try: cpu = result['contacted'][ip]['ansible_facts'][ 'ansible_processor'][-1] except KeyError: cpu = 'Null' # CPU核心数 try: cpu_cores = result['contacted'][ip]['ansible_facts'][ 'ansible_processor_vcpus'] except KeyError: cpu_cores = 'Null' # 物理内存容量 try: memory = result['contacted'][ip]['ansible_facts'][ 'ansible_memtotal_mb'] except KeyError: memory = 'Null' # 磁盘容量 try: disk = sum([int(result['contacted'][ip]['ansible_facts']["ansible_devices"][i]["sectors"]) * \ int(result['contacted'][ip]['ansible_facts']["ansible_devices"][i][ "sectorsize"]) / 1024 / 1024 / 1024 \ for i in result['contacted'][ip]['ansible_facts']["ansible_devices"] if i[0:2] in ("sd", "ss", "vd", "xv")]) except KeyError: disk = 'Null' # 磁盘mount # disk_mount = str( # [{"mount": i["mount"], "size": i["size_total"] / 1024 / 1024 / 1024} for i in result['contacted'][ip]['ansible_facts']["ansible_mounts"]]) # # print(disk_mount) # 服务器类型 try: os_type = " ".join( (result['contacted'][ip]['ansible_facts'] ["ansible_distribution"], result['contacted'][ip] ['ansible_facts']["ansible_distribution_version"])) except KeyError: os_type = 'Null' try: os_kernel = result['contacted'][ip]['ansible_facts'][ 'ansible_kernel'] except KeyError: os_kernel = 'Null' asset_data[ip]['sn'] = sn asset_data[ip]['host_name'] = host_name asset_data[ip]['cpu'] = cpu asset_data[ip]['cpu_cores'] = cpu_cores asset_data[ip]['memory'] = M2human(memory) asset_data[ip]['disk'] = '{disk}G'.format(disk=disk) asset_data[ip]['os_type'] = os_type asset_data[ip]['os_kernel'] = os_kernel print(asset_data) return asset_data