Skip to content

Commit

Permalink
Improve container detection. (#522) (#524)
Browse files Browse the repository at this point in the history
(cherry picked from commit e87b327)
  • Loading branch information
felixfontein authored Dec 6, 2022
1 parent 5d99e4f commit e2053d7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/522-current-image.yml
Original file line number Diff line number Diff line change
@@ -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)."
5 changes: 4 additions & 1 deletion plugins/modules/current_container_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,13 @@ 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'
Expand Down

0 comments on commit e2053d7

Please sign in to comment.