diff --git a/changelogs/fragments/detect-generic-container.yml b/changelogs/fragments/detect-generic-container.yml new file mode 100644 index 00000000000..49d9b072c5b --- /dev/null +++ b/changelogs/fragments/detect-generic-container.yml @@ -0,0 +1,4 @@ +--- +bugfixes: +- virtual facts - detect generic container environment based on non-empty + "container" env var diff --git a/lib/ansible/module_utils/facts/utils.py b/lib/ansible/module_utils/facts/utils.py index 5d78eab47c9..ca67098fe68 100644 --- a/lib/ansible/module_utils/facts/utils.py +++ b/lib/ansible/module_utils/facts/utils.py @@ -36,11 +36,17 @@ def get_file_content(path, default=None, strip=True): return data -def get_file_lines(path, strip=True): +def get_file_lines(path, strip=True, line_sep=None): '''get list of lines from file''' data = get_file_content(path, strip=strip) if data: - ret = data.splitlines() + if line_sep is None: + ret = data.splitlines() + else: + if len(line_sep) == 1: + ret = data.rstrip(line_sep).split(line_sep) + else: + ret = data.split(line_sep) else: ret = [] return ret diff --git a/lib/ansible/module_utils/facts/virtual/linux.py b/lib/ansible/module_utils/facts/virtual/linux.py index 8add4a1f6cd..fdbcbc33948 100644 --- a/lib/ansible/module_utils/facts/virtual/linux.py +++ b/lib/ansible/module_utils/facts/virtual/linux.py @@ -49,7 +49,7 @@ class LinuxVirtual(Virtual): # 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'): + for line in get_file_lines('/proc/1/environ', line_sep='\x00'): if re.search('container=lxc', line): virtual_facts['virtualization_type'] = 'lxc' virtual_facts['virtualization_role'] = 'guest' @@ -58,6 +58,10 @@ class LinuxVirtual(Virtual): virtual_facts['virtualization_type'] = 'podman' virtual_facts['virtualization_role'] = 'guest' return virtual_facts + if re.search('^container=.', line): + virtual_facts['virtualization_type'] = 'container' + virtual_facts['virtualization_role'] = 'guest' + return virtual_facts if os.path.exists('/proc/vz') and not os.path.exists('/proc/lve'): virtual_facts['virtualization_type'] = 'openvz'