def main():
    """ main entry point for module execution
    """
    neighbors_spec = {
        'name': dict(required=True),
        'activate': dict(type='bool')
    }

    argument_spec = {
        'afi': dict(required=True, choices=['ipv4', 'ipv6', 'evpn']),
        'neighbors': dict(type='list', elements='dict',
                          options=neighbors_spec),
        'replace': dict(type='bool'),
        'state': dict(default='present', choices=['present', 'absent'])
    }

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

    connection = Connection(module._socket_path)

    bgp_as = get_bgp_as(connection)

    if not bgp_as:
        module.fail_json(msg='bgp not configured on this node')

    result = {'changed': False}

    commands = list()

    if module.params['state'] == 'absent':
        commands.extend([
            'router bgp %s' % bgp_as,
            'no address-family %s' % module.params['afi'], 'exit'
        ])

    else:
        commands.append('router bgp %s' % bgp_as)

        if module.params['replace']:
            commands.append('no address-family %s' % module.params['afi'])

        commands.append('address-family %s' % module.params['afi'])
        commands.extend(configure(module))
        commands.append('exit')

    if commands:
        commit = not module.check_mode
        resp = connection.load_config(commands, commit)
        if resp.get('diff'):
            result['changed'] = True
            if module._diff:
                result['diff'] = {'prepared': resp['diff']}

    module.exit_json(**result)
Exemple #2
0
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(name=dict(required=True),
                         remote_as=dict(),
                         maximum_routes=dict(),
                         next_hop_unchanged=dict(type='bool'),
                         update_source=dict(type='bool'),
                         ebgp_multihop=dict(type='int'),
                         send_community_extended=dict(type='bool'),
                         neighbors=dict(type='list'),
                         replace=dict(type='bool'),
                         negate_on_none=dict(type='bool'),
                         state=dict(default='present',
                                    choices=['present', 'absent']))

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

    connection = Connection(module._socket_path)

    name = module.params['name']

    bgp_as = get_bgp_as(connection)
    neighbors = get_bgp_peer_group_neighbors(connection, name)

    if not bgp_as:
        module.fail_json(msg='bgp not configured on this node')

    result = {'changed': False}

    commands = list()

    if module.params['state'] == 'absent':
        commands.extend([
            'router bgp %s' % bgp_as,
            'no neighbor %s' % module.params['name'], 'exit'
        ])

    else:
        if module.params['replace']:
            commands.append('no neighbor %s' % module.params['name'])
        commands.extend(configure(module, bgp_as, neighbors))

    if commands:
        commit = not module.check_mode
        resp = connection.load_config(commands, commit)
        if resp.get('diff'):
            result['changed'] = True
            if module._diff:
                result['diff'] = {'prepared': resp['diff']}

    module.exit_json(**result)
Exemple #3
0
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(protocol=dict(required=True,
                                       choices=['connected', 'static']),
                         route_map=dict(),
                         state=dict(default='present',
                                    choices=['present', 'absent']))

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

    connection = Connection(module._socket_path)

    bgp_as = get_bgp_as(connection)

    result = {'changed': False}

    commands = list()

    if module.params['state'] == 'absent':
        commands.extend([
            'router bgp %s' % bgp_as,
            'no redistribute %s' % module.params['protocol'], 'exit'
        ])

    elif module.params['state'] == 'present':
        commands.extend([
            'router bgp %s' % bgp_as,
            'no redistribute %s' % module.params['protocol']
        ])
        cmd = 'redistribute %s' % module.params['protocol']
        if module.params['route_map'] is not None:
            cmd += ' route-map %s' % module.params['route_map']
        commands.extend([cmd, 'exit'])

    if commands:
        commit = not module.check_mode
        resp = connection.load_config(commands, commit)
        if resp.get('diff'):
            result['changed'] = True
            if module._diff:
                result['diff'] = {'prepared': resp['diff']}

    module.exit_json(**result)
