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: avoid changed status for most cases with VPN connections #5126

Merged
merged 4 commits into from Sep 3, 2022
Merged

nmcli: avoid changed status for most cases with VPN connections #5126

merged 4 commits into from Sep 3, 2022

Conversation

yan12125
Copy link
Contributor

SUMMARY

Follow-up #4746

  • nmcli connection show includes vpn.service-type but not vpn-type.
    Switching to vpn.service-type removes unneeded diffs while keeping
    the same functionality, as vpn-type is an alias of vpn.service-type
    per nm-settings-nmcli(1).

    NetworkManager also adds org.freedesktop.NetworkManager. prefix for
    known VPN types [1]. The logic is non-trivial so I didn't implement it
    in this commit. If a user specifies service-type: l2tp, changed will
    be always be True:

-    "vpn.service-type": "org.freedesktop.NetworkManager.l2tp"
+    "vpn.service-type": "l2tp"
  • The vpn.data field from nmcli connection show is sorted by keys and
    there are spaces around equal signs. I added codes for parsing such
    data.

Tests are also updated to match outputs of nmcli commands.

[1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/blob/1.38.4/src/libnm-core-impl/nm-vpn-plugin-info.c#L619

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

nmcli

ADDITIONAL INFORMATION

N/A

Follow-up #4746

* `nmcli connection show` includes vpn.service-type but not vpn-type.
  Switching to vpn.service-type removes unneeded diffs while keeping
  the same functionality, as vpn-type is an alias of vpn.service-type
  per nm-settings-nmcli(1).

  NetworkManager also adds `org.freedesktop.NetworkManager.` prefix for
  known VPN types [1]. The logic is non-trivial so I didn't implement it
  in this commit. If a user specifies `service-type: l2tp`, changed will
  be always be True:

    -    "vpn.service-type": "org.freedesktop.NetworkManager.l2tp"
    +    "vpn.service-type": "l2tp"

* The vpn.data field from `nmcli connection show` is sorted by keys and
  there are spaces around equal signs. I added codes for parsing such
  data.

Tests are also updated to match outputs of nmcli commands.

[1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/blob/1.38.4/src/libnm-core-impl/nm-vpn-plugin-info.c#L619
@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added bug This issue/PR relates to a bug module module net_tools plugins plugin (any type) tests tests unit tests/unit labels Aug 18, 2022
@github-actions
Copy link

github-actions bot commented Aug 18, 2022

Docs Build 📝

Thank you for contribution!✨

This PR has been merged and your docs changes will be incorporated when they are next published.

Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

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

CC @jremerich who implemented this originally.

plugins/modules/net_tools/nmcli.py Outdated Show resolved Hide resolved
plugins/modules/net_tools/nmcli.py Outdated Show resolved Hide resolved
plugins/modules/net_tools/nmcli.py Outdated Show resolved Hide resolved
plugins/modules/net_tools/nmcli.py Outdated Show resolved Hide resolved
@@ -1670,7 +1670,7 @@ def connection_options(self, detect_change=False):
for name, value in self.vpn.items():
if name == 'service-type':
options.update({
'vpn-type': value,
'vpn.service-type': value,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are vpn-type and vpn.service-type always exchangeable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like vpn-type is an alias of vpn.service-type since NetworkManager 1.4 [1] (released in Aug 2016). How old NetworkManager should this collection support?

[1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/commit/c3422e917d75b48bcfde9036caec61bf97d6c312

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, that's a very good question, and one I think next to impossible to answer. It seems that the module does not say which versions of nmcli it supports, and never mentioned that in the past. So the answer would be: "every version that the module supported in the past and that it didn't explicitly drop support for (which is probably none)".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apparently current nmcli.py does not work with very old NetworkManager, anyway. With NetworkManager 1.2.6 on Ubuntu 16.04, I got:

$ ansible some_old_machine -m nmcli -a "conn_name=foo state=present type=ethernet ifname=foo" --become
some_old_machine | FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "msg": "Error: mandatory 'ifname' not seen before 'ipv6.ignore-auto-dns'.\n",
    "name": "foo",
    "rc": 2
}

The used command is:

/usr/bin/nmcli con add type ethernet con-name foo ipv6.ignore-auto-dns no ipv4.ignore-auto-routes no connection.interface-name foo connection.autoconnect yes ipv4.never-default no ipv4.ignore-auto-dns no ipv6.ignore-auto-routes no ipv4.may-fail yes

ifname is specified via connection.interface-name, while older nmcli insists using ifname. On the other hand, the same command works fine with NetworkManager 1.22.10 on Ubuntu 20.04.

As a side note, ifname is required before NetworkManager 1.22 [1]. I don't have a machine with NetworkManager between 1.4 and 1.22 for testing, though.

[1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/commit/02e5a8d10a39f0f401b72f3a0a39619770fe51de

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, I guess let's try this out :)

* Make space stripping more flexible - works for cases without equal
  signs.
* Keep vpn.data in a test case with no spaces
@yan12125
Copy link
Contributor Author

Thank you very much for detailed review and kind suggestions! I fixed some, while others need more time for investigation.

@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-5 labels Aug 22, 2022
@jremerich
Copy link
Contributor

Hi!

I took a look at the changes and didn't see anything I can contribute.

Thanks for your contribution, @yan12125 !

@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Sep 1, 2022
@felixfontein felixfontein merged commit 6ff594b into ansible-collections:main Sep 3, 2022
@patchback
Copy link

patchback bot commented Sep 3, 2022

Backport to stable-5: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-5/6ff594b524a9180a66a5d98b465f2de1d75086a8/pr-5126

Backported as #5220

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label Sep 3, 2022
patchback bot pushed a commit that referenced this pull request Sep 3, 2022
* nmcli: avoid changed status for most cases with VPN connections

Follow-up #4746

* `nmcli connection show` includes vpn.service-type but not vpn-type.
  Switching to vpn.service-type removes unneeded diffs while keeping
  the same functionality, as vpn-type is an alias of vpn.service-type
  per nm-settings-nmcli(1).

  NetworkManager also adds `org.freedesktop.NetworkManager.` prefix for
  known VPN types [1]. The logic is non-trivial so I didn't implement it
  in this commit. If a user specifies `service-type: l2tp`, changed will
  be always be True:

    -    "vpn.service-type": "org.freedesktop.NetworkManager.l2tp"
    +    "vpn.service-type": "l2tp"

* The vpn.data field from `nmcli connection show` is sorted by keys and
  there are spaces around equal signs. I added codes for parsing such
  data.

Tests are also updated to match outputs of nmcli commands.

[1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/blob/1.38.4/src/libnm-core-impl/nm-vpn-plugin-info.c#L619

* Add changelog

* Some suggested changes

* Make space stripping more flexible - works for cases without equal
  signs.
* Keep vpn.data in a test case with no spaces

* nmcli: allow any string for vpn service-type

(cherry picked from commit 6ff594b)
@felixfontein
Copy link
Collaborator

@yan12125 thanks for improving this module!
@jremerich thanks for reviewing!

felixfontein pushed a commit that referenced this pull request Sep 3, 2022
… (#5220)

* nmcli: avoid changed status for most cases with VPN connections

Follow-up #4746

* `nmcli connection show` includes vpn.service-type but not vpn-type.
  Switching to vpn.service-type removes unneeded diffs while keeping
  the same functionality, as vpn-type is an alias of vpn.service-type
  per nm-settings-nmcli(1).

  NetworkManager also adds `org.freedesktop.NetworkManager.` prefix for
  known VPN types [1]. The logic is non-trivial so I didn't implement it
  in this commit. If a user specifies `service-type: l2tp`, changed will
  be always be True:

    -    "vpn.service-type": "org.freedesktop.NetworkManager.l2tp"
    +    "vpn.service-type": "l2tp"

* The vpn.data field from `nmcli connection show` is sorted by keys and
  there are spaces around equal signs. I added codes for parsing such
  data.

Tests are also updated to match outputs of nmcli commands.

[1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/blob/1.38.4/src/libnm-core-impl/nm-vpn-plugin-info.c#L619

* Add changelog

* Some suggested changes

* Make space stripping more flexible - works for cases without equal
  signs.
* Keep vpn.data in a test case with no spaces

* nmcli: allow any string for vpn service-type

(cherry picked from commit 6ff594b)

Co-authored-by: Chih-Hsuan Yen <yan12125@gmail.com>
@yan12125 yan12125 deleted the nmcli-remove-diffs branch September 3, 2022 10:43
bratwurzt pushed a commit to bratwurzt/community.general that referenced this pull request Nov 7, 2022
…ble-collections#5126)

* nmcli: avoid changed status for most cases with VPN connections

Follow-up ansible-collections#4746

* `nmcli connection show` includes vpn.service-type but not vpn-type.
  Switching to vpn.service-type removes unneeded diffs while keeping
  the same functionality, as vpn-type is an alias of vpn.service-type
  per nm-settings-nmcli(1).

  NetworkManager also adds `org.freedesktop.NetworkManager.` prefix for
  known VPN types [1]. The logic is non-trivial so I didn't implement it
  in this commit. If a user specifies `service-type: l2tp`, changed will
  be always be True:

    -    "vpn.service-type": "org.freedesktop.NetworkManager.l2tp"
    +    "vpn.service-type": "l2tp"

* The vpn.data field from `nmcli connection show` is sorted by keys and
  there are spaces around equal signs. I added codes for parsing such
  data.

Tests are also updated to match outputs of nmcli commands.

[1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/blob/1.38.4/src/libnm-core-impl/nm-vpn-plugin-info.c#L619

* Add changelog

* Some suggested changes

* Make space stripping more flexible - works for cases without equal
  signs.
* Keep vpn.data in a test case with no spaces

* nmcli: allow any string for vpn service-type
bratwurzt pushed a commit to bratwurzt/community.general that referenced this pull request Nov 7, 2022
…ble-collections#5126)

* nmcli: avoid changed status for most cases with VPN connections

Follow-up ansible-collections#4746

* `nmcli connection show` includes vpn.service-type but not vpn-type.
  Switching to vpn.service-type removes unneeded diffs while keeping
  the same functionality, as vpn-type is an alias of vpn.service-type
  per nm-settings-nmcli(1).

  NetworkManager also adds `org.freedesktop.NetworkManager.` prefix for
  known VPN types [1]. The logic is non-trivial so I didn't implement it
  in this commit. If a user specifies `service-type: l2tp`, changed will
  be always be True:

    -    "vpn.service-type": "org.freedesktop.NetworkManager.l2tp"
    +    "vpn.service-type": "l2tp"

* The vpn.data field from `nmcli connection show` is sorted by keys and
  there are spaces around equal signs. I added codes for parsing such
  data.

Tests are also updated to match outputs of nmcli commands.

[1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/blob/1.38.4/src/libnm-core-impl/nm-vpn-plugin-info.c#L619

* Add changelog

* Some suggested changes

* Make space stripping more flexible - works for cases without equal
  signs.
* Keep vpn.data in a test case with no spaces

* nmcli: allow any string for vpn service-type
This was referenced Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug module module net_tools plugins plugin (any type) stale_ci CI is older than 7 days, rerun before merging tests tests unit tests/unit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants