Commit graph

74 commits

Author SHA1 Message Date
Wen Liang
cb3e23ac5d infiniband: Reject the interface name for the ipoib connection
If the `p_key` is specified, then ipoib (IP over Infiniband) connection
will be created. In this case, the interface name must be unset.

NM only allows the user to set the interface name to a fixed value
based on the interface name of the parent and the value of the `p_key`,
for example, if the interface name of the parent is `ib0` and the
`p_key` is `0x000a`, then the `interface_name` for the ipoib connection
must be `ib0.000a` or unset. But this kind of validation in NM is
pointless because it is not useful at all to have the interface name for
the ipoib connection, NM should has also rejected setting the interface
name for the ipoib connection initially.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2022-05-19 12:41:35 +02:00
Wen Liang
6da2df4ba0 Add support for routing rules
In order to enable the user using the policy routing (e.g. source
routing) or instructing the system which table to look up to determine
the correct route, add support for routing rules.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2022-04-21 12:19:26 +02:00
Wen Liang
512d840fc4 Util: Normalize address family value before getting prefix length
Some address family values (e.g. "ipv4" or "ipv6") should not be
considered as bogus value if they can be normalized into
`socket.AF_INET` or `socket.AF_INET6`, it is more tolerant but also
still preserving the capability for rejecting the utterly bogus address
family values.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2022-04-21 12:19:26 +02:00
Wen Liang
59ee08ae02 Reject configuring ipv6 disabled if not supported in NM
In NM, the property `NM_SETTING_IP6_CONFIG_METHOD_DISABLED` is only
supported since NM 1.20, therefore, the role should reject configuring
`ipv6 disabled` if `NM_SETTING_IP6_CONFIG_METHOD_DISABLED` is not
supported by the running version of NM.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2022-04-05 16:56:34 +02:00
Gris Ge
3a2fe6d13d InfiniBand bond: Fix typo of bond mode active-backup
The correct bond mode is `active-backup` instead of `active_backup`.

Thanks to vpal who found this issue via
https://github.com/linux-system-roles/network/issues/475

Signed-off-by: Gris Ge <fge@redhat.com>
2022-03-11 07:04:19 +01:00
Wen Liang
59be618857 Support more bond options
In order to enable user to flexibly control the network transmission
over the bonded interface, support all the bond options which are
currently supported by NetworkManager.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2022-02-11 23:40:34 +01:00
Gris Ge
c98c17a236 Fix problem when switch provider from initscript to nm
Problem:

After `tests_bridge_initscripts.yml` passed, the `tests_bridge_nm.yml`
will fail with NetworkManager 1.18.

Root cause:

 1. The `absent` and `down` action of initscript provider will not
    remove the bridge interface which fail the assertion in
    `tests_bridge_nm.yml`.
 2. In initscript mode, network role will create ifcfg file with
    `NM_CONTROLLED=no` instructing NetworkManager to mark the bridge as
    unmanaged. The follow up `down` and `absent` action of initscript
    provider will not change the NetworkManager's understanding on
    unmanaged state of this interface.

Fixes:
 1. We cannot change existing behaviour of initscript on not deleting
    interface in `down` and `absent` action. So we change the test
    function `tests/playbooks/down_profile.yml` to delete the interface
    manually via `ip link del <ifname>` command.

 2. Use `NM.Client.reload_connections_async()` to reload the
    configuration for nm provider on NetworkManager 1.18.

Previous test infrastructure is running each test file in a brand new VM
or container which cause this problem not been found before.

Dedicate test case `tests/tests_switch_provider.yml` included.

