当前位置: 首页>>代码示例>>Python>>正文


Python Inventory.generateInventory方法代码示例

本文整理汇总了Python中inventory.Inventory.generateInventory方法的典型用法代码示例。如果您正苦于以下问题:Python Inventory.generateInventory方法的具体用法?Python Inventory.generateInventory怎么用?Python Inventory.generateInventory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在inventory.Inventory的用法示例。


在下文中一共展示了Inventory.generateInventory方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_010_validate_inventory

# 需要导入模块: from inventory import Inventory [as 别名]
# 或者: from inventory.Inventory import generateInventory [as 别名]
 def test_010_validate_inventory(self):
     i = Inventory()
     config = configparser.ConfigParser()
     config.read_file(open('./ansible.cfg'))
     box_list = [['local', '127.0.0.1', 'USER_NAME', 'USER_PASSWORD']]
     codes = {'local':'test'}
     ret, msg = i.generateInventory(config,box_list, "./test.hosts", codes, "")
     self.assertEqual(ret,True)
开发者ID:FourtekIT-incubator,项目名称:openebs,代码行数:10,代码来源:test_inventory.py

示例2: main

# 需要导入模块: from inventory import Inventory [as 别名]
# 或者: from inventory.Inventory import generateInventory [as 别名]

#.........这里部分代码省略.........
        default_inventory_path = ansible_path + '/inventory/'
        try:
            config.read_file(open(ansible_cfg_path))
            inventory_path = ansible_path + config.get('defaults', 'inventory')
        except IOError:
            if os.path.isdir(default_inventory_path) is not True:
                os.makedirs(default_inventory_path)
            inventory_path = default_inventory_path + 'hosts'
    else:
        os.makedirs('inventory')
        default_inventory_path = current_path + '/inventory/'
        inventory_path = default_inventory_path + 'hosts'

    # Define logging levels for script execution
    logfile = '%s/host-status.log' % (default_inventory_path)

    if args.loglevel and args.loglevel.upper() == "DEBUG":
        logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
                            filename=logfile, filemode='a', level=10)
    else:
        logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
                            filename=logfile, filemode='a', level=20)

    # Initiate log file
    clearLogCmd = '> %s' % (logfile)
    executeCmd(clearLogCmd)

    # Initialize dictionary holding supported host codes
    SupportedHostCodes = {
            'localhost': None,
            'mayamaster': 'openebs-mayamasters',
            'mayahost': 'openebs-mayahosts',
            'kubemaster': 'kubernetes-kubemasters',
            'kubeminion': 'kubernetes-kubeminions'
            }

    """Create list of tuples containing individual machine info & initialize
    localhost password"""
    HostList = []
    local_password = None
    v = Validator()
    with open(Hosts, "rb") as fp:
        for i in fp.readlines():
            tmp = i.split(",")
            if tmp[0] != '\n' and "#" not in tmp[0]:
                ret, msg = v.validateInput(tmp, SupportedHostCodes)
                if not ret:
                    print msg
                    exit()

                if tmp[0] == "localhost":
                    local_password = tmp[3].rstrip('\n')
                try:
                    HostList.append((tmp[0], tmp[1],
                                     tmp[2], tmp[3].rstrip('\n')))
                except IndexError as e:
                    info_text = "Unable to parse input, failed with error %s"
                    logging.info(info_text, e)
                    exit()

    if args.passwordless and args.passwordless.upper() == "TRUE":
        # Setup passwordless SSH between the localhost and target hosts
        setupSSH(key_rsa_path, key_append_path, key_gen_cmd, HostList)
        passwdless = True
    else:
        passwdless = False

    # Generate Ansible hosts file from 'machines.in'
    codes = list(SupportedHostCodes)
    inventory = Inventory()
    for i in codes:
        codeSubList = []
        for j in HostList:
            if i in j:
                codeSubList.append(j)
        ret, msg = inventory.generateInventory(config, codeSubList,
                                               inventory_path,
                                               SupportedHostCodes,
                                               passwdless)
        if not ret:
            print msg
            exit()

    print "Inventory config generated successfully"
    logging.info("Inventory config generated successfully")

    # Insert localhost line into beginning of inventory file
    if local_password:
        lpasswd = "\"{{ lookup('env','%s') }}\"" % (local_password)
        localhostString = """localhost ansible_connection=local
        ansible_become_pass=%s\n\n""" % (lpasswd)

        with open(inventory_path, 'rb') as f:
            with open('hosts.tmp', 'wb') as f2:
                f2.write(localhostString)
                f2.write(f.read())
        os.rename('hosts.tmp', inventory_path)

    # Sanitize the Ansible inventory file
    replace(inventory_path, " = ", "=")
开发者ID:FourtekIT-incubator,项目名称:openebs,代码行数:104,代码来源:generate_inventory.py


注:本文中的inventory.Inventory.generateInventory方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。