Make modprobe module check for builtins as well (#37150)

Without this modprobe always reports changed when modprobe-ing a builtin module.

With this, if a kernel module is a builtin, the modprobe module will:
- succeed (without incorrectly reporting changed) if ``state`` is ``present``;
- fail if ``state`` is ``absent``

The failure will have whatever error message modprobe returns when
attempting to remove a builtin module. For example:
``modprobe: ERROR: Module nfs is builtin.``
pull/50217/head
Jacob Floyd 6 years ago committed by Alicia Cozine
parent 76450fd1c2
commit 069e0b8d57

@ -0,0 +1,3 @@
---
bugfixes:
- "modprobe - The modprobe module now detects builtin kernel modules. If a kernel module is builtin the modprobe module will now: succeed (without incorrectly reporting changed) if ``state`` is ``present``; and fail if ``state`` is ``absent`` (with an error message like ``modprobe: ERROR: Module nfs is builtin.``). (https://github.com/ansible/ansible/pull/37150)"

@ -131,6 +131,13 @@ Noteworthy module changes
* The ``na_ontap_cluster_peer`` module has replaced ``source_intercluster_lif`` and ``dest_intercluster_lif`` string options with * The ``na_ontap_cluster_peer`` module has replaced ``source_intercluster_lif`` and ``dest_intercluster_lif`` string options with
``source_intercluster_lifs`` and ``dest_intercluster_lifs`` list options ``source_intercluster_lifs`` and ``dest_intercluster_lifs`` list options
* The ``modprobe`` module now detects kernel builtins. Previously, attempting to remove (with ``state: absent``)
a builtin kernel module succeeded without any error message because ``modprobe`` did not detect the module as
``present``. Now, ``modprobe`` will fail if a kernel module is builtin and ``state: absent`` (with an error message
from the modprobe binary like ``modprobe: ERROR: Module nfs is builtin.``), and it will succeed without reporting
changed if ``state: present``. Any playbooks that are using ``changed_when: no`` to mask this quirk can safely
remove that workaround. To get the previous behavior when applying ``state: absent`` to a builtin kernel module,
use ``failed_when: false`` or ``ignore_errors: true`` in your playbook.
Plugins Plugins
======= =======

@ -83,14 +83,22 @@ def main():
# Check if module is present # Check if module is present
try: try:
modules = open('/proc/modules')
present = False present = False
with open('/proc/modules') as modules:
module_name = name.replace('-', '_') + ' ' module_name = name.replace('-', '_') + ' '
for line in modules: for line in modules:
if line.startswith(module_name): if line.startswith(module_name):
present = True present = True
break break
modules.close() if not present:
command = [module.get_bin_path('uname', True), '-r']
rc, uname_kernel_release, err = module.run_command(command)
module_file = '/' + name + '.ko'
with open('/lib/modules/' + uname_kernel_release + '/modules.builtin') as builtins:
for line in builtins:
if line.endswith(module_file):
present = True
break
except IOError as e: except IOError as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **result) module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **result)

Loading…
Cancel
Save