Signed-off-by: Gris Ge <fge@redhat.com>
2022-01-28 17:47:13 +01:00
Wen Liang
5eb03fa992 Support routing tables in static routes
The users want to use the policy routing (e.g. source routing), so
that they can forward the packet based on the other criteria except for
the destination address in the packet. In such scenario, the routing
tables have to be supported beforehand in static routes, so that the
users can define policy routing rules later to instruct the system
which table to use to determine the correct route.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2022-01-27 18:18:47 +01:00
Wen Liang
5db8d0c5d1 Reject DNS_search setting if no IP family is enabled
NetworkManager only allows to configure the DNS options for each IP
family, when they are enabled. Therefore, reject DNS settings in
ArgValidator if no IP family is enabled.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-12-14 13:29:08 +01:00
Wen Liang
05b38b4217 Reject IPv6 DNS setting if IPv6 family is not enabled
NetworkManager only allows to configure the IPv6 nameservers if the
IPv6 family is enabled. Therefore, reject the IPv6 nameservers setting
in ArgValidator if the IPv6 family is not enabled.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-11-29 10:33:58 -05:00
Rich Megginson
16ac152542 support ansible-core 2.12; ansible-plugin-scan; fix pylint errors; py39
Use the new tox-lsr that uses ansible-core 2.12.
Use ansible-plugin-scan in CI.
Fix pylint errors found by new ansible-test pylinter.
CI test with py39

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
2021-11-08 20:01:01 +01:00
Wen Liang
9fd19afa25 Allow configuring network connection via matching path
Users can easily configure or update network connection via matching
physical device path of the interface, which add certain flexibilty of
user experience.

Update connection profile via matching `path` setting:

```yaml
  - name: eth0
    type: ethernet
    autoconnect: yes
    # For PCI devices, the path has the form "pci-$domain:$bus:$device.$function"
    # It will only update the interface with the path "pci-0000:00:03.0"
    match:
      path:
        - pci-0000:00:03.0
```

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-10-06 18:22:42 +08:00
Wen Liang
b569704c72 arg_validator: accept None as valid input for ArgValidatorDict
Users might want to use jinja2 templates to set properties. As such,
it's convenient to accept None as an alias for an empty dictionary.

For exmaple, setting like `"match": None` will be allowed by the role:
e.g.
        network_connections:
          - name: enp0s8
            type: ethernet
            persistent_state: present
            state: up
            match:
            ip:
              route_metric4: 10

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-10-06 18:22:42 +08:00
Wen Liang
af07c2a58d arg_validator: accept None as valid input for ArgValidatorList
Users might want to use jinja2 templates to set properties. As such,
it's convenient to accept None as an alias for an empty list.

For exmaple, setting like `"match": {"path": None}` will be allowed by
the role:

        network_connections:
          - name: enp0s8
            type: ethernet
            persistent_state: present
            state: up
            match:
              path:
            ip:
              route_metric4: 10

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-10-06 18:22:42 +08:00
Thomas Haller
7a1683848c arg_validator: make ArgValidator.DEFAULT the default for dictionary's default value
The most used default value for ArgValidatorDict is
ArgValidator.DEFAULT, which means to generate the default value
based on the nested validators.

ArgValidator.DEFAULT is also a better default for the default_value.
Otherwise, it's a bit odd to have (and honor) ArgValidator.DEFAULT, if
that is not in fact the default default_value.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-08-26 16:30:09 +08:00
Thomas Haller
63ddf64a41 arg_validator: use DEFAULT argument to generate default value for dictionaries
The previous solution was ugly, as it hacked the private
self._default_value of the parent.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-08-26 16:30:09 +08:00
Thomas Haller
e01e550c59 arg_validator: support generating default value for ArgValidatorDict
Some dictionaries may wish to have a default value that is a dictionary
with the default of all keys.

Add a method to generate this.

This returns a callable instead of a plain dictionary. In practice, the
generated default value should always be the same. However, we also
expect that ArgValidator validation gives a result that is owned by the
caller. In case of mutable values (like a dictionary), we want a deep
clone.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-08-26 16:30:09 +08:00
Thomas Haller
e0a1b6af68 arg_validator: make ArgValidatorDeprecated a subclass of ArgValidator
ArgValidatorDeprecated is used as a nested validator, it also should
subtype ArgValidator base class. Technially, in python you can do duck
typing and get away with a different class that merely behaves
sufficiently similar. But why?

That way, we also can set default_value=ArgValidator.MISSING.
As a result, we no longer need to special case ArgValidatorDeprecated
at various places, because the default value will indicate that
it should not be present.

Co-authored-by: Till Maas <opensource@till.name>

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-08-26 16:30:09 +08:00
Thomas Haller
a262c77c26 arg_validator: expect ArgValidatorDeprecated.deprecated_by setting to be valid
"validator.deprecated_by" is user configuration of the validator. It depends on the
setup of the current ArgValidatorDict instance. It would be a bug if the user
references a non-existing validator.

