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


Python NetworkModule.config方法代码示例

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


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

示例1: main

# 需要导入模块: from ansible.module_utils.network import NetworkModule [as 别名]
# 或者: from ansible.module_utils.network.NetworkModule import config [as 别名]
def main():
    """main entry point for module execution
    """

    argument_spec = dict(
        netconf_port=dict(type='int', default=830, aliases=['listens_on']),
        state=dict(default='present', choices=['present', 'absent']),
        transport=dict(default='cli', choices=['cli'])
    )

    module = NetworkModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    state = module.params['state']
    port = module.params['netconf_port']

    result = dict(changed=False)

    instance = get_instance(module)

    if state == 'present' and instance.get('state') == 'absent':
        commands = 'set system services netconf ssh port %s' % port
    elif state == 'present' and port != instance.get('port'):
        commands = 'set system services netconf ssh port %s' % port
    elif state == 'absent' and instance.get('state') == 'present':
        commands = 'delete system services netconf'
    else:
        commands = None

    if commands:
        if not module.check_mode:
            try:
                comment = 'configuration updated by junos_netconf'
                module.config(commands, comment=comment)
            except NetworkError:
                exc = get_exception()
                module.fail_json(msg=str(exc), **exc.kwargs)
        result['changed'] = True
        result['commands'] = commands

    module.exit_json(**result)
开发者ID:2ndQuadrant,项目名称:ansible,代码行数:43,代码来源:junos_netconf.py

示例2: main

# 需要导入模块: from ansible.module_utils.network import NetworkModule [as 别名]
# 或者: from ansible.module_utils.network.NetworkModule import config [as 别名]
def main():
    """ main entry point for module execution
    """

    argument_spec = dict(
        src=dict(),
        force=dict(default=False, type='bool'),
        include_defaults=dict(default=True, type='bool'),
        backup=dict(default=False, type='bool'),
        config=dict(),
    )

    mutually_exclusive = [('config', 'backup'), ('config', 'force')]

    module = NetworkModule(argument_spec=argument_spec,
                           mutually_exclusive=mutually_exclusive,
                           supports_check_mode=True)

    result = dict(changed=False)

    candidate = NetworkConfig(contents=module.params['src'], indent=2)

    contents = get_config(module)
    if contents:
        config = NetworkConfig(contents=contents, indent=2)
        result['_backup'] = str(contents)

    if not module.params['force']:
        commands = candidate.difference(config)
        commands = dumps(commands, 'commands').split('\n')
        commands = [str(c) for c in commands if c]
    else:
        commands = str(candidate).split('\n')

    if commands:
        if not module.check_mode:
            response = module.config(commands)
            result['responses'] = response
        result['changed'] = True

    result['updates'] = commands
    module.exit_json(**result)
开发者ID:likewg,项目名称:DevOps,代码行数:44,代码来源:_nxos_template.py

示例3: main

# 需要导入模块: from ansible.module_utils.network import NetworkModule [as 别名]
# 或者: from ansible.module_utils.network.NetworkModule import config [as 别名]
def main():
    """ main entry point for module execution
    """

    argument_spec = dict(
        src=dict(type='str'),
        force=dict(default=False, type='bool'),
        backup=dict(default=False, type='bool'),
        config=dict(type='dict'),
    )

    mutually_exclusive = [('config', 'backup'), ('config', 'force')]

    module = NetworkModule(argument_spec=argument_spec,
                           mutually_exclusive=mutually_exclusive,
                           supports_check_mode=True)

    if not module.params['transport'] and not HAS_OPS:
        module.fail_json(msg='unable to import ops.dc library')

    result = dict(changed=False)

    contents = get_config(module)
    result['_backup'] = contents

    if module.params['transport'] in ['ssh', 'rest']:
        config = contents

        try:
            src = module.from_json(module.params['src'])
        except ValueError:
            module.fail_json(msg='unable to load src due to json parsing error')

        changeset = diff(src, config)
        candidate = merge(changeset, config)

        updates = dict()
        for path, key, new_value, old_value in changeset:
            path = '%s.%s' % ('.'.join(path), key)
            updates[path] = str(new_value)
        result['updates'] = updates

        if changeset:
            if not module.check_mode:
                module.config(config)
            result['changed'] = True

    else:
        candidate = NetworkConfig(contents=module.params['src'], indent=4)

        if contents:
            config = NetworkConfig(contents=contents, indent=4)

        if not module.params['force']:
            commands = candidate.difference(config)
            commands = dumps(commands, 'commands').split('\n')
            commands = [str(c) for c in commands if c]
        else:
            commands = str(candidate).split('\n')

        if commands:
            if not module.check_mode:
                response = module.config(commands)
                result['responses'] = response
            result['changed'] = True

        result['updates'] = commands

    module.exit_json(**result)
开发者ID:2ndQuadrant,项目名称:ansible,代码行数:71,代码来源:_ops_template.py


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