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

fix: Allow network to restart when wireless or team connection is specified #675

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
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@
- `network_connections` - The connection profiles are configured as
`network_connections`, which is a list of dictionaries that include specific
options.
- `network_allow_restart` - Certain configurations require the role to restart
network services. For example, if a wireless connection is configured and
NetworkManager-wifi is not installed, NetworkManager must be restarted prior
to the connection being configured. Setting this to `false` will prevent the
role from restarting network service.
- `network_allow_restart` - It defaults to `false`. To load NetworkManager plugins
after installation, NetworkManager requires to be restarted. For example, if a
wireless connection is configured and NetworkManager-wifi is not installed,
NetworkManager must be restarted prior to the connection being configured. The
restart can result in connectivity loss and therefore the role does not allow it
without explicit consent. The user can consent to it by setting
`network_allow_restart` to `true`. Setting `network_allow_restart` to `false` will
prevent the role from restarting NetworkManager.
- `network_state` - The network state settings can be configured in the managed
host, and the format and the syntax of the configuration should be consistent
with the [nmstate state examples](https://nmstate.io/examples.html) (YAML).
Expand Down Expand Up @@ -259,7 +262,7 @@
- `macvlan`
- `infiniband`
- `wireless`
- `dummy`

Check warning on line 265 in README.md

View workflow job for this annotation

GitHub Actions / Detect non-inclusive language

`dummy` may be insensitive, use `placeholder`, `sample` instead

#### `type: ethernet`

Expand Down Expand Up @@ -355,9 +358,9 @@

- `password`: password for the network (required if `wpa-psk` or `sae` is used)

#### `type: dummy`

Check warning on line 361 in README.md

View workflow job for this annotation

GitHub Actions / Detect non-inclusive language

`dummy` may be insensitive, use `placeholder`, `sample` instead

Dummy network interface, `nm` (NetworkManager) is the only supported `network_provider`

Check warning on line 363 in README.md

View workflow job for this annotation

GitHub Actions / Detect non-inclusive language

`Dummy` may be insensitive, use `placeholder`, `sample` instead
for this type.

### `autoconnect`
Expand Down
50 changes: 49 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,53 @@
when:
- network_state is defined
- ansible_distribution_major_version | int < 8

- name: Check if updates for network packages are available through the DNF
package manager due to wireless or team interfaces
dnf:
update_cache: true
name: "{{ network_packages }}"
state: latest # noqa package-latest
register: dnf_package_update_info
check_mode: true
when:
- ansible_distribution == 'Fedora' or
ansible_distribution_major_version | int > 7
- __network_wireless_connections_defined
or __network_team_connections_defined
- not __network_is_ostree

- name: Check if updates for network packages are available through the YUM
package manager due to wireless or team interfaces
yum:
update_cache: true
name: "{{ network_packages }}" # noqa package-latest
state: latest
register: yum_package_update_info
check_mode: true
when:
- ansible_distribution_major_version | int < 8
- __network_wireless_connections_defined
or __network_team_connections_defined
- not __network_is_ostree

- name: Ask user's consent to restart NetworkManager due to wireless or team
interfaces
fail:
msg: NetworkManager needs to be restarted to be able to proceed
because wireless and team interfaces are defined. This might
disturb the connectivity of the managed system. Please set
`network_allow_restart` to `true` if you are prepared for this.
Notice that the necessary action is to install NetworkManager-wifi or
NetworkManager-team plugin and to restart NetworkManager.
register: __network_service_restart_requested
when:
- __network_wireless_connections_defined
or __network_team_connections_defined
- network_provider == "nm"
- not network_allow_restart
- dnf_package_update_info is changed or yum_package_update_info is changed

# Depending on the plugins, checking installed packages might be slow
# for example subscription manager might slow this down
# Therefore install packages only when rpm does not find them
Expand Down Expand Up @@ -70,7 +117,8 @@
ansible_distribution_major_version | int > 8

# If network packages changed and wireless or team connections are specified,
# NetworkManager must be restarted
# NetworkManager must be restarted, and the user needs to explicitly consent
# to restart NetworkManager by setting `network_allow_restart` to `true`
- name: Restart NetworkManager due to wireless or team interfaces
service:
name: NetworkManager
Expand Down
1 change: 1 addition & 0 deletions tests/ensure_provider_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"playbooks/tests_wireless.yml": {
EXTRA_RUN_CONDITION: "ansible_distribution_major_version == '7'",
},
"playbooks/tests_wireless_and_network_restart.yml": {},
"playbooks/tests_wireless_plugin_installation.yml": {},
"playbooks/tests_wireless_wpa3_owe.yml": {
"comment": "# OWE has not been supported by NetworkManager 1.18.8 on \
Expand Down
1 change: 1 addition & 0 deletions tests/playbooks/tests_team_plugin_installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
include_role:
name: linux-system-roles.network
vars:
network_allow_restart: true
network_connections:
# Specify the team profile
- name: team0
Expand Down
80 changes: 80 additions & 0 deletions tests/playbooks/tests_wireless_and_network_restart.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for testing wifi and network restart
hosts: all
vars:
interface: wlan0
profile: "{{ interface }}"
package: NetworkManager-wifi
wifi_restart_network: false
network_connections_result_error:
NetworkManager needs to be restarted to be able to proceed
because wireless and team interfaces are defined. This might
disturb the connectivity of the managed system. Please set
`network_allow_restart` to `true` if you are prepared for this.
Notice that the necessary action is to install NetworkManager-wifi or
NetworkManager-team plugin and to restart NetworkManager.
lsr_fail_debug:
- __network_connections_result
tasks:
- name: Show playbook name
debug:
msg: "this is: playbooks/tests_wireless_and_network_restart.yml"
tags:
- always

- name: Cannot test ostree systems because the package can not be removed
or installed - 'NetworkManager-wifi'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we are using the jinja2 syntax for the '{{ package }}', the {{ package }}' should be appeared at the end of the task name.

Copy link
Member

Choose a reason for hiding this comment

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

FWIW, cannot is more common than can not.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right, thanks for pointing it out, I will address it in another PR.

meta: end_host
when: __network_is_ostree | d(false)

- name: Test the wifi connection without NetworkManager restarted
tags:
- tests::wifi:create
block:
- name: Include the task 'run_test.yml'
include_tasks: tasks/run_test.yml
vars:
lsr_description:
Given a system with '{{ package }}' package removed,
when creating a wifi connection without NetworkManager restarted,
then the role should fail with the error specified in
`network_connections_result_error`.
lsr_setup:
- tasks/delete_interface.yml
- tasks/assert_device_absent.yml
- tasks/remove_package.yml
lsr_test:
- tasks/create_wireless_profile_restart_network.yml
lsr_assert:
- tasks/assert_network_connections_failed.yml
lsr_cleanup:
- tasks/check_network_dns.yml

- name: "Reset testing variables"
set_fact:
wifi_restart_network: true

- name: Test the wifi connection with NetworkManager restarted
tags:
- tests::wifi:restart
block:
- name: Include the task 'run_test.yml'
include_tasks: tasks/run_test.yml
vars:
lsr_description:
Given a system with '{{ package }}' package removed,
when creating a wifi connection with NetworkManager restarted,
then the wifi device and profile are present and
the '{{ package }}' package is installed in the system.
lsr_setup:
- tasks/delete_interface.yml
- tasks/assert_device_absent.yml
- tasks/remove_package.yml
lsr_test:
- tasks/create_wireless_profile_restart_network.yml
lsr_assert:
- tasks/assert_package_installed.yml
lsr_cleanup:
- tasks/cleanup_profile+device.yml
- tasks/check_network_dns.yml
1 change: 1 addition & 0 deletions tests/playbooks/tests_wireless_plugin_installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
include_role:
name: linux-system-roles.network
vars:
network_allow_restart: true
network_connections:
- name: wlan0
type: wireless
Expand Down
10 changes: 10 additions & 0 deletions tests/tasks/assert_network_connections_failed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: BSD-3-Clause
---
- name: Assert that configuring network connections failed
assert:
that:
- __network_service_restart_requested is failed
- __network_service_restart_requested is search(
network_connections_result_error)
msg: Configuring network connections is not failed with the error
"{{ network_connections_result_error }}"
11 changes: 11 additions & 0 deletions tests/tasks/assert_package_installed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: BSD-3-Clause
---
- name: "Get the rpm package facts"
package_facts:
manager: "auto"

- name: "Assert installed package '{{ package }}'"
assert:
that:
- package in ansible_facts.packages
msg: "'{{ package }}' is not installed"
1 change: 1 addition & 0 deletions tests/tasks/create_team_profile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
include_role:
name: linux-system-roles.network
vars:
network_allow_restart: true
network_connections:
- name: "{{ interface }}"
persistent_state: present
Expand Down
21 changes: 21 additions & 0 deletions tests/tasks/create_wireless_profile_restart_network.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-License-Identifier: BSD-3-Clause
---
- name: Create wireless connection with rescue
block:
- name: Import network role
import_role:
name: linux-system-roles.network
vars:
network_allow_restart: '{{ wifi_restart_network }}'
network_connections:
- name: "{{ interface }}"
type: wireless
wireless:
ssid: "My WPA2-PSK Network"
key_mgmt: "wpa-psk"
password: "p@55w0rD"

rescue:
- name: Show rescue result
debug:
var: __network_service_restart_requested
6 changes: 6 additions & 0 deletions tests/tasks/remove_package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: BSD-3-Clause
---
- name: "Remove the package '{{ package }}'"
package:
name: "{{ package }}"
state: absent
24 changes: 24 additions & 0 deletions tests/tests_wireless_and_network_restart_nm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: BSD-3-Clause
# This file was generated by ensure_provider_tests.py
---
# set network provider and gather facts
# yamllint disable rule:line-length
- name: Run playbook 'playbooks/tests_wireless_and_network_restart.yml' with nm as provider
hosts: all
tasks:
- name: Include the task 'el_repo_setup.yml'
include_tasks: tasks/el_repo_setup.yml
- name: Set network provider to 'nm'
set_fact:
network_provider: nm
tags:
- always


# The test requires or should run with NetworkManager, therefore it cannot run
# on RHEL/CentOS 6
- name: Import the playbook 'playbooks/tests_wireless_and_network_restart.yml'
import_playbook: playbooks/tests_wireless_and_network_restart.yml
when:
- ansible_distribution_major_version != '6'
- ansible_distribution != 'Fedora'
Loading