Thus, use dictionary lookup instead of get() and fail early.

The previous code of course also didn't work, because the
returned None value would have led to an exception shortly after.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-08-26 16:30:09 +08:00
Thomas Haller
72319a1294 arg_validator: call get_default_value() for getting the default value
self._default_value might be a callable, to get the actual default
value, self.get_default_value() must be called.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-08-26 16:30:09 +08:00
Thomas Haller
8a00e7fab2 arg_validator: rename "default_value" field to "_default_value"
The name "default_value" is also used as parameter name, which
means that searching the source for the term gives unrelated
results. Rename to give a unique name.

Also, the field is really private and should usually not be called
directly. Instead get_default_value() should be called.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-08-26 16:30:09 +08:00
Thomas Haller
4afa81cdd0 arg_validator: rename ArgValidator.DEFAULT_SENTINEL to DEFAULT
To be consistent with ArgValidator.MISSING, which serves a similar
purpose.

Also, replace the if-else python ternary, which I find hard to read.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-08-26 16:30:09 +08:00
Thomas Haller
03833e8ac2 arg_validator: drop wrong default value for ArgValidatorIPRoute
The parameter is not used (neither by callers not the implementation).
It also makes not sense.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-08-26 16:30:09 +08:00
Thomas Haller
6e53bcf65f arg_validator: drop wrong default value for ArgValidatorIPAddr
The parameter is not used (neither by callers not the implementation).
It also makes not sense.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-08-26 16:30:09 +08:00
Wen Liang
a2af42d0f2 wifi: Add Simultaneous Authentication of Equals(SAE) support
Enable WPA3 SAE support via:

    ```yaml
    network_connections:
      - name: wlan0
        type: wireless
        wireless:
          ssid: "WIFI_SSID"
          key_mgmt: "sae"
	  password: "p@55w0rD"
    ```

Integration test case was included for Fedora and CentOS. ( Failed in setting up the
mock wifi on RHEL, so skipped the integration test on RHEL)

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-08-09 09:59:22 +08:00
Jack Adolph
b368bce8aa Add 'auto_gateway' option
If enabled, a default route will be configured using the default gateway. If disabled,
the default route will be removed.

If this variable is not specified, the role will use the default behavior of the
`network_provider` selected.

Setting this option to `no` is equivalent to:
- `DEFROUTE = no` in initscripts, or
- `ipv4.never-default/ipv6.never-default yes` in nmcli

Signed-off-by: Jack Adolph <jack.adolph@gmail.com>
2021-06-04 07:07:13 +08:00
Wen Liang
a6c98bd660 arg_validator: reject bool arguments from ArgValidatorNum
`ArgValidatorNum` would normalize boolean into int when
`self.numeric_type` is int, then `self.numeric_type(False)` is 0 and
`self.numeric_type(True)` is 1. Therefore, we need to fix
`ArgValidatorNum()` to reject boolean values when integer values are
expected for the setting. This bug fix potentially breaks previously
"working" playbooks (but realistically, they were not working, because
setting 0 or 1 was unlikely intended).

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-05-24 18:51:54 +08:00
Wen Liang
dfacbf72f7 arg_validator: normalize numeric value for ArgValidatorNum
In python, bool is a subclass of int. Thus,
isinstance(value, self.numeric_type) would be True, with value being a
bool and numeric_type an int.

ArgValidatorNum should normalize the input values to be of type
self.numeric_type, except the default_value, which might be None (or
anything really).

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-05-24 18:51:54 +08:00
Wen Liang
93e509b533 Fix: support dns_search and dns_options for all address family
`dns_search` and `dns_options` should not be specific to the address
family. Previously, `dns_search` and `dns_options` were only supported
for IPv4 nameservers, so we also need to support `dns_search` and
`dns_options` for IPv6 nameservers.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-05-24 15:34:52 +08:00
Wen Liang
2444e27cce wifi: Add Opportunistic Wireless Encryption (OWE) support
Enable WPA3 OWE support via:

    ```yaml
    network_connections:
      - name: wlan0
        type: wireless
        wireless:
          ssid: "WIFI_SSID"
          key_mgmt: "owe"
    ```

