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.
20 short_description: Look for the system kernel on the filesystem
22 - We need to find the kernel on non-booted systems, disk images, chroots, etc.
23 To do this we check /lib/modules and look for the kernel that matches the running
24 kernle, or failing that we look for the highest-numbered kernel
26 kernel: starting kernel to check
27 module_dir: Override kernel module dir, default /lib/modules
30 LIB_MODULES = "/lib/modules"
33 def try_int(s, *args):
34 """Convert to integer if possible."""
37 except (TypeError, ValueError):
38 return args[0] if args else s
41 def convert_ints(fields, orig):
42 return tuple((try_int(f) for f in fields)), orig
46 module = AnsibleModule(
48 'kernel': {'required': True, 'type': 'str'},
49 'module_dir': {'required': False, 'type': 'str', 'default': LIB_MODULES},
52 params = module.params
53 kernel = params['kernel']
54 module_dir = params['module_dir']
56 if os.path.isdir(os.path.join(module_dir, kernel)):
57 module.exit_json(changed=False, kernel=kernel)
59 kernel_dirs = os.listdir(module_dir)
60 kernels = sorted((convert_ints(re.split('[-.]', k), k) for k in kernel_dirs), reverse=True)
62 newest_kernel = kernels[0][-1]
64 module.fail_json(msg="Unable to find kernels in {}".format(module_dir))
66 if os.path.isdir(os.path.join(module_dir, newest_kernel)):
67 module.exit_json(changed=False, kernel=newest_kernel)
71 module.fail_json(msg="Unable to kernel other than {}".format(kernel))
74 # <<INCLUDE_ANSIBLE_MODULE_COMMON>>
75 from ansible.module_utils.basic import * # noqa
77 if __name__ == '__main__':
82 get kernel from uname, ansible_kernel
83 look for that kernel in /lib/modules
84 if that kernel doens't exist