2 # Copyright (c) 2017 Intel Corporation
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
21 short_description: Look for the system kernel on the filesystem
23 - We need to find the kernel on non-booted systems, disk images, chroots, etc.
24 To do this we check /lib/modules and look for the kernel that matches the running
25 kernle, or failing that we look for the highest-numbered kernel
27 kernel: starting kernel to check
28 module_dir: Override kernel module dir, default /lib/modules
31 LIB_MODULES = "/lib/modules"
34 def try_int(s, *args):
35 """Convert to integer if possible."""
38 except (TypeError, ValueError):
39 return args[0] if args else s
42 def convert_ints(fields, orig):
43 return tuple((try_int(f) for f in fields)), orig
47 module = AnsibleModule(
49 'kernel': {'required': True, 'type': 'str'},
50 'module_dir': {'required': False, 'type': 'str', 'default': LIB_MODULES},
53 params = module.params
54 kernel = params['kernel']
55 module_dir = params['module_dir']
57 if os.path.isdir(os.path.join(module_dir, kernel)):
58 module.exit_json(changed=False, kernel=kernel)
60 kernel_dirs = os.listdir(module_dir)
61 kernels = sorted((convert_ints(re.split('[-.]', k), k) for k in kernel_dirs), reverse=True)
63 newest_kernel = kernels[0][-1]
65 module.fail_json(msg="Unable to find kernels in {}".format(module_dir))
67 if os.path.isdir(os.path.join(module_dir, newest_kernel)):
68 module.exit_json(changed=False, kernel=newest_kernel)
72 module.fail_json(msg="Unable to kernel other than {}".format(kernel))
75 # <<INCLUDE_ANSIBLE_MODULE_COMMON>>
76 from ansible.module_utils.basic import * # noqa
78 if __name__ == '__main__':
83 get kernel from uname, ansible_kernel
84 look for that kernel in /lib/modules
85 if that kernel doens't exist