Debian Codegraph @

lib/ansible/module_utils/facts/system/cmdline.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 shlex
19 import typing as t
20
21 from ansible.module_utils.facts.utils import get_file_content
22
23 from ansible.module_utils.facts.collector import BaseFactCollector
24
25
26 class CmdLineFactCollector(BaseFactCollector):
27 name = 'cmdline'
28 _fact_ids = set() # type: t.Set[str]
29
30 def _get_proc_cmdline(self):
31 return get_file_content('/proc/cmdline')
32
33 def _parse_proc_cmdline(self, data):
34 cmdline_dict = {}
35 try:
36 for piece in shlex.split(data, posix=False):
37 item = piece.split('=', 1)
38 if len(item) == 1:
39 cmdline_dict[item[0]] = True
40 else:
41 cmdline_dict[item[0]] = item[1]
42 except ValueError:
43 pass
44
45 return cmdline_dict
46
47 def _parse_proc_cmdline_facts(self, data):
48 cmdline_dict = {}
49 try:
50 for piece in shlex.split(data, posix=False):
51 item = piece.split('=', 1)
52 if len(item) == 1:
53 cmdline_dict[item[0]] = True
54 else:
55 if item[0] in cmdline_dict:
56 if isinstance(cmdline_dict[item[0]], list):
57 cmdline_dict[item[0]].append(item[1])
58 else:
59 new_list = [cmdline_dict[item[0]], item[1]]
60 cmdline_dict[item[0]] = new_list
61 else:
62 cmdline_dict[item[0]] = item[1]
63 except ValueError:
64 pass
65
66 return cmdline_dict
67
68 def collect(self, module=None, collected_facts=None):
69 cmdline_facts = {}
70
71 data = self._get_proc_cmdline()
72
73 if not data:
74 return cmdline_facts
75
76 cmdline_facts['cmdline'] = self._parse_proc_cmdline(data)
77 cmdline_facts['proc_cmdline'] = self._parse_proc_cmdline_facts(data)
78
79 return cmdline_facts