コード例 #1
0
    def absent_ipv4(self, want, have):
        commands = []

        key = 'ipv4'
        want_ipv4 = want.get(key)
        have_ipv4 = have.get(key)

        if not want_ipv4 and not have_ipv4:
            pass
        elif not want_ipv4 and have_ipv4:
            # キーは存在するので、
            # ipv4:
            # という、空文字列で指定されたということ。
            # この場合は、ipv4アドレスを消したい、という意味だと捉えて設定されているアドレスを消す
            tokens = have_ipv4.split('/')
            ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
            commands.append('no ip address {}'.format(ipv4))
        elif want_ipv4 and not have_ipv4:
            pass
        elif want_ipv4 and have_ipv4:
            if want_ipv4 == have_ipv4:
                # 同じものが設定されているそれを消す
                tokens = have_ipv4.split('/')
                if len(tokens) == 2:
                    ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
                    commands.append('no ip address {}'.format(ipv4))
            else:
                # 指定されたものとは違うものが設定されている。それを消すかどうかだけど・・・ここでは消さない
                pass

        return commands
コード例 #2
0
    def absent_ipv4_secondary(self, want, have):
        commands = []

        key = 'ipv4_secondary'
        want_secondary = want.get(key)
        have_secondary = have.get(key)

        if not want_secondary and not have_secondary:
            pass
        elif not want_secondary and have_secondary:
            # キーは存在するので、
            # ipv4_secondary:
            # という、空文字列で指定されたということ。
            # この場合は、セカンダリアドレスを全て消したい、という意味だと捉えて設定されているアドレスを消す
            for prefix in have_secondary:
                tokens = prefix.split('/')
                if len(tokens) == 2:
                    ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
                    commands.append('no ip address {} secondary'.format(ipv4))
        elif want_secondary and not have_secondary:
            pass
        elif want_secondary and have_secondary:
            superfluous = set(have_secondary).difference(set(want_secondary))
            for prefix in superfluous:
                tokens = prefix.split('/')
                if len(tokens) == 2:
                    ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
                    commands.append('no ip address {} secondary'.format(ipv4))

        return commands
コード例 #3
0
def map_obj_to_commands(updates, module):
    commands = list()
    want, have = updates
    for w in want:
        name = w["name"]
        ipv4 = w["ipv4"]
        ipv6 = w["ipv6"]
        state = w["state"]

        interface = "interface " + name
        commands.append(interface)

        obj_in_have = search_obj_in_list(name, have)
        if state == "absent" and obj_in_have:
            if obj_in_have["ipv4"]:
                if ipv4:
                    address = ipv4.split("/")
                    if len(address) == 2:
                        ipv4 = "{0} {1}".format(
                            address[0], to_netmask(address[1])
                        )
                    commands.append("no ip address {0}".format(ipv4))
                else:
                    commands.append("no ip address")
            if obj_in_have["ipv6"]:
                if ipv6:
                    commands.append("no ipv6 address {0}".format(ipv6))
                else:
                    commands.append("no ipv6 address")
                    if "dhcp" in obj_in_have["ipv6"]:
                        commands.append("no ipv6 address dhcp")

        elif state == "present":
            if ipv4:
                if (
                    obj_in_have is None
                    or obj_in_have.get("ipv4") is None
                    or ipv4 != obj_in_have["ipv4"]
                ):
                    address = ipv4.split("/")
                    if len(address) == 2:
                        ipv4 = "{0} {1}".format(
                            address[0], to_netmask(address[1])
                        )
                    commands.append("ip address {0}".format(ipv4))

            if ipv6:
                if (
                    obj_in_have is None
                    or obj_in_have.get("ipv6") is None
                    or ipv6.lower()
                    not in [addr.lower() for addr in obj_in_have["ipv6"]]
                ):
                    commands.append("ipv6 address {0}".format(ipv6))

        if commands[-1] == interface:
            commands.pop(-1)

    return commands
