From 17ec2d4952e0a641b07ce7774f744ee8f1a037ad Mon Sep 17 00:00:00 2001 From: Jan Fader Date: Wed, 3 Nov 2021 20:26:36 +0100 Subject: [PATCH] check /.dockerenv and /.dockerinit to guess a dockercontainer (#74349) * use os.path.exists for docker-check Co-authored-by: Jacob Floyd --- .../74349-improve-docker-detection.yml | 2 ++ .../module_utils/facts/virtual/linux.py | 10 ++++++++ .../module_utils/facts/virtual/test_linux.py | 24 +++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 changelogs/fragments/74349-improve-docker-detection.yml diff --git a/changelogs/fragments/74349-improve-docker-detection.yml b/changelogs/fragments/74349-improve-docker-detection.yml new file mode 100644 index 00000000000..5d386a3daad --- /dev/null +++ b/changelogs/fragments/74349-improve-docker-detection.yml @@ -0,0 +1,2 @@ +bugfixes: + - setup - detect docker container with check for ./dockerenv or ./dockinit (https://github.com/ansible/ansible/pull/74349). diff --git a/lib/ansible/module_utils/facts/virtual/linux.py b/lib/ansible/module_utils/facts/virtual/linux.py index 066cf5e13c9..31fa061749a 100644 --- a/lib/ansible/module_utils/facts/virtual/linux.py +++ b/lib/ansible/module_utils/facts/virtual/linux.py @@ -110,6 +110,16 @@ class LinuxVirtual(Virtual): 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') diff --git a/test/units/module_utils/facts/virtual/test_linux.py b/test/units/module_utils/facts/virtual/test_linux.py index 25503750ae6..7c13299ebf6 100644 --- a/test/units/module_utils/facts/virtual/test_linux.py +++ b/test/units/module_utils/facts/virtual/test_linux.py @@ -8,6 +8,30 @@ __metaclass__ = type from ansible.module_utils.facts.virtual import linux +def mock_os_path_is_file_docker(filename): + if filename in ('/.dockerenv', '/.dockerinit'): + return True + return False + + +def test_get_virtual_facts_docker(mocker): + mocker.patch('os.path.exists', mock_os_path_is_file_docker) + + module = mocker.Mock() + module.run_command.return_value = (0, '', '') + inst = linux.LinuxVirtual(module) + facts = inst.get_virtual_facts() + + expected = { + 'virtualization_role': 'guest', + 'virtualization_tech_host': set(), + 'virtualization_type': 'docker', + 'virtualization_tech_guest': set(['docker', 'container']), + } + + assert facts == expected + + def test_get_virtual_facts_bhyve(mocker): mocker.patch('os.path.exists', return_value=False) mocker.patch('ansible.module_utils.facts.virtual.linux.get_file_content', return_value='')