def create_hosts(argvs): ''' create hosts :param argvs: :return: ''' if '-f' in argvs: hosts_file = argvs[argvs.index("-f") + 1] else: print_err( "invalid usage, should be:\ncreate_hosts -f <the new hosts file>", quit=True) source = yaml_parser(hosts_file) if source: for key, val in source.items(): print(key, val) #models.Host 是导入进来的。是from models import models #把参数给models.py中的Host类中的形参 obj = models.Host(hostname=key, ip=val.get('ip'), port=val.get('port') or 22) session.add(obj) #一定要提交才能真的插入进去 session.commit()
def create_hosts(argvs): '''读取参数中的yaml文件并解析''' if '-f' in argvs: hosts_file = argvs[argvs.index('-f') + 1] else: print_err("create_hosts -f <the hosts file>", quit=True) source = yaml_parser(hosts_file) if source: for key, val in source.items(): print(key, val) obj = models.Host( hostname=key, ip=val.get('ip'), port=val.get('port') or 22, ) session.add(obj) session.commit()
def create_hosts(argvs): """ create hosts :param argvs: :return: """ if '-f' in argvs: hosts_file = argvs[argvs.index("-f") + 1] else: print_err("invalid usage, should be:\ncreate_hosts -f <the new hosts file>", logout=True) return source = yaml_parser(hosts_file) if source: logger.debug("source:\n%s" % source) for key, val in source.items(): logger.debug("%s:%s" % (key, val)) obj = models.Host(hostname=key, ip=val.get('ip'), port=val.get('port') or 22) logger.info(obj) session.add(obj) session.commit() logger.info("create hosts sucess!")
def create_hosts(argv): ''' create hosts :param argv: :return: ''' if '-f' in argv: host_file = argv[argv.index('-f') + 1] else: print_err( "invalid usage, should be:\ncreate_hosts -f <the new hosts file>", quit=True) source = yaml_parser(host_file) if source: print(source) for key, val in source: obj = models.Host(hostname=key, ip=val.get('ip'), port=val.get('port') or 22) session.add(obj) session.commit()
def create_hosts(argvs): ''' create 主机 :param argvs: :return: ''' if '-f' in argvs: # 指定一个文件名否则报错 hosts_file = argvs[argvs.index("-f") + 1] else: print_err( "invalid usage, should be:\ncreate_hosts -f <the new hosts file>", quit=True) source = yaml_parser(hosts_file) # 传文件回来 if source: # 循环字典 print(source) for key, val in source.items(): print(key, val) obj = models.Host(hostname=key, ip=val.get('ip'), port=val.get('port') or 22) # 添加到表 session.add(obj) session.commit()
def create_hosts(argvs): ''' create hosts :param argvs: command line arguments :return: ''' # 输入的命令行参数是否含有'-f'字符串 if '-f' in argvs: # 取出文件名 hosts_file = argvs[argvs.index("-f") + 1] else: print_err( "invalid usage, should be:\ncreate_hosts -f <the new hosts file>", quit=True) source = yaml_parser(hosts_file) if source: print(source) for key, val in source.items(): print(key, val) obj = models.Host(hostname=key, ip=val.get('ip'), port=val.get('port') or 22) session.add(obj) session.commit()