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
16from __future__ import (absolute_import, division, print_function)
17__metaclass__ = type
18
19import re
20
21
22def get_sysctl(module, prefixes):
23    sysctl_cmd = module.get_bin_path('sysctl')
24    cmd = [sysctl_cmd]
25    cmd.extend(prefixes)
26
27    rc, out, err = module.run_command(cmd)
28    if rc != 0:
29        return dict()
30
31    sysctl = dict()
32    for line in out.splitlines():
33        if not line:
34            continue
35        (key, value) = re.split(r'\s?=\s?|: ', line, maxsplit=1)
36        sysctl[key] = value.strip()
37
38    return sysctl
39