Debian Codegraph @

lib/ansible/module_utils/facts/sysctl.py

raw
1 # This file is part of Ansible
2 #
3 # Ansible is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # Ansible is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
15
16 from __future__ import annotations
17
18 import re
19
20
21 def get_sysctl(module, prefixes):
22
23 sysctl = dict()
24 sysctl_cmd = module.get_bin_path('sysctl')
25 if sysctl_cmd is not None:
26
27 cmd = [sysctl_cmd]
28 cmd.extend(prefixes)
29
30 try:
31 rc, out, err = module.run_command(cmd)
32 except OSError as ex:
33 module.error_as_warning('Unable to read sysctl.', exception=ex)
34 rc = 1
35
36 if rc == 0:
37 key = ''
38 value = ''
39 for line in out.splitlines():
40 if not line.strip():
41 continue
42
43 if line.startswith(' '):
44 # handle multiline values, they will not have a starting key
45 # Add the newline back in so people can split on it to parse
46 # lines if they need to.
47 value += '\n' + line
48 continue
49
50 if key:
51 sysctl[key] = value.strip()
52
53 try:
54 (key, value) = re.split(r'\s?=\s?|: ', line, maxsplit=1)
55 except Exception as ex:
56 module.error_as_warning(f'Unable to split sysctl line {line!r}.', exception=ex)
57
58 if key:
59 sysctl[key] = value.strip()
60
61 return sysctl