コード例 #4
0
ファイル: ios_l3_interface.py プロジェクト: sergun4ik/ansible
def map_obj_to_commands(updates, module):
    commands = list()
    want, have = updates
    for w in want:
        name = w['name']
        ipv4 = w['ipv4']
        ipv6 = w['ipv6']
        state = w['state']
        updown = w['updown']

        interface = 'interface ' + name
        commands.append(interface)

        obj_in_have = search_obj_in_list(name, have)
        if state == 'absent' and obj_in_have:
            if obj_in_have['ipv4']:
                if ipv4:
                    address = ipv4.split('/')
                    if len(address) == 2:
                        ipv4 = '{0} {1}'.format(address[0], to_netmask(address[1]))
                    commands.append('no ip address {0}'.format(ipv4))
                else:
                    commands.append('no ip address')
            if obj_in_have['ipv6']:
                if ipv6:
                    commands.append('no ipv6 address {0}'.format(ipv6))
                else:
                    commands.append('no ipv6 address')
                    if 'dhcp' in obj_in_have['ipv6']:
                        commands.append('no ipv6 address dhcp')

        elif state == 'present':
            if updown == 'up':
                commands.append('no shutdown')
            elif updown == 'down':
                commands.append('shutdown')

            if ipv4:
                if obj_in_have is None or obj_in_have.get('ipv4') is None or ipv4 != obj_in_have['ipv4']:
                    address = ipv4.split('/')
                    if len(address) == 2:
                        ipv4 = '{0} {1}'.format(address[0], to_netmask(address[1]))
                    commands.append('ip address {0}'.format(ipv4))

            if ipv6:
                if obj_in_have is None or obj_in_have.get('ipv6') is None or ipv6.lower() not in [addr.lower() for addr in obj_in_have['ipv6']]:
                    commands.append('ipv6 address {0}'.format(ipv6))

        if commands[-1] == interface:
            commands.pop(-1)

    return commands
コード例 #5
0
    def present_ipv4_secondary(self, want, have):
        commands = []

        key = 'ipv4_secondary'
        want_secondary = want.get(key)
        have_secondary = have.get(key)

        if not want_secondary and not have_secondary:
            pass
        elif not want_secondary and have_secondary:
            # キーは存在するので、
            # ipv4_secondary:
            # という、空文字列で指定されたということ。
            # この場合は、セカンダリアドレスを存在しない状態にしたい、という意味だと捉えて設定されているアドレスを消す
            for prefix in have_secondary:
                tokens = prefix.split('/')
                if len(tokens) == 2:
                    ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
                    commands.append('no ip address {} secondary'.format(ipv4))
        elif want_secondary and not have_secondary:
            # 新規にセカンダリアドレスを設定
            for prefix in want_secondary:
                tokens = prefix.split('/')
                ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
                commands.append('ip address {} secondary'.format(ipv4))
        elif want_secondary and have_secondary:
            # 不足する分を追加で設定する
            missing = set(want_secondary).difference(set(have_secondary))
            for prefix in missing:
                tokens = prefix.split('/')
                if len(tokens) == 2:
                    ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
                    commands.append('ip address {} secondary'.format(ipv4))

            if want.get('purge') is True:
                # 余分なものはpurgeする
                superfluous = set(have_secondary).difference(
                    set(want_secondary))
                for prefix in superfluous:
                    tokens = prefix.split('/')
                    if len(tokens) == 2:
                        ipv4 = '{0} {1}'.format(tokens[0],
                                                to_netmask(tokens[1]))
                        commands.append(
                            'no ip address {} secondary'.format(ipv4))

        return commands
コード例 #6
0
ファイル: utils.py プロジェクト: ragurampvs/ansible-1
def validate_n_expand_ipv4(module, want):
    # Check if input IPV4 is valid IP and expand IPV4 with its subnet mask
    ip_addr_want = want.get('address')
    validate_ipv4(ip_addr_want, module)
    ip = ip_addr_want.split('/')
    if len(ip) == 2:
        ip_addr_want = '{0} {1}'.format(ip[0], to_netmask(ip[1]))

    return ip_addr_want
コード例 #7
0
def map_obj_to_commands(updates, module):
    commands = list()
    want, have = updates
    for w in want:
        name = w['name']
        ipv4 = w['ipv4']
        ipv6 = w['ipv6']
        state = w['state']

        interface = 'interface ' + name
        commands.append(interface)

        obj_in_have = search_obj_in_list(name, have)
        if state == 'absent' and obj_in_have:
            if obj_in_have['ipv4']:
                if ipv4:
                    address = ipv4.split('/')
                    if len(address) == 2:
                        ipv4 = '{0} {1}'.format(address[0], to_netmask(address[1]))
                    commands.append('no ip address {}'.format(ipv4))
                else:
                    commands.append('no ip address')
            if obj_in_have['ipv6']:
                if ipv6:
                    commands.append('no ipv6 address {}'.format(ipv6))
                else:
                    commands.append('no ipv6 address')

        elif state == 'present':
            if ipv4:
                if obj_in_have is None or obj_in_have.get('ipv4') is None or ipv4 != obj_in_have['ipv4']:
                    address = ipv4.split('/')
                    if len(address) == 2:
                        ipv4 = '{0} {1}'.format(address[0], to_netmask(address[1]))
                    commands.append('ip address {}'.format(ipv4))

            if ipv6:
                if obj_in_have is None or obj_in_have.get('ipv6') is None or ipv6.lower() != obj_in_have['ipv6'].lower():
                    commands.append('ipv6 address {}'.format(ipv6))

        if commands[-1] == interface:
            commands.pop(-1)

    return commands
