| 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 |
import socket |
| 20 |
import platform |
| 21 |
import typing as t |
| 22 |
|
| 23 |
from ansible.module_utils.facts.utils import get_file_content |
| 24 |
|
| 25 |
from ansible.module_utils.facts.collector import BaseFactCollector |
| 26 |
|
| 27 |
# i86pc is a Solaris and derivatives-ism |
| 28 |
SOLARIS_I86_RE_PATTERN = r'i([3456]86|86pc)' |
| 29 |
solaris_i86_re = re.compile(SOLARIS_I86_RE_PATTERN) |
| 30 |
|
| 31 |
|
| 32 |
class PlatformFactCollector(BaseFactCollector): |
| 33 |
name = 'platform' |
| 34 |
_fact_ids = set(['system', |
| 35 |
'kernel', |
| 36 |
'kernel_version', |
| 37 |
'machine', |
| 38 |
'python_version', |
| 39 |
'architecture', |
| 40 |
'machine_id']) # type: t.Set[str] |
| 41 |
|
| 42 |
def collect(self, module=None, collected_facts=None): |
| 43 |
platform_facts = {} |
| 44 |
# platform.system() can be Linux, Darwin, Java, or Windows |
| 45 |
platform_facts['system'] = platform.system() |
| 46 |
platform_facts['kernel'] = platform.release() |
| 47 |
platform_facts['kernel_version'] = platform.version() |
| 48 |
platform_facts['machine'] = platform.machine() |
| 49 |
|
| 50 |
platform_facts['python_version'] = platform.python_version() |
| 51 |
|
| 52 |
platform_facts['fqdn'] = socket.getfqdn() |
| 53 |
platform_facts['hostname'] = platform.node().split('.')[0] |
| 54 |
platform_facts['nodename'] = platform.node() |
| 55 |
|
| 56 |
platform_facts['domain'] = '.'.join(platform_facts['fqdn'].split('.')[1:]) |
| 57 |
|
| 58 |
arch_bits = platform.architecture()[0] |
| 59 |
|
| 60 |
platform_facts['userspace_bits'] = arch_bits.replace('bit', '') |
| 61 |
if platform_facts['machine'] == 'x86_64': |
| 62 |
platform_facts['architecture'] = platform_facts['machine'] |
| 63 |
if platform_facts['userspace_bits'] == '64': |
| 64 |
platform_facts['userspace_architecture'] = 'x86_64' |
| 65 |
elif platform_facts['userspace_bits'] == '32': |
| 66 |
platform_facts['userspace_architecture'] = 'i386' |
| 67 |
elif solaris_i86_re.search(platform_facts['machine']): |
| 68 |
platform_facts['architecture'] = 'i386' |
| 69 |
if platform_facts['userspace_bits'] == '64': |
| 70 |
platform_facts['userspace_architecture'] = 'x86_64' |
| 71 |
elif platform_facts['userspace_bits'] == '32': |
| 72 |
platform_facts['userspace_architecture'] = 'i386' |
| 73 |
else: |
| 74 |
platform_facts['architecture'] = platform_facts['machine'] |
| 75 |
|
| 76 |
if platform_facts['system'] == 'AIX': |
| 77 |
# Attempt to use getconf to figure out architecture |
| 78 |
# fall back to bootinfo if needed |
| 79 |
getconf_bin = module.get_bin_path('getconf') |
| 80 |
if getconf_bin: |
| 81 |
rc, out, err = module.run_command([getconf_bin, 'MACHINE_ARCHITECTURE']) |
| 82 |
data = out.splitlines() |
| 83 |
platform_facts['architecture'] = data[0] |
| 84 |
else: |
| 85 |
bootinfo_bin = module.get_bin_path('bootinfo') |
| 86 |
rc, out, err = module.run_command([bootinfo_bin, '-p']) |
| 87 |
data = out.splitlines() |
| 88 |
platform_facts['architecture'] = data[0] |
| 89 |
elif platform_facts['system'] == 'OpenBSD': |
| 90 |
platform_facts['architecture'] = platform.uname()[5] |
| 91 |
|
| 92 |
machine_id = get_file_content("/var/lib/dbus/machine-id") or get_file_content("/etc/machine-id") |
| 93 |
if machine_id: |
| 94 |
machine_id = machine_id.splitlines()[0] |
| 95 |
platform_facts["machine_id"] = machine_id |
| 96 |
|
| 97 |
return platform_facts |