From e87b327764b27050a4f7f366d1f10f1610a8ef41 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 6 Dec 2022 08:11:44 +0100 Subject: [PATCH] Improve container detection. (#522) --- changelogs/fragments/522-current-image.yml | 2 ++ plugins/modules/current_container_facts.py | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/522-current-image.yml diff --git a/changelogs/fragments/522-current-image.yml b/changelogs/fragments/522-current-image.yml new file mode 100644 index 000000000..44f9ca80a --- /dev/null +++ b/changelogs/fragments/522-current-image.yml @@ -0,0 +1,2 @@ +bugfixes: + - "current_container_facts - make container detection work better in more cases (https://github.com/ansible-collections/community.docker/pull/522)." diff --git a/plugins/modules/current_container_facts.py b/plugins/modules/current_container_facts.py index e902d6767..99f9b8ecc 100644 --- a/plugins/modules/current_container_facts.py +++ b/plugins/modules/current_container_facts.py @@ -116,15 +116,18 @@ def main(): with open(mountinfo_path, 'rb') as f: contents = f.read().decode('utf-8') + # As to why this works, see the explanations by Matt Clay in + # https://github.com/ansible/ansible/blob/80d2f8da02052f64396da6b8caaf820eedbf18e2/test/lib/ansible_test/_internal/docker_util.py#L571-L610 + for line in contents.splitlines(): parts = line.split() if len(parts) >= 5 and parts[4] == '/etc/hostname': - m = re.match('.*/docker/containers/([a-f0-9]+)/hostname', parts[3]) + m = re.match('.*/([a-f0-9]{64})/hostname$', parts[3]) if m: container_id = m.group(1) container_type = 'docker' - m = re.match('/containers/overlay-containers/([a-f0-9]+)/userdata/hostname', parts[3]) + m = re.match('.*/([a-f0-9]{64})/userdata/hostname$', parts[3]) if m: container_id = m.group(1) container_type = 'podman'