Integration test case was included for Fedora and CentOS. ( Failed in setting up the
mock wifi on RHEL, so skipped the integration test on RHEL)

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-05-24 10:51:01 +08:00
Wen Liang
ae2d60a0b2 Add support for ETHTOOL Ring option
ETHTOOL Ring option is not supported by NetworkManager until
NM 1.25.2. Currently, ETHTOOL Ring option is not suppored by
Network role, so enable the support for ETHTOOL Ring option.

Configure ethtool ring option via:

	```yaml
            network_connections:
              - name: testnic1
                type: ethernet
                state: up
                ip:
                  dhcp4: no
                  auto6: no
                ethtool:
                  ring:
                    rx: 128
                    rx_jumbo: 128
                    rx_mini: 128
                    tx: 128
	```

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-05-11 18:19:16 +02:00
Rich Megginson
570246bad3 no need for __init__.py in module_utils/
The file module_utils/__init__.py is not needed.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
2021-05-06 11:15:11 +02:00
Rich Megginson
c1b0002bc9 fix python black issues
There was recently an upgrade to python black which made function
documentation checking stricter.  This caused network tox CI to
break.  The fix is to format the function doc text in the manner
expected by black.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
2021-05-03 18:41:05 +02:00
Thomas Haller
c2e83b1d82 argval: use callable() check in get_default_value()
Previously, we would catch any kind of exception. We only want
to suppress an error if the default value is not a callable
(but instead a plain value).

Usually, in python they say it's easier to ask for forgiveness
than permission. However, that is often not true (the code here
this case becomes simpler, not more complicated). But more important,
in this case we only get a TypeError, which doesn't exactly tell
us what is wrong. We want to only suppress the error, if the
instance is not callable.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-04-16 12:09:43 +02:00
Thomas Haller
bf9933e482 argval: cleanup default values in ArgValidatorDict._validate_impl()
- split the "if" block in two. It's easier to read to only
  have one condition and to think about them independently.

- check "self.all_missing_during_validate" before getting the
  default value.

Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-04-16 12:09:43 +02:00
Rich Megginson
f5ff30a66c fix most ansible-test issues, suppress the rest
Automation Hub, and possibly Galaxy in the future, require the
collection to be screened with `ansible-test sanity` among other
checks.  The role had a number of issues:
* Use `AssertionError` instead of `assert`
* Use of `logging` module not in accordance with standards, but these
  are ok and the errors were suppressed
* Several import errors which are ok because they are checked
  elsewhere
* __init__.py in the module_utils directories must be empty, so a
  new file myerror.py was added to move the code from __init__.py
  * NOTE: network_lsr/nm/__init__.py is not empty
* The documentation block in the module was not properly constructed
  or formatted.
* shellcheck issues, including removing unused files
* use `unused` instead of `_` (underscore) for variables that are
  unused

add WARNING to module docs - collection users should not use directly

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
2021-04-09 19:57:55 +02:00
Wen Liang
8318686620 Support ipv6.method disabled in network role
Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-02-19 18:12:52 +01:00
Fernando Fernandez Mancera
3167b3c125 inclusive language: use "port" instead of "slave"
In order to promote a diverse and inclusive environment we are replacing
all the reference to "slave" for "port".

Test case added.

Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
2021-02-03 15:15:04 +01:00
Fernando Fernandez Mancera
a283e47c12 inclusive language: use "controller" instead of "master"
In order to promote a diverse and inclusive environment we are replacing
all the reference to "master" for "controller".

Test case added.

Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
2021-02-03 12:57:03 +01:00
Wen Liang
880b7ab0cc Support dns-options in network role
Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2021-01-28 09:57:45 +08:00
Thomas Haller
675c7e8073 argument_validator: add documentation comments
Signed-off-by: Thomas Haller <thaller@redhat.com>
2021-01-24 20:51:48 +08:00
Gris Ge
19139286ed nm provider: Refactor the volatilize action of network connection
Refactor the volatilize action of nm provider:
 * Move code to `module_utils/network_lsr/nm`
 * The `module_utils/network_lsr/nm` only volatilize profile by given UUID
   instead of guess. The `library/network_connections.py` is responsible
   on choosing UUID.

