ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
827 stars 1.53k forks source link

apply nmcli configuration #3752

Closed avbg closed 1 month ago

avbg commented 2 years ago

Summary

Problem: how to apply the configuration on the host after launching the "nmcli" module? Now I do it through the nmcli up handler. maybe there is another option that I don't know about?

I expected that with the "present" state, they would be applied, since this is the internal mechanism of the NetworkManager. Perhaps a new state should be added for this?

ansible --version
ansible [core 2.11.1] 
  config file = /srv/infra/itpark/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/pythonEnvs/ansible/lib/python3.9/site-packages/ansible
  ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/user/pythonEnvs/ansible/bin/ansible
  python version = 3.9.7 (default, Oct 10 2021, 15:13:22) [GCC 11.1.0]
  jinja version = 3.0.1
  libyaml = True

Issue Type

Feature Idea

Component Name

nmcli

Additional Information

Code of Conduct

ansibullbot commented 2 years ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 2 years ago

cc @alcamie101 click here for bot help

Ajpantuso commented 2 years ago

NetworkManager doesn't apply changes to an active connection until the configuration is reloaded. In Ansible you would typically notify a handler to either restart NetworkManager or run nmcli con reload. If we added an option to do this it probably wouldn't be a new state, but something like reload which when set to True would toggle active connections with con down and then con up.

avbg commented 2 years ago

It's not so important through an option or through a state. it is much more convenient when inside the module, and not through the handler.

ansibullbot commented 2 years ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

rptaylor commented 1 year ago

Different operations can require different commands to apply them. Usually restarting NetworkManager is not the recommended/desired thing to do except for some special cases (0 1 2)

It is not necessary or recommended to restart NetworkManager to reload interface configuration files.

Note that a restart of the NetworkManager service is usually not required, for example when doing changes to the networking configuration. This is one of the exceptions.

Even nmcli con reload may not work in some cases:

Note: This action only makes NetworkManager aware of any changes to configuration, it does not apply changes to an already-up connection.

Personally I have found it pretty confusing to determine which operation is needed to apply what kind of change when using NetworkManager, which in turn makes it difficult as a user to try to make the nmcli module apply changes, even if I had to write a handler for every invocation of the module which would be very cumbersome.

Often nmcli dev reapply devicename is the right thing to do, but not always, e.g.

# nmcli dev reapply bond0
Error: Reapplying connection to device 'bond0' (/org/freedesktop/NetworkManager/Devices/14) failed: Can't reapply 'lacp_rate' bond option

so in that case nmcli down/up bond0 is required. That makes a total of 4 (at least) different possible commands that may be required to apply a nmcli change!

Anyway even just considering the one case of nmcli dev reapply devicename, making a handler to reapply the device that has been changed would require a variable (apparently using variables in a handler requires include_vars ???) and more complexity in crafting tasks properly compared to the module just doing a reapply while it is modifying a device.

Preferably the ansible module would "do the right thing" to apply whatever type of change is made with some option apply or reload (default True).