Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nmcli: added new module option 'slave_type' to allow create non-ethernet slave connections #6108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/473-nmcli-slave-type-implemented.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- nmcli - new module option ``slave_type`` added to allow creation of various types of slave devices (https://github.com/ansible-collections/community.general/issues/473, https://github.com/ansible-collections/community.general/pull/6108).
103 changes: 95 additions & 8 deletions plugins/modules/nmcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
- Type C(macvlan) is added in community.general 6.6.0.
- Type C(wireguard) is added in community.general 4.3.0.
- Type C(vpn) is added in community.general 5.1.0.
- Using C(bond-slave), C(bridge-slave) or C(team-slave) implies C(ethernet) connection type with corresponding I(slave_type) option.
- If you want to control non-ethernet connection attached to C(bond), C(bridge) or C(team) consider using C(slave_type) option.
type: str
choices: [ bond, bond-slave, bridge, bridge-slave, dummy, ethernet, generic, gre, infiniband, ipip, macvlan, sit, team, team-slave, vlan, vxlan,
wifi, gsm, wireguard, vpn ]
Expand All @@ -81,9 +83,16 @@
type: str
choices: [ datagram, connected ]
version_added: 5.8.0
slave_type:
description:
- Type of the device of this slave's master connection (for example C(bond)).
type: str
choices: [ 'bond', 'bridge', 'team' ]
version_added: 7.0.0
master:
description:
- Master <master (ifname, or connection UUID or conn_name) of bridge, team, bond master connection profile.
- Mandatory if I(slave_type) is defined.
type: str
ip4:
description:
Expand Down Expand Up @@ -1429,6 +1438,39 @@
autoconnect: false
state: present

## Creating bond attached to bridge example
- name: Create bond attached to bridge
community.general.nmcli:
type: bond
conn_name: bond0
slave_type: bridge
master: br0
state: present

- name: Create master bridge
community.general.nmcli:
type: bridge
conn_name: br0
method4: disabled
method6: disabled
state: present

## Creating vlan connection attached to bridge
- name: Create master bridge
community.general.nmcli:
type: bridge
conn_name: br0
state: present

- name: Create VLAN 5
community.general.nmcli:
type: vlan
conn_name: eth0.5
slave_type: bridge
master: br0
vlandev: eth0
vlanid: 5
state: present
'''

RETURN = r"""#
Expand Down Expand Up @@ -1475,6 +1517,7 @@ def __init__(self, module):
self.ignore_unsupported_suboptions = module.params['ignore_unsupported_suboptions']
self.autoconnect = module.params['autoconnect']
self.conn_name = module.params['conn_name']
self.slave_type = module.params['slave_type']
self.master = module.params['master']
self.ifname = module.params['ifname']
self.type = module.params['type']
Expand Down Expand Up @@ -1570,6 +1613,14 @@ def __init__(self, module):

self.edit_commands = []

self.extra_options_validation()

def extra_options_validation(self):
""" Additional validation of options set passed to module that cannot be implemented in module's argspecs. """
if self.type not in ("bridge-slave", "team-slave", "bond-slave"):
if self.master is None and self.slave_type is not None:
self.module.fail_json(msg="'master' option is required when 'slave_type' is specified.")

def execute_command(self, cmd, use_unsafe_shell=False, data=None):
if isinstance(cmd, list):
cmd = [to_text(item) for item in cmd]
Expand Down Expand Up @@ -1634,6 +1685,7 @@ def connection_options(self, detect_change=False):
if self.slave_conn_type:
heaveaxy marked this conversation as resolved.
Show resolved Hide resolved
options.update({
'connection.master': self.master,
'connection.slave-type': self.slave_type,
heaveaxy marked this conversation as resolved.
Show resolved Hide resolved
})

# Options specific to a connection type.
Expand All @@ -1649,9 +1701,17 @@ def connection_options(self, detect_change=False):
'xmit_hash_policy': self.xmit_hash_policy,
})
elif self.type == 'bond-slave':
options.update({
'connection.slave-type': 'bond',
})
if self.slave_type and self.slave_type != 'bond':
self.module.fail_json(msg="Connection type '%s' cannot be combined with '%s' slave-type. "
"Allowed slave-type for '%s' is 'bond'."
% (self.type, self.slave_type, self.type)
)
if not self.slave_type:
self.module.warn("Connection 'slave-type' property automatically set to 'bond' "
"because of using 'bond-slave' connection type.")
options.update({
'connection.slave-type': 'bond',
})
elif self.type == 'bridge':
options.update({
'bridge.ageing-time': self.ageingtime,
Expand All @@ -1675,16 +1735,36 @@ def connection_options(self, detect_change=False):
'team.runner-fast-rate': self.runner_fast_rate,
})
elif self.type == 'bridge-slave':
if self.slave_type and self.slave_type != 'bridge':
self.module.fail_json(msg="Connection type '%s' cannot be combined with '%s' slave-type. "
"Allowed slave-type for '%s' is 'bridge'."
% (self.type, self.slave_type, self.type)
)
if not self.slave_type:
self.module.warn("Connection 'slave-type' property automatically set to 'bridge' "
"because of using 'bridge-slave' connection type.")
options.update({'connection.slave-type': 'bridge'})
self.module.warn(
"Connection type as 'bridge-slave' implies 'ethernet' connection with 'bridge' slave-type. "
"Consider using slave_type='bridge' with necessary type."
)
options.update({
'connection.slave-type': 'bridge',
'bridge-port.path-cost': self.path_cost,
'bridge-port.hairpin-mode': self.hairpin,
'bridge-port.priority': self.slavepriority,
})
elif self.type == 'team-slave':
options.update({
'connection.slave-type': 'team',
})
if self.slave_type and self.slave_type != 'team':
self.module.fail_json(msg="Connection type '%s' cannot be combined with '%s' slave-type. "
"Allowed slave-type for '%s' is 'team'."
% (self.type, self.slave_type, self.type)
)
if not self.slave_type:
self.module.warn("Connection 'slave-type' property automatically set to 'team' "
"because of using 'team-slave' connection type.")
options.update({
'connection.slave-type': 'team',
})
elif self.tunnel_conn_type:
options.update({
'ip-tunnel.local': self.ip_tunnel_local,
Expand Down Expand Up @@ -1869,6 +1949,12 @@ def ip6_privacy_to_num(privacy):
@property
def slave_conn_type(self):
return self.type in (
'ethernet',
'bridge',
'bond',
'vlan',
'team',
'wifi',
'bond-slave',
'bridge-slave',
'team-slave',
Expand Down Expand Up @@ -2260,6 +2346,7 @@ def main():
state=dict(type='str', required=True, choices=['absent', 'present']),
conn_name=dict(type='str', required=True),
master=dict(type='str'),
slave_type=dict(type='str', choices=['bond', 'bridge', 'team']),
ifname=dict(type='str'),
type=dict(type='str',
choices=[
Expand Down Expand Up @@ -2416,7 +2503,7 @@ def main():
if nmcli.runner_fast_rate is not None and nmcli.runner != "lacp":
nmcli.module.fail_json(msg="runner-fast-rate is only allowed for runner lacp")
# team-slave checks
if nmcli.type == 'team-slave':
if nmcli.type == 'team-slave' or nmcli.slave_type == 'team':
if nmcli.master is None:
nmcli.module.fail_json(msg="Please specify a name for the master when type is %s" % nmcli.type)
if nmcli.ifname is None:
Expand Down
Loading