コード例 #8
0
    def present_ipv4(self, want, have):
        commands = []

        key = 'ipv4'
        want_ipv4 = want.get(key)
        have_ipv4 = have.get(key)

        if not want_ipv4 and not have_ipv4:
            pass
        elif not want_ipv4 and have_ipv4:
            # キーは存在するので、
            # ipv4:
            # という、空文字列で指定されたということ。
            # この場合は、ipv4アドレスを空っぽの状態にしたい、という意味だと捉えて設定されているアドレスを消す
            tokens = have_ipv4.split('/')
            ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
            commands.append('no ip address {}'.format(ipv4))
        elif want_ipv4 and not have_ipv4:
            # 新規にアドレスを設定
            tokens = want_ipv4.split('/')
            ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
            commands.append('ip address {}'.format(ipv4))
        elif want_ipv4 and have_ipv4:
            if want_ipv4 == have_ipv4:
                # 同じものが設定されているなら何もしない
                pass
            else:
                # 安全のため既存の設定をnoで削除してから新規でアドレスを設定する
                tokens = have_ipv4.split('/')
                if len(tokens) == 2:
                    ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
                    commands.append('no ip address {}'.format(ipv4))
                tokens = want_ipv4.split('/')
                if len(tokens) == 2:
                    ipv4 = '{0} {1}'.format(tokens[0], to_netmask(tokens[1]))
                    commands.append('ip address {}'.format(ipv4))

        return commands
コード例 #9
0
def map_obj_to_commands(updates, module):
    commands = list()
    want, have = updates
    for w in want:
        name = w['name']
        ipv4 = w['ipv4']
        ipv6 = w['ipv6']
        state = w['state']
        if 'replace' in w:
            replace = w['replace'] == 'yes'
        else:
            replace = False
        if w['mode'] is not None:
            mode = ' ' + w['mode']
        else:
            mode = ''
        if w['secondary'] is not None:
            secondary = w['secondary'] == 'yes'
        else:
            secondary = False

        interface = 'interface ' + name
        commands.append(interface)

        obj_in_have = search_obj_in_list(name, have)
        if state == 'absent' and have == []:
            if ipv4:
                address = ipv4.split('/')
                if len(address) == 2:
                    ipv4 = '{addr} {mask}'.format(addr=address[0],
                                                  mask=to_netmask(address[1]))
                commands.append('no ip address {ip}'.format(ip=ipv4))
            if ipv6:
                commands.append('no ipv6 address {ip}'.format(ip=ipv6))

        elif state == 'absent' and obj_in_have:
            if obj_in_have['ipv4']:
                if ipv4:
                    address = ipv4.split('/')
                    if len(address) == 2:
                        ipv4 = '{addr} {mask}'.format(addr=address[0],
                                                      mask=to_netmask(
                                                          address[1]))
                    commands.append('no ip address {ip}'.format(ip=ipv4))
            if obj_in_have['ipv6']:
                if ipv6:
                    commands.append('no ipv6 address {ip}'.format(ip=ipv6))

        elif state == 'present':
            if ipv4:
                if obj_in_have is None or obj_in_have.get(
                        'ipv4') is None or ipv4 != obj_in_have['ipv4']:
                    address = ipv4.split('/')
                    if len(address) == 2:
                        ipv4 = '{0} {1}'.format(address[0],
                                                to_netmask(address[1]))
                    commands.append('ip address %s%s%s%s' %
                                    (format(ipv4), mode, ' replace' if
                                     (replace) else '', ' secondary' if
                                     (secondary) else ''))

            if ipv6:
                if obj_in_have is None or obj_in_have.get(
                        'ipv6') is None or ipv6.lower() not in [
                            addr.lower() for addr in obj_in_have['ipv6']
                        ]:
                    commands.append('ipv6 address {ip}'.format(ip=ipv6))

        if commands[-1] == interface:
            commands.pop(-1)
        else:
            commands.append("exit")

    return commands
コード例 #10
0
ファイル: test_utils.py プロジェクト: awiddersheim/ansible
def test_to_netmask_invalid():
    with pytest.raises(ValueError):
        to_netmask(128)
コード例 #11
0
ファイル: test_utils.py プロジェクト: awiddersheim/ansible
def test_to_netmask():
    assert '255.0.0.0' == to_netmask(8)
    assert '255.0.0.0' == to_netmask('8')
コード例 #12
0
def test_to_netmask_invalid():
    with pytest.raises(ValueError):
        to_netmask(128)
コード例 #13
0
def test_to_netmask():
    assert '255.0.0.0' == to_netmask(8)
    assert '255.0.0.0' == to_netmask('8')