Signed-off-by: Gris Ge <fge@redhat.com>
2021-01-23 11:19:32 +08:00
Noriko Hosoi
023ecbd027 Collections - workaround for the module_utils path finding issue in ansible 2.9
The details of the issue is described in this ansible issue:
   https://github.com/ansible/ansible/issues/68361

The workaround contains 2 changes.
1) Advice from @sivel:
   replacing, e.g.,
     from ansible.module_utils.network_lsr.nm.provider import NetworkManagerProvider
   with
     from ansible.module_utils.network_lsr.nm import provider
   and then use provider.NetworkManagerProvider
2) In the later module_utils path finding path, gi.require_version("NM", "1.0")
   in module_utils/network_lsr/nm/client.py fails with "ValueError: Namespace
   NM not available" on the control node. By ignoring the exception, the failure
   is worked around. Please note that the missing package issue never occurs
   on the managed nodes since in case of "nm", the NetworkManager package is
   installed in the network role.

Signed-off-by: Noriko Hosoi <nhosoi@redhat.com>
2021-01-14 17:04:10 -05:00
Antonio Cardace
b586d11e79 Add support for ethtool coalesce settings
Also add related tests.

Resolves: #112
Signed-off-by: Antonio Cardace <acardace@redhat.com>
2021-01-09 11:02:07 +08:00
Wen Liang
f848949c76 RFE: Support dummy interfaces
Signed-off-by: Wen Liang <liangwen12year@gmail.com>
2020-12-16 14:28:41 +08:00
Gris Ge
c4643e56bb nm provider: Refactor the down action of network connection
When deactivating a profile in libNM, we should:

 * Check `NM.ActionConnection` existence
 * Check `NM.ActionConnection.props.state` not DEACTIVATED
 * Use signal `state-changed` of `NM.ActionConnection`.
 * Only invoke `NM.Client.deactivate_connection_async()` if not
   in DEACTIVATING state.
 * Ignore `NM.ManagerError.CONNECTIONNOTACTIVE` error.

This patch also introduced a new class `NetworkManagerProvider`
in `module_utils/network_lsr/nm`:

 * Independent from Ansible but need to use absolute import due to
   limitation of ansible 2.8.
 * Provide sync function wrapping async calls of libNM.
 * Use stable logging method of python.
 * Only load this module when provider is nm.

This patch also changed how logging is handling in
`Cmd_nm.run_action_down()` as initial step on isolate ansible log
mechanism from provider module.

By moving provider codes to `module_utils` folder, we can eventually
simplify the bloated `library/network_connections.py`.

Signed-off-by: Gris Ge <fge@redhat.com>
2020-11-19 18:58:26 +08:00
Noriko Hosoi
890af84071 RHELPLAN-56586 - sync collections related changes from template to network role
The task 56586 is for adding tests against the collection converted
format to the tox/travis CI to capture a problem before merging the
pr, if any. It'd be helpful to find out bugs in the conversion tool
lsr_role2collection.py, as well. The source of this commit is located
in linux-system-roles/template. They are synced by auto-maintenance/
sync-template.sh, then manually adjusted to the network role.

.travis/runcollection.sh is the test script. tox.ini is modified to run
it in the tox/travis CI. The script downloads lsr_role2collection.py,
then converts the network role into the conversion format in the working
directory .tox. In the collection, it runs a set of tests black, flake8,
yamllint, py38 to check the converted result.
2020-10-23 13:35:14 +08:00
Till Maas
9a6bdb5403 Update formatting according to new black version
Signed-off-by: Till Maas <opensource@till.name>
2020-08-28 09:11:56 +02:00
Elvira García Ruiz
c29a2bf708 Add Pytest integration tests
tests/integration/test_ethernet.py is a script capable of using the network
module without executing it from Ansible and using Pytest. The example test
sets an ip in a test interface. The changes in tox.ini adapt the testing to the
new script. The __init__.py files were added to allow python2.7 compatibility.

Signed-off-by: Elvira García Ruiz <elviragr@riseup.net>
2020-08-24 21:52:25 +02:00
Till Maas
fb99ae6777 Simplify persistent state handling
Since the order of the actions list does not matter anymore because the
role iterates over all actions in a defined order, simplify the
handling for persistent states.
2020-07-06 13:13:56 +02:00