Exemple #4
0
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(bgp_as=dict(required=True, type='int'),
                         router_id=dict(),
                         maximum_paths=dict(type='int'),
                         state=dict(default='present',
                                    choices=['present', 'absent']))

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

    connection = Connection(module._socket_path)
    bgp_as = get_bgp_as(connection)

    result = {'changed': False}

    commands = list()

    if module.params['state'] == 'absent':
        commands.append('no router bgp %s' % module.params['bgp_as'])

    elif module.params['state'] == 'present':
        if bgp_as and bgp_as != module.params['bgp_as']:
            commands.append('no router bgp %s' % bgp_as)
        commands.extend(configure(module))

    if commands:
        commit = not module.check_mode
        resp = connection.load_config(commands, commit)
        if resp.get('diff'):
            result['changed'] = True
            if module._diff:
                result['diff'] = {'prepared': resp['diff']}

    module.exit_json(**result)
Exemple #5
0
def main():
    """ main entry point for module execution
    """
    entries_spec = dict(seqno=dict(type='int', required=True),
                        rule=dict(default='permit', choices=['permit',
                                                             'deny']),
                        prefix=dict(),
                        ge=dict(),
                        le=dict(),
                        eq=dict())

    argument_spec = dict(name=dict(required=True),
                         entries=dict(type='list',
                                      elements='dict',
                                      options=entries_spec),
                         replace=dict(type='bool', default=False),
                         state=dict(default='present',
                                    choices=['present', 'absent']))

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

    connection = Connection(module._socket_path)

    if not check_version(connection, 4, 15):
        module.fail_json('failed version check')

    result = {'changed': False}

    commands = list()

    if module.params['state'] == 'absent':
        out = connection.get('show ip prefix-list %s' % module.params['name'])
        if out:
            commands.append('no ip prefix-list %s' % module.params['name'])

    elif module.params['state'] == 'present':
        if module.params['replace']:
            commands.append('no ip prefix-list %s' % module.params['name'])

        commands.append('ip prefix-list %s' % module.params['name'])

        out = connection.get('show ip prefix-list %s' % module.params['name'])
        config_lines = out.split('\n')

        for item in module.params['entries']:
            entry = configure(item)

            if entry in config_lines:
                break

            seqno = 'seqno %s' % item['seqno']
            if seqno in out:
                commands.append('no seq %s' % module.params['seqno'])

            commands.append(entry)

        commands.append('exit')

    if commands:
        commit = not module.check_mode
        resp = connection.load_config(commands, commit)
        if resp.get('diff'):
            result['changed'] = True
            if module._diff:
                result['diff'] = {'prepared': resp['diff']}

    module.exit_json(**result)
Exemple #6
0
def main():
    """ main entry point for module execution
    """
    entries_spec = {
        'rule': dict(default='permit', choices=['permit', 'deny']),
        'seqno': dict(type='int'),
        'description': dict(),
        'include_route_map': dict(aliases=['sub_route_map']),
        'continue': dict(type='bool'),
        'continue_seqno': dict(type='int'),
        'match_ip_address_prefix_list': dict(),
        'match_ip_address_access_list': dict(),
        'set_tag': dict(type='int')
    }

    argument_spec = dict(name=dict(required=True),
                         entries=dict(type='list',
                                      elements='dict',
                                      options=entries_spec),
                         replace=dict(type='bool', default=False),
                         state=dict(default='present',
                                    choices=['present', 'absent']))

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

    connection = Connection(module._socket_path)

    if not check_version(connection, 4, 15):
        module.fail_json(msg='failed version check')

    result = {'changed': False}

    commands = list()

    if module.params['state'] == 'absent':
        out = connection.get('show route-map %s' % module.params['name'])
        if out:
            commands.append('no route-map %s' % module.params['name'])

    elif module.params['state'] == 'present':
        if module.params['replace']:
            commands.append('no route-map %s' % module.params['name'])

        try:
            for entry in module.params['entries']:
                commands.extend(configure(module.params['name'], entry))
        except ValueError as exc:
            module.fail_json(msg=to_text(exc))

        commands.append('exit')

    if commands:
        commit = not module.check_mode
        resp = connection.load_config(commands, commit)
        if resp.get('diff'):
            result['changed'] = True
            if module._diff:
                result['diff'] = {'prepared': resp['diff']}

    module.exit_json(**result)