summaryrefslogtreecommitdiffstats
path: root/lib/ansible/module_utils/facts/virtual
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ansible/module_utils/facts/virtual')
-rw-r--r--lib/ansible/module_utils/facts/virtual/__init__.py0
-rw-r--r--lib/ansible/module_utils/facts/virtual/base.py80
-rw-r--r--lib/ansible/module_utils/facts/virtual/dragonfly.py25
-rw-r--r--lib/ansible/module_utils/facts/virtual/freebsd.py79
-rw-r--r--lib/ansible/module_utils/facts/virtual/hpux.py72
-rw-r--r--lib/ansible/module_utils/facts/virtual/linux.py405
-rw-r--r--lib/ansible/module_utils/facts/virtual/netbsd.py73
-rw-r--r--lib/ansible/module_utils/facts/virtual/openbsd.py74
-rw-r--r--lib/ansible/module_utils/facts/virtual/sunos.py139
-rw-r--r--lib/ansible/module_utils/facts/virtual/sysctl.py112
10 files changed, 1059 insertions, 0 deletions
diff --git a/lib/ansible/module_utils/facts/virtual/__init__.py b/lib/ansible/module_utils/facts/virtual/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/ansible/module_utils/facts/virtual/__init__.py
diff --git a/lib/ansible/module_utils/facts/virtual/base.py b/lib/ansible/module_utils/facts/virtual/base.py
new file mode 100644
index 0000000..67b59a5
--- /dev/null
+++ b/lib/ansible/module_utils/facts/virtual/base.py
@@ -0,0 +1,80 @@
+# base classes for virtualization facts
+# -*- coding: utf-8 -*-
+#
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import ansible.module_utils.compat.typing as t
+
+from ansible.module_utils.facts.collector import BaseFactCollector
+
+
+class Virtual:
+ """
+ This is a generic Virtual subclass of Facts. This should be further
+ subclassed to implement per platform. If you subclass this,
+ you should define:
+ - virtualization_type
+ - virtualization_role
+ - container (e.g. solaris zones, freebsd jails, linux containers)
+
+ All subclasses MUST define platform.
+ """
+ platform = 'Generic'
+
+ # FIXME: remove load_on_init if we can
+ def __init__(self, module, load_on_init=False):
+ self.module = module
+
+ # FIXME: just here for existing tests cases till they are updated
+ def populate(self, collected_facts=None):
+ virtual_facts = self.get_virtual_facts()
+
+ return virtual_facts
+
+ def get_virtual_facts(self):
+ virtual_facts = {
+ 'virtualization_type': '',
+ 'virtualization_role': '',
+ 'virtualization_tech_guest': set(),
+ 'virtualization_tech_host': set(),
+ }
+ return virtual_facts
+
+
+class VirtualCollector(BaseFactCollector):
+ name = 'virtual'
+ _fact_class = Virtual
+ _fact_ids = set([
+ 'virtualization_type',
+ 'virtualization_role',
+ 'virtualization_tech_guest',
+ 'virtualization_tech_host',
+ ]) # type: t.Set[str]
+
+ def collect(self, module=None, collected_facts=None):
+ collected_facts = collected_facts or {}
+ if not module:
+ return {}
+
+ # Network munges cached_facts by side effect, so give it a copy
+ facts_obj = self._fact_class(module)
+
+ facts_dict = facts_obj.populate(collected_facts=collected_facts)
+
+ return facts_dict
diff --git a/lib/ansible/module_utils/facts/virtual/dragonfly.py b/lib/ansible/module_utils/facts/virtual/dragonfly.py
new file mode 100644
index 0000000..b176f8b
--- /dev/null
+++ b/lib/ansible/module_utils/facts/virtual/dragonfly.py
@@ -0,0 +1,25 @@
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+from ansible.module_utils.facts.virtual.freebsd import FreeBSDVirtual, VirtualCollector
+
+
+class DragonFlyVirtualCollector(VirtualCollector):
+ # Note the _fact_class impl is actually the FreeBSDVirtual impl
+ _fact_class = FreeBSDVirtual
+ _platform = 'DragonFly'
diff --git a/lib/ansible/module_utils/facts/virtual/freebsd.py b/lib/ansible/module_utils/facts/virtual/freebsd.py
new file mode 100644
index 0000000..7062d01
--- /dev/null
+++ b/lib/ansible/module_utils/facts/virtual/freebsd.py
@@ -0,0 +1,79 @@
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import os
+
+from ansible.module_utils.facts.virtual.base import Virtual, VirtualCollector
+from ansible.module_utils.facts.virtual.sysctl import VirtualSysctlDetectionMixin
+
+
+class FreeBSDVirtual(Virtual, VirtualSysctlDetectionMixin):
+ """
+ This is a FreeBSD-specific subclass of Virtual. It defines
+ - virtualization_type
+ - virtualization_role
+ """
+ platform = 'FreeBSD'
+
+ def get_virtual_facts(self):
+ virtual_facts = {}
+ host_tech = set()
+ guest_tech = set()
+
+ # Set empty values as default
+ virtual_facts['virtualization_type'] = ''
+ virtual_facts['virtualization_role'] = ''
+
+ if os.path.exists('/dev/xen/xenstore'):
+ guest_tech.add('xen')
+ virtual_facts['virtualization_type'] = 'xen'
+ virtual_facts['virtualization_role'] = 'guest'
+
+ kern_vm_guest = self.detect_virt_product('kern.vm_guest')
+ guest_tech.update(kern_vm_guest['virtualization_tech_guest'])
+ host_tech.update(kern_vm_guest['virtualization_tech_host'])
+
+ hw_hv_vendor = self.detect_virt_product('hw.hv_vendor')
+ guest_tech.update(hw_hv_vendor['virtualization_tech_guest'])
+ host_tech.update(hw_hv_vendor['virtualization_tech_host'])
+
+ sec_jail_jailed = self.detect_virt_product('security.jail.jailed')
+ guest_tech.update(sec_jail_jailed['virtualization_tech_guest'])
+ host_tech.update(sec_jail_jailed['virtualization_tech_host'])
+
+ if virtual_facts['virtualization_type'] == '':
+ sysctl = kern_vm_guest or hw_hv_vendor or sec_jail_jailed
+ # We call update here, then re-set virtualization_tech_host/guest
+ # later.
+ virtual_facts.update(sysctl)
+
+ virtual_vendor_facts = self.detect_virt_vendor('hw.model')
+ guest_tech.update(virtual_vendor_facts['virtualization_tech_guest'])
+ host_tech.update(virtual_vendor_facts['virtualization_tech_host'])
+
+ if virtual_facts['virtualization_type'] == '':
+ virtual_facts.update(virtual_vendor_facts)
+
+ virtual_facts['virtualization_tech_guest'] = guest_tech
+ virtual_facts['virtualization_tech_host'] = host_tech
+ return virtual_facts
+
+
+class FreeBSDVirtualCollector(VirtualCollector):
+ _fact_class = FreeBSDVirtual
+ _platform = 'FreeBSD'
diff --git a/lib/ansible/module_utils/facts/virtual/hpux.py b/lib/ansible/module_utils/facts/virtual/hpux.py
new file mode 100644
index 0000000..1057482
--- /dev/null
+++ b/lib/ansible/module_utils/facts/virtual/hpux.py
@@ -0,0 +1,72 @@
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import os
+import re
+
+from ansible.module_utils.facts.virtual.base import Virtual, VirtualCollector
+
+
+class HPUXVirtual(Virtual):
+ """
+ This is a HP-UX specific subclass of Virtual. It defines
+ - virtualization_type
+ - virtualization_role
+ """
+ platform = 'HP-UX'
+
+ def get_virtual_facts(self):
+ virtual_facts = {}
+ host_tech = set()
+ guest_tech = set()
+
+ if os.path.exists('/usr/sbin/vecheck'):
+ rc, out, err = self.module.run_command("/usr/sbin/vecheck")
+ if rc == 0:
+ guest_tech.add('HP vPar')
+ virtual_facts['virtualization_type'] = 'guest'
+ virtual_facts['virtualization_role'] = 'HP vPar'
+ if os.path.exists('/opt/hpvm/bin/hpvminfo'):
+ rc, out, err = self.module.run_command("/opt/hpvm/bin/hpvminfo")
+ if rc == 0 and re.match('.*Running.*HPVM vPar.*', out):
+ guest_tech.add('HPVM vPar')
+ virtual_facts['virtualization_type'] = 'guest'
+ virtual_facts['virtualization_role'] = 'HPVM vPar'
+ elif rc == 0 and re.match('.*Running.*HPVM guest.*', out):
+ guest_tech.add('HPVM IVM')
+ virtual_facts['virtualization_type'] = 'guest'
+ virtual_facts['virtualization_role'] = 'HPVM IVM'
+ elif rc == 0 and re.match('.*Running.*HPVM host.*', out):
+ guest_tech.add('HPVM')
+ virtual_facts['virtualization_type'] = 'host'
+ virtual_facts['virtualization_role'] = 'HPVM'
+ if os.path.exists('/usr/sbin/parstatus'):
+ rc, out, err = self.module.run_command("/usr/sbin/parstatus")
+ if rc == 0:
+ guest_tech.add('HP nPar')
+ virtual_facts['virtualization_type'] = 'guest'
+ virtual_facts['virtualization_role'] = 'HP nPar'
+
+ virtual_facts['virtualization_tech_guest'] = guest_tech
+ virtual_facts['virtualization_tech_host'] = host_tech
+ return virtual_facts
+
+
+class HPUXVirtualCollector(VirtualCollector):
+ _fact_class = HPUXVirtual
+ _platform = 'HP-UX'
diff --git a/lib/ansible/module_utils/facts/virtual/linux.py b/lib/ansible/module_utils/facts/virtual/linux.py
new file mode 100644
index 0000000..31fa061
--- /dev/null
+++ b/lib/ansible/module_utils/facts/virtual/linux.py
@@ -0,0 +1,405 @@
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import glob
+import os
+import re
+
+from ansible.module_utils.facts.virtual.base import Virtual, VirtualCollector
+from ansible.module_utils.facts.utils import get_file_content, get_file_lines
+
+
+class LinuxVirtual(Virtual):
+ """
+ This is a Linux-specific subclass of Virtual. It defines
+ - virtualization_type
+ - virtualization_role
+ """
+ platform = 'Linux'
+
+ # For more information, check: http://people.redhat.com/~rjones/virt-what/
+ def get_virtual_facts(self):
+ virtual_facts = {}
+
+ # We want to maintain compatibility with the old "virtualization_type"
+ # and "virtualization_role" entries, so we need to track if we found
+ # them. We won't return them until the end, but if we found them early,
+ # we should avoid updating them again.
+ found_virt = False
+
+ # But as we go along, we also want to track virt tech the new way.
+ host_tech = set()
+ guest_tech = set()
+
+ # lxc/docker
+ if os.path.exists('/proc/1/cgroup'):
+ for line in get_file_lines('/proc/1/cgroup'):
+ if re.search(r'/docker(/|-[0-9a-f]+\.scope)', line):
+ guest_tech.add('docker')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'docker'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if re.search('/lxc/', line) or re.search('/machine.slice/machine-lxc', line):
+ guest_tech.add('lxc')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'lxc'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if re.search('/system.slice/containerd.service', line):
+ guest_tech.add('containerd')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'containerd'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ # lxc does not always appear in cgroups anymore but sets 'container=lxc' environment var, requires root privs
+ if os.path.exists('/proc/1/environ'):
+ for line in get_file_lines('/proc/1/environ', line_sep='\x00'):
+ if re.search('container=lxc', line):
+ guest_tech.add('lxc')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'lxc'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if re.search('container=podman', line):
+ guest_tech.add('podman')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'podman'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if re.search('^container=.', line):
+ guest_tech.add('container')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'container'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ if os.path.exists('/proc/vz') and not os.path.exists('/proc/lve'):
+ virtual_facts['virtualization_type'] = 'openvz'
+ if os.path.exists('/proc/bc'):
+ host_tech.add('openvz')
+ if not found_virt:
+ virtual_facts['virtualization_role'] = 'host'
+ else:
+ guest_tech.add('openvz')
+ if not found_virt:
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ systemd_container = get_file_content('/run/systemd/container')
+ if systemd_container:
+ guest_tech.add(systemd_container)
+ if not found_virt:
+ virtual_facts['virtualization_type'] = systemd_container
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ # If docker/containerd has a custom cgroup parent, checking /proc/1/cgroup (above) might fail.
+ # https://docs.docker.com/engine/reference/commandline/dockerd/#default-cgroup-parent
+ # Fallback to more rudimentary checks.
+ if os.path.exists('/.dockerenv') or os.path.exists('/.dockerinit'):
+ guest_tech.add('docker')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'docker'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ # ensure 'container' guest_tech is appropriately set
+ if guest_tech.intersection(set(['docker', 'lxc', 'podman', 'openvz', 'containerd'])) or systemd_container:
+ guest_tech.add('container')
+
+ if os.path.exists("/proc/xen"):
+ is_xen_host = False
+ try:
+ for line in get_file_lines('/proc/xen/capabilities'):
+ if "control_d" in line:
+ is_xen_host = True
+ except IOError:
+ pass
+
+ if is_xen_host:
+ host_tech.add('xen')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'xen'
+ virtual_facts['virtualization_role'] = 'host'
+ else:
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'xen'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ # assume guest for this block
+ if not found_virt:
+ virtual_facts['virtualization_role'] = 'guest'
+
+ product_name = get_file_content('/sys/devices/virtual/dmi/id/product_name')
+ sys_vendor = get_file_content('/sys/devices/virtual/dmi/id/sys_vendor')
+ product_family = get_file_content('/sys/devices/virtual/dmi/id/product_family')
+
+ if product_name in ('KVM', 'KVM Server', 'Bochs', 'AHV'):
+ guest_tech.add('kvm')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'kvm'
+ found_virt = True
+
+ if sys_vendor == 'oVirt':
+ guest_tech.add('oVirt')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'oVirt'
+ found_virt = True
+
+ if sys_vendor == 'Red Hat':
+ if product_family == 'RHV':
+ guest_tech.add('RHV')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'RHV'
+ found_virt = True
+ elif product_name == 'RHEV Hypervisor':
+ guest_tech.add('RHEV')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'RHEV'
+ found_virt = True
+
+ if product_name in ('VMware Virtual Platform', 'VMware7,1'):
+ guest_tech.add('VMware')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'VMware'
+ found_virt = True
+
+ if product_name in ('OpenStack Compute', 'OpenStack Nova'):
+ guest_tech.add('openstack')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'openstack'
+ found_virt = True
+
+ bios_vendor = get_file_content('/sys/devices/virtual/dmi/id/bios_vendor')
+
+ if bios_vendor == 'Xen':
+ guest_tech.add('xen')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'xen'
+ found_virt = True
+
+ if bios_vendor == 'innotek GmbH':
+ guest_tech.add('virtualbox')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'virtualbox'
+ found_virt = True
+
+ if bios_vendor in ('Amazon EC2', 'DigitalOcean', 'Hetzner'):
+ guest_tech.add('kvm')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'kvm'
+ found_virt = True
+
+ KVM_SYS_VENDORS = ('QEMU', 'Amazon EC2', 'DigitalOcean', 'Google', 'Scaleway', 'Nutanix')
+ if sys_vendor in KVM_SYS_VENDORS:
+ guest_tech.add('kvm')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'kvm'
+ found_virt = True
+
+ if sys_vendor == 'KubeVirt':
+ guest_tech.add('KubeVirt')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'KubeVirt'
+ found_virt = True
+
+ # FIXME: This does also match hyperv
+ if sys_vendor == 'Microsoft Corporation':
+ guest_tech.add('VirtualPC')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'VirtualPC'
+ found_virt = True
+
+ if sys_vendor == 'Parallels Software International Inc.':
+ guest_tech.add('parallels')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'parallels'
+ found_virt = True
+
+ if sys_vendor == 'OpenStack Foundation':
+ guest_tech.add('openstack')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'openstack'
+ found_virt = True
+
+ # unassume guest
+ if not found_virt:
+ del virtual_facts['virtualization_role']
+
+ if os.path.exists('/proc/self/status'):
+ for line in get_file_lines('/proc/self/status'):
+ if re.match(r'^VxID:\s+\d+', line):
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'linux_vserver'
+ if re.match(r'^VxID:\s+0', line):
+ host_tech.add('linux_vserver')
+ if not found_virt:
+ virtual_facts['virtualization_role'] = 'host'
+ else:
+ guest_tech.add('linux_vserver')
+ if not found_virt:
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ if os.path.exists('/proc/cpuinfo'):
+ for line in get_file_lines('/proc/cpuinfo'):
+ if re.match('^model name.*QEMU Virtual CPU', line):
+ guest_tech.add('kvm')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'kvm'
+ elif re.match('^vendor_id.*User Mode Linux', line):
+ guest_tech.add('uml')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'uml'
+ elif re.match('^model name.*UML', line):
+ guest_tech.add('uml')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'uml'
+ elif re.match('^machine.*CHRP IBM pSeries .emulated by qemu.', line):
+ guest_tech.add('kvm')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'kvm'
+ elif re.match('^vendor_id.*PowerVM Lx86', line):
+ guest_tech.add('powervm_lx86')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'powervm_lx86'
+ elif re.match('^vendor_id.*IBM/S390', line):
+ guest_tech.add('PR/SM')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'PR/SM'
+ lscpu = self.module.get_bin_path('lscpu')
+ if lscpu:
+ rc, out, err = self.module.run_command(["lscpu"])
+ if rc == 0:
+ for line in out.splitlines():
+ data = line.split(":", 1)
+ key = data[0].strip()
+ if key == 'Hypervisor':
+ tech = data[1].strip()
+ guest_tech.add(tech)
+ if not found_virt:
+ virtual_facts['virtualization_type'] = tech
+ else:
+ guest_tech.add('ibm_systemz')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'ibm_systemz'
+ else:
+ continue
+ if virtual_facts['virtualization_type'] == 'PR/SM':
+ if not found_virt:
+ virtual_facts['virtualization_role'] = 'LPAR'
+ else:
+ if not found_virt:
+ virtual_facts['virtualization_role'] = 'guest'
+ if not found_virt:
+ found_virt = True
+
+ # Beware that we can have both kvm and virtualbox running on a single system
+ if os.path.exists("/proc/modules") and os.access('/proc/modules', os.R_OK):
+ modules = []
+ for line in get_file_lines("/proc/modules"):
+ data = line.split(" ", 1)
+ modules.append(data[0])
+
+ if 'kvm' in modules:
+ host_tech.add('kvm')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'kvm'
+ virtual_facts['virtualization_role'] = 'host'
+
+ if os.path.isdir('/rhev/'):
+ # Check whether this is a RHEV hypervisor (is vdsm running ?)
+ for f in glob.glob('/proc/[0-9]*/comm'):
+ try:
+ with open(f) as virt_fh:
+ comm_content = virt_fh.read().rstrip()
+
+ if comm_content in ('vdsm', 'vdsmd'):
+ # We add both kvm and RHEV to host_tech in this case.
+ # It's accurate. RHEV uses KVM.
+ host_tech.add('RHEV')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'RHEV'
+ break
+ except Exception:
+ pass
+
+ found_virt = True
+
+ if 'vboxdrv' in modules:
+ host_tech.add('virtualbox')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'virtualbox'
+ virtual_facts['virtualization_role'] = 'host'
+ found_virt = True
+
+ if 'virtio' in modules:
+ host_tech.add('kvm')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'kvm'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ # In older Linux Kernel versions, /sys filesystem is not available
+ # dmidecode is the safest option to parse virtualization related values
+ dmi_bin = self.module.get_bin_path('dmidecode')
+ # We still want to continue even if dmidecode is not available
+ if dmi_bin is not None:
+ (rc, out, err) = self.module.run_command('%s -s system-product-name' % dmi_bin)
+ if rc == 0:
+ # Strip out commented lines (specific dmidecode output)
+ vendor_name = ''.join([line.strip() for line in out.splitlines() if not line.startswith('#')])
+ if vendor_name.startswith('VMware'):
+ guest_tech.add('VMware')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'VMware'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ if 'BHYVE' in out:
+ guest_tech.add('bhyve')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'bhyve'
+ virtual_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ if os.path.exists('/dev/kvm'):
+ host_tech.add('kvm')
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'kvm'
+ virtual_facts['virtualization_role'] = 'host'
+ found_virt = True
+
+ # If none of the above matches, return 'NA' for virtualization_type
+ # and virtualization_role. This allows for proper grouping.
+ if not found_virt:
+ virtual_facts['virtualization_type'] = 'NA'
+ virtual_facts['virtualization_role'] = 'NA'
+ found_virt = True
+
+ virtual_facts['virtualization_tech_guest'] = guest_tech
+ virtual_facts['virtualization_tech_host'] = host_tech
+ return virtual_facts
+
+
+class LinuxVirtualCollector(VirtualCollector):
+ _fact_class = LinuxVirtual
+ _platform = 'Linux'
diff --git a/lib/ansible/module_utils/facts/virtual/netbsd.py b/lib/ansible/module_utils/facts/virtual/netbsd.py
new file mode 100644
index 0000000..b4ef14e
--- /dev/null
+++ b/lib/ansible/module_utils/facts/virtual/netbsd.py
@@ -0,0 +1,73 @@
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import os
+
+from ansible.module_utils.facts.virtual.base import Virtual, VirtualCollector
+from ansible.module_utils.facts.virtual.sysctl import VirtualSysctlDetectionMixin
+
+
+class NetBSDVirtual(Virtual, VirtualSysctlDetectionMixin):
+ platform = 'NetBSD'
+
+ def get_virtual_facts(self):
+ virtual_facts = {}
+ host_tech = set()
+ guest_tech = set()
+
+ # Set empty values as default
+ virtual_facts['virtualization_type'] = ''
+ virtual_facts['virtualization_role'] = ''
+
+ virtual_product_facts = self.detect_virt_product('machdep.dmi.system-product')
+ guest_tech.update(virtual_product_facts['virtualization_tech_guest'])
+ host_tech.update(virtual_product_facts['virtualization_tech_host'])
+ virtual_facts.update(virtual_product_facts)
+
+ virtual_vendor_facts = self.detect_virt_vendor('machdep.dmi.system-vendor')
+ guest_tech.update(virtual_vendor_facts['virtualization_tech_guest'])
+ host_tech.update(virtual_vendor_facts['virtualization_tech_host'])
+
+ if virtual_facts['virtualization_type'] == '':
+ virtual_facts.update(virtual_vendor_facts)
+
+ # The above logic is tried first for backwards compatibility. If
+ # something above matches, use it. Otherwise if the result is still
+ # empty, try machdep.hypervisor.
+ virtual_vendor_facts = self.detect_virt_vendor('machdep.hypervisor')
+ guest_tech.update(virtual_vendor_facts['virtualization_tech_guest'])
+ host_tech.update(virtual_vendor_facts['virtualization_tech_host'])
+
+ if virtual_facts['virtualization_type'] == '':
+ virtual_facts.update(virtual_vendor_facts)
+
+ if os.path.exists('/dev/xencons'):
+ guest_tech.add('xen')
+
+ if virtual_facts['virtualization_type'] == '':
+ virtual_facts['virtualization_type'] = 'xen'
+ virtual_facts['virtualization_role'] = 'guest'
+
+ virtual_facts['virtualization_tech_guest'] = guest_tech
+ virtual_facts['virtualization_tech_host'] = host_tech
+ return virtual_facts
+
+
+class NetBSDVirtualCollector(VirtualCollector):
+ _fact_class = NetBSDVirtual
+ _platform = 'NetBSD'
diff --git a/lib/ansible/module_utils/facts/virtual/openbsd.py b/lib/ansible/module_utils/facts/virtual/openbsd.py
new file mode 100644
index 0000000..c449028
--- /dev/null
+++ b/lib/ansible/module_utils/facts/virtual/openbsd.py
@@ -0,0 +1,74 @@
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import re
+
+from ansible.module_utils.facts.virtual.base import Virtual, VirtualCollector
+from ansible.module_utils.facts.virtual.sysctl import VirtualSysctlDetectionMixin
+
+from ansible.module_utils.facts.utils import get_file_content
+
+
+class OpenBSDVirtual(Virtual, VirtualSysctlDetectionMixin):
+ """
+ This is a OpenBSD-specific subclass of Virtual. It defines
+ - virtualization_type
+ - virtualization_role
+ """
+ platform = 'OpenBSD'
+ DMESG_BOOT = '/var/run/dmesg.boot'
+
+ def get_virtual_facts(self):
+ virtual_facts = {}
+ host_tech = set()
+ guest_tech = set()
+
+ # Set empty values as default
+ virtual_facts['virtualization_type'] = ''
+ virtual_facts['virtualization_role'] = ''
+
+ virtual_product_facts = self.detect_virt_product('hw.product')
+ guest_tech.update(virtual_product_facts['virtualization_tech_guest'])
+ host_tech.update(virtual_product_facts['virtualization_tech_host'])
+ virtual_facts.update(virtual_product_facts)
+
+ virtual_vendor_facts = self.detect_virt_vendor('hw.vendor')
+ guest_tech.update(virtual_vendor_facts['virtualization_tech_guest'])
+ host_tech.update(virtual_vendor_facts['virtualization_tech_host'])
+
+ if virtual_facts['virtualization_type'] == '':
+ virtual_facts.update(virtual_vendor_facts)
+
+ # Check the dmesg if vmm(4) attached, indicating the host is
+ # capable of virtualization.
+ dmesg_boot = get_file_content(OpenBSDVirtual.DMESG_BOOT)
+ for line in dmesg_boot.splitlines():
+ match = re.match('^vmm0 at mainbus0: (SVM/RVI|VMX/EPT)$', line)
+ if match:
+ host_tech.add('vmm')
+ virtual_facts['virtualization_type'] = 'vmm'
+ virtual_facts['virtualization_role'] = 'host'
+
+ virtual_facts['virtualization_tech_guest'] = guest_tech
+ virtual_facts['virtualization_tech_host'] = host_tech
+ return virtual_facts
+
+
+class OpenBSDVirtualCollector(VirtualCollector):
+ _fact_class = OpenBSDVirtual
+ _platform = 'OpenBSD'
diff --git a/lib/ansible/module_utils/facts/virtual/sunos.py b/lib/ansible/module_utils/facts/virtual/sunos.py
new file mode 100644
index 0000000..1e92677
--- /dev/null
+++ b/lib/ansible/module_utils/facts/virtual/sunos.py
@@ -0,0 +1,139 @@
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import os
+
+from ansible.module_utils.facts.virtual.base import Virtual, VirtualCollector
+
+
+class SunOSVirtual(Virtual):
+ """
+ This is a SunOS-specific subclass of Virtual. It defines
+ - virtualization_type
+ - virtualization_role
+ - container
+ """
+ platform = 'SunOS'
+
+ def get_virtual_facts(self):
+ virtual_facts = {}
+ host_tech = set()
+ guest_tech = set()
+
+ # Check if it's a zone
+ zonename = self.module.get_bin_path('zonename')
+ if zonename:
+ rc, out, err = self.module.run_command(zonename)
+ if rc == 0:
+ if out.rstrip() == "global":
+ host_tech.add('zone')
+ else:
+ guest_tech.add('zone')
+ virtual_facts['container'] = 'zone'
+
+ # Check if it's a branded zone (i.e. Solaris 8/9 zone)
+ if os.path.isdir('/.SUNWnative'):
+ guest_tech.add('zone')
+ virtual_facts['container'] = 'zone'
+
+ # If it's a zone check if we can detect if our global zone is itself virtualized.
+ # Relies on the "guest tools" (e.g. vmware tools) to be installed
+ if 'container' in virtual_facts and virtual_facts['container'] == 'zone':
+ modinfo = self.module.get_bin_path('modinfo')
+ if modinfo:
+ rc, out, err = self.module.run_command(modinfo)
+ if rc == 0:
+ for line in out.splitlines():
+ if 'VMware' in line:
+ guest_tech.add('vmware')
+ virtual_facts['virtualization_type'] = 'vmware'
+ virtual_facts['virtualization_role'] = 'guest'
+ if 'VirtualBox' in line:
+ guest_tech.add('virtualbox')
+ virtual_facts['virtualization_type'] = 'virtualbox'
+ virtual_facts['virtualization_role'] = 'guest'
+
+ if os.path.exists('/proc/vz'):
+ guest_tech.add('virtuozzo')
+ virtual_facts['virtualization_type'] = 'virtuozzo'
+ virtual_facts['virtualization_role'] = 'guest'
+
+ # Detect domaining on Sparc hardware
+ virtinfo = self.module.get_bin_path('virtinfo')
+ if virtinfo:
+ # The output of virtinfo is different whether we are on a machine with logical
+ # domains ('LDoms') on a T-series or domains ('Domains') on a M-series. Try LDoms first.
+ rc, out, err = self.module.run_command("/usr/sbin/virtinfo -p")
+ # The output contains multiple lines with different keys like this:
+ # DOMAINROLE|impl=LDoms|control=false|io=false|service=false|root=false
+ # The output may also be not formatted and the returncode is set to 0 regardless of the error condition:
+ # virtinfo can only be run from the global zone
+ if rc == 0:
+ try:
+ for line in out.splitlines():
+ fields = line.split('|')
+ if fields[0] == 'DOMAINROLE' and fields[1] == 'impl=LDoms':
+ guest_tech.add('ldom')
+ virtual_facts['virtualization_type'] = 'ldom'
+ virtual_facts['virtualization_role'] = 'guest'
+ hostfeatures = []
+ for field in fields[2:]:
+ arg = field.split('=')
+ if arg[1] == 'true':
+ hostfeatures.append(arg[0])
+ if len(hostfeatures) > 0:
+ virtual_facts['virtualization_role'] = 'host (' + ','.join(hostfeatures) + ')'
+ except ValueError:
+ pass
+
+ else:
+ smbios = self.module.get_bin_path('smbios')
+ if not smbios:
+ return
+ rc, out, err = self.module.run_command(smbios)
+ if rc == 0:
+ for line in out.splitlines():
+ if 'VMware' in line:
+ guest_tech.add('vmware')
+ virtual_facts['virtualization_type'] = 'vmware'
+ virtual_facts['virtualization_role'] = 'guest'
+ elif 'Parallels' in line:
+ guest_tech.add('parallels')
+ virtual_facts['virtualization_type'] = 'parallels'
+ virtual_facts['virtualization_role'] = 'guest'
+ elif 'VirtualBox' in line:
+ guest_tech.add('virtualbox')
+ virtual_facts['virtualization_type'] = 'virtualbox'
+ virtual_facts['virtualization_role'] = 'guest'
+ elif 'HVM domU' in line:
+ guest_tech.add('xen')
+ virtual_facts['virtualization_type'] = 'xen'
+ virtual_facts['virtualization_role'] = 'guest'
+ elif 'KVM' in line:
+ guest_tech.add('kvm')
+ virtual_facts['virtualization_type'] = 'kvm'
+ virtual_facts['virtualization_role'] = 'guest'
+
+ virtual_facts['virtualization_tech_guest'] = guest_tech
+ virtual_facts['virtualization_tech_host'] = host_tech
+ return virtual_facts
+
+
+class SunOSVirtualCollector(VirtualCollector):
+ _fact_class = SunOSVirtual
+ _platform = 'SunOS'
diff --git a/lib/ansible/module_utils/facts/virtual/sysctl.py b/lib/ansible/module_utils/facts/virtual/sysctl.py
new file mode 100644
index 0000000..1c7b2b3
--- /dev/null
+++ b/lib/ansible/module_utils/facts/virtual/sysctl.py
@@ -0,0 +1,112 @@
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import re
+
+
+class VirtualSysctlDetectionMixin(object):
+ def detect_sysctl(self):
+ self.sysctl_path = self.module.get_bin_path('sysctl')
+
+ def detect_virt_product(self, key):
+ virtual_product_facts = {}
+ host_tech = set()
+ guest_tech = set()
+
+ # We do similar to what we do in linux.py -- We want to allow multiple
+ # virt techs to show up, but maintain compatibility, so we have to track
+ # when we would have stopped, even though now we go through everything.
+ found_virt = False
+
+ self.detect_sysctl()
+ if self.sysctl_path:
+ rc, out, err = self.module.run_command("%s -n %s" % (self.sysctl_path, key))
+ if rc == 0:
+ if re.match('(KVM|kvm|Bochs|SmartDC).*', out):
+ guest_tech.add('kvm')
+ if not found_virt:
+ virtual_product_facts['virtualization_type'] = 'kvm'
+ virtual_product_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if re.match('.*VMware.*', out):
+ guest_tech.add('VMware')
+ if not found_virt:
+ virtual_product_facts['virtualization_type'] = 'VMware'
+ virtual_product_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if out.rstrip() == 'VirtualBox':
+ guest_tech.add('virtualbox')
+ if not found_virt:
+ virtual_product_facts['virtualization_type'] = 'virtualbox'
+ virtual_product_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if re.match('(HVM domU|XenPVH|XenPV|XenPVHVM).*', out):
+ guest_tech.add('xen')
+ if not found_virt:
+ virtual_product_facts['virtualization_type'] = 'xen'
+ virtual_product_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if out.rstrip() == 'Hyper-V':
+ guest_tech.add('Hyper-V')
+ if not found_virt:
+ virtual_product_facts['virtualization_type'] = 'Hyper-V'
+ virtual_product_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if out.rstrip() == 'Parallels':
+ guest_tech.add('parallels')
+ if not found_virt:
+ virtual_product_facts['virtualization_type'] = 'parallels'
+ virtual_product_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if out.rstrip() == 'RHEV Hypervisor':
+ guest_tech.add('RHEV')
+ if not found_virt:
+ virtual_product_facts['virtualization_type'] = 'RHEV'
+ virtual_product_facts['virtualization_role'] = 'guest'
+ found_virt = True
+ if (key == 'security.jail.jailed') and (out.rstrip() == '1'):
+ guest_tech.add('jails')
+ if not found_virt:
+ virtual_product_facts['virtualization_type'] = 'jails'
+ virtual_product_facts['virtualization_role'] = 'guest'
+ found_virt = True
+
+ virtual_product_facts['virtualization_tech_guest'] = guest_tech
+ virtual_product_facts['virtualization_tech_host'] = host_tech
+ return virtual_product_facts
+
+ def detect_virt_vendor(self, key):
+ virtual_vendor_facts = {}
+ host_tech = set()
+ guest_tech = set()
+ self.detect_sysctl()
+ if self.sysctl_path:
+ rc, out, err = self.module.run_command("%s -n %s" % (self.sysctl_path, key))
+ if rc == 0:
+ if out.rstrip() == 'QEMU':
+ guest_tech.add('kvm')
+ virtual_vendor_facts['virtualization_type'] = 'kvm'
+ virtual_vendor_facts['virtualization_role'] = 'guest'
+ if out.rstrip() == 'OpenBSD':
+ guest_tech.add('vmm')
+ virtual_vendor_facts['virtualization_type'] = 'vmm'
+ virtual_vendor_facts['virtualization_role'] = 'guest'
+
+ virtual_vendor_facts['virtualization_tech_guest'] = guest_tech
+ virtual_vendor_facts['virtualization_tech_host'] = host_tech
+ return virtual_vendor_facts