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: conn_reload param and up/down states #8897

Merged
merged 6 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions changelogs/fragments/8897-nmcli-add-reload-and-up-down.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- nmcli - add ``conn_enable`` param to reload connection (https://github.com/ansible-collections/community.general/issues/3752, https://github.com/ansible-collections/community.general/issues/8704, https://github.com/ansible-collections/community.general/pull/8897).
- nmcli - add ``state=up`` and ``state=down`` to enable/disable connections (https://github.com/ansible-collections/community.general/issues/3752, https://github.com/ansible-collections/community.general/issues/8704, https://github.com/ansible-collections/community.general/issues/7152, https://github.com/ansible-collections/community.general/pull/8897).
60 changes: 58 additions & 2 deletions plugins/modules/nmcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
state:
description:
- Whether the device should exist or not, taking action if the state is different from what is stated.
- Using O(state=present) to create connection will automatically bring connection up.
- Using O(state=up) and O(state=down) will not modify connection with other parameters.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to mention that those states have been introduced in community.general 9.5.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to add these beacuse version_added is for new params rather then updated ones
Please check last commit if I did it right

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Textually, like:

Suggested change
- Using O(state=present) to create connection will automatically bring connection up.
- Using O(state=up) and O(state=down) will not modify connection with other parameters.
- Using O(state=present) to create connection will automatically bring connection up.
- Using O(state=up) and O(state=down) will not modify connection with other parameters. These states have been added in community.general 9.5.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

type: str
required: true
choices: [ absent, present ]
choices: [ absent, present, up, down ]
autoconnect:
description:
- Whether the connection should start on boot.
Expand All @@ -48,6 +50,12 @@
- The name used to call the connection. Pattern is <type>[-<ifname>][-<num>].
type: str
required: true
conn_reload:
description:
- Whether the connection should be reloaded if it was modified.
type: bool
required: false
default: false
ifname:
description:
- The interface to bind the connection to.
Expand Down Expand Up @@ -1309,6 +1317,25 @@
type: ethernet
state: present

- name: Change the property of a setting e.g. MTU and reload connection
community.general.nmcli:
conn_name: my-eth1
mtu: 1500
type: ethernet
state: present
conn_reload: true

- name: Disable connection
community.general.nmcli:
conn_name: my-eth1
state: down

- name: Reload and enable connection
community.general.nmcli:
conn_name: my-eth1
state: up
reload: true

- name: Add second ip4 address
community.general.nmcli:
conn_name: my-eth1
Expand Down Expand Up @@ -1581,6 +1608,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.conn_reload = module.params['conn_reload']
self.slave_type = module.params['slave_type']
self.master = module.params['master']
self.ifname = module.params['ifname']
Expand Down Expand Up @@ -2165,6 +2193,10 @@ def up_connection(self):
cmd = [self.nmcli_bin, 'con', 'up', self.conn_name]
return self.execute_command(cmd)

def reload_connection(self):
cmd = [self.nmcli_bin, 'con', 'reload']
return self.execute_command(cmd)

def connection_update(self, nmcli_command):
if nmcli_command == 'create':
cmd = [self.nmcli_bin, 'con', 'add', 'type']
Expand Down Expand Up @@ -2431,8 +2463,9 @@ def main():
argument_spec=dict(
ignore_unsupported_suboptions=dict(type='bool', default=False),
autoconnect=dict(type='bool', default=True),
state=dict(type='str', required=True, choices=['absent', 'present']),
state=dict(type='str', required=True, choices=['absent', 'present', 'up', 'down']),
conn_name=dict(type='str', required=True),
conn_reload=dict(type='bool', required=False, default=False),
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
master=dict(type='str'),
slave_type=dict(type='str', choices=['bond', 'bridge', 'team', 'ovs-port']),
ifname=dict(type='str'),
Expand Down Expand Up @@ -2639,6 +2672,8 @@ def main():
if module.check_mode:
module.exit_json(changed=True, **result)
(rc, out, err) = nmcli.modify_connection()
if nmcli.conn_reload:
(rc, out, err) = nmcli.reload_connection()
else:
result['Exists'] = 'Connections already exist and no changes made'
if module.check_mode:
Expand All @@ -2650,6 +2685,27 @@ def main():
(rc, out, err) = nmcli.create_connection()
if rc is not None and rc != 0:
module.fail_json(name=nmcli.conn_name, msg=err, rc=rc)

elif nmcli.state == 'up':
if nmcli.connection_exists():
if module.check_mode:
module.exit_json(changed=True)
if nmcli.conn_reload:
(rc, out, err) = nmcli.reload_connection()
(rc, out, err) = nmcli.up_connection()
if rc != 0:
module.fail_json(name=('No Connection named %s exists' % nmcli.conn_name), msg=err, rc=rc)

elif nmcli.state == 'down':
if nmcli.connection_exists():
if module.check_mode:
module.exit_json(changed=True)
if nmcli.conn_reload:
(rc, out, err) = nmcli.reload_connection()
(rc, out, err) = nmcli.down_connection()
if rc != 0:
module.fail_json(name=('No Connection named %s exists' % nmcli.conn_name), msg=err, rc=rc)

except NmcliModuleError as e:
module.fail_json(name=nmcli.conn_name, msg=str(e))

Expand Down
1 change: 1 addition & 0 deletions tests/unit/plugins/modules/test_nmcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4251,6 +4251,7 @@ def test_bond_connection_unchanged(mocked_generic_connection_diff_check, capfd):
autoconnect=dict(type='bool', default=True),
state=dict(type='str', required=True, choices=['absent', 'present']),
conn_name=dict(type='str', required=True),
conn_reload=dict(type='bool', required=False, default=False),
master=dict(type='str'),
slave_type=dict(type=str, choices=['bond', 'bridge', 'team']),
ifname=dict(type='str'),
Expand Down