python 通过ansible 获取服务器基本信息:
#!/usr/bin/env python #coding:utf-8 def sever_info(ip) cmd = "/usr/bin/ansible {ip} -m setup".format(ip=ip) raw_info = subprocess.check_output(cmd,shell=True) base_info = json.loads(raw_info.split(‘=>‘)[1])[‘ansible_facts‘] cpu = base_info[‘ansible_processor_vcpus‘] memory = round(int(base_info[‘ansible_memtotal_mb‘])/1024.0,1) disk_info = base_info[‘ansible_devices‘] disk_volume = sum([int(disk_info[disk][‘sectors‘])*int(disk_info[disk][‘sectorsize‘]) for disk in disk_info])/1024**3 hostname = base_info[‘ansible_hostname‘] os = base_info[‘ansible_lsb‘][‘description‘] print cpu,memory,disk_info,disk_volume,hostname,os if __name__=="__main__": sever_info(192.168.1.1)
原文地址:http://3379770.blog.51cto.com/3369770/1906776