Add options field to bond structure. (#38512)

This change allows users to pass bond options
alongside bond mode, in order to allow greater
flexibility for bond creation. Previously,
bond options were derived from bond mode, e.g.
"miimon" was set to 100 for mode 3 (load-balance),
however a user may want to use 200.
This commit is contained in:
leongold 2018-04-12 11:09:50 +03:00 committed by ansibot
parent 91da1653e0
commit b9d97d85f6

View file

@ -1,7 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# Copyright (c) 2016 Red Hat, Inc. # Copyright (c) 2016, 2018 Red Hat, Inc.
# #
# This file is part of Ansible # This file is part of Ansible
# #
@ -47,6 +47,7 @@ options:
- "Dictionary describing network bond:" - "Dictionary describing network bond:"
- "C(name) - Bond name." - "C(name) - Bond name."
- "C(mode) - Bonding mode." - "C(mode) - Bonding mode."
- "C(options) - Bonding options."
- "C(interfaces) - List of interfaces to create a bond." - "C(interfaces) - List of interfaces to create a bond."
interface: interface:
description: description:
@ -96,6 +97,19 @@ EXAMPLES = '''
gateway: 1.2.3.4 gateway: 1.2.3.4
version: v4 version: v4
# Create bond on eth1 and eth2 interface, specifiyng both mode and miimon:
- name: Bonds
ovirt_host_networks:
name: myhost
bond:
name: bond0
mode: 1
options:
miimon: 200
interfaces:
- eth1
- eth2
# Remove bond0 bond from host interfaces: # Remove bond0 bond from host interfaces:
- ovirt_host_networks: - ovirt_host_networks:
state: absent state: absent
@ -146,6 +160,7 @@ try:
except ImportError: except ImportError:
pass pass
from ansible.module_utils import six
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ovirt import ( from ansible.module_utils.ovirt import (
BaseModule, BaseModule,
@ -160,15 +175,17 @@ from ansible.module_utils.ovirt import (
) )
def get_mode_type(mode_number): def get_bond_options(mode, usr_opts):
""" MIIMON_100 = dict(miimon='100')
Adaptive transmit load balancing (balance-tlb): mode=1 miimon=100 DEFAULT_MODE_OPTS = {
Dynamic link aggregation (802.3ad): mode=2 miimon=100 '1': MIIMON_100,
Load balance (balance-xor): mode=3 miimon=100 '2': MIIMON_100,
Active-Backup: mode=4 miimon=100 xmit_hash_policy=2 '3': MIIMON_100,
""" '4': dict(xmit_hash_policy='2', **MIIMON_100)
}
options = [] options = []
if mode_number is None: if mode is None:
return options return options
def get_type_name(mode_number): def get_type_name(mode_number):
@ -176,32 +193,36 @@ def get_mode_type(mode_number):
We need to maintain this type strings, for the __compare_options method, We need to maintain this type strings, for the __compare_options method,
for easier comparision. for easier comparision.
""" """
return [ modes = [
'Active-Backup', 'Active-Backup',
'Load balance (balance-xor)', 'Load balance (balance-xor)',
None, None,
'Dynamic link aggregation (802.3ad)', 'Dynamic link aggregation (802.3ad)',
][mode_number] ]
if (not 0 < mode_number <= len(modes) - 1):
return None
return modes[mode_number - 1]
try: try:
mode_number = int(mode_number) mode_number = int(mode)
if mode_number >= 1 and mode_number <= 4: except ValueError:
if mode_number == 4: raise Exception('Bond mode must be a number.')
options.append(otypes.Option(name='xmit_hash_policy', value='2'))
options.append(otypes.Option(name='miimon', value='100'))
options.append( options.append(
otypes.Option( otypes.Option(
name='mode', name='mode',
type=get_type_name(mode_number - 1), type=get_type_name(mode_number),
value=str(mode_number) value=str(mode_number)
) )
) )
else:
options.append(otypes.Option(name='mode', value=str(mode_number)))
except ValueError:
raise Exception("Bond mode must be a number.")
opts_dict = DEFAULT_MODE_OPTS.get(mode, {})
opts_dict.update(**usr_opts)
options.extend(
[otypes.Option(name=opt, value=value)
for opt, value in six.iteritems(opts_dict)]
)
return options return options
@ -250,7 +271,7 @@ class HostNetworksModule(BaseModule):
# Check if bond configuration should be updated: # Check if bond configuration should be updated:
if bond: if bond:
update = self.__compare_options(get_mode_type(bond.get('mode')), getattr(nic.bonding, 'options', [])) update = self.__compare_options(get_bond_options(bond.get('mode'), bond.get('options')), getattr(nic.bonding, 'options', []))
update = update or not equal( update = update or not equal(
sorted(bond.get('interfaces')) if bond.get('interfaces') else None, sorted(bond.get('interfaces')) if bond.get('interfaces') else None,
sorted(get_link_name(self._connection, s) for s in nic.bonding.slaves) sorted(get_link_name(self._connection, s) for s in nic.bonding.slaves)
@ -369,7 +390,7 @@ def main():
otypes.HostNic( otypes.HostNic(
name=bond.get('name'), name=bond.get('name'),
bonding=otypes.Bonding( bonding=otypes.Bonding(
options=get_mode_type(bond.get('mode')), options=get_bond_options(bond.get('mode'), bond.get('options')),
slaves=[ slaves=[
otypes.HostNic(name=i) for i in bond.get('interfaces', []) otypes.HostNic(name=i) for i in bond.get('interfaces', [])
], ],