Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker_container: improve image finding / change detection #62971

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/62971-docker_container-image-finding.yml
@@ -0,0 +1,2 @@
bugfixes:
- "docker_container - make sure that when image is missing, check mode indicates a change (image will be pulled)."
10 changes: 8 additions & 2 deletions lib/ansible/modules/cloud/docker/docker_container.py
Expand Up @@ -2640,15 +2640,21 @@ def _get_image(self):
if not tag:
tag = "latest"
image = self.client.find_image(repository, tag)
if not self.check_mode:
if not image or self.parameters.pull:
if not image or self.parameters.pull:
if not self.check_mode:
self.log("Pull the image.")
image, alreadyToLatest = self.client.pull_image(repository, tag)
if alreadyToLatest:
self.results['changed'] = False
else:
self.results['changed'] = True
self.results['actions'].append(dict(pulled_image="%s:%s" % (repository, tag)))
elif not image:
# If the image isn't there, claim we'll pull.
# (Implicitly: if the image is there, claim it already was latest.)
self.results['changed'] = True
self.results['actions'].append(dict(pulled_image="%s:%s" % (repository, tag)))

self.log("image")
self.log(image, pretty_print=True)
return image
Expand Down
Expand Up @@ -9,7 +9,7 @@
- name: Pull images
docker_image:
name: "{{ image }}"
pull: true
source: pull
loop:
- "hello-world:latest"
- "alpine:3.8"
Expand Down Expand Up @@ -63,6 +63,28 @@
force_kill: yes
register: create_4

- name: Untag image
# Image will not be deleted since the container still uses it
docker_image:
name: alpine:3.8
force_absent: yes
state: absent

- name: Create container with alpine image via name (check mode, will pull, same image)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do this test one more time after create_6 to be sure that changed will be false. And also create_6 after that again (so twice in actual mode for one image), just in case, and also check that changed will be false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not necessary, idempotence for started and created containers is already tested in start-stop.yml.

docker_container:
image: alpine:3.8
name: "{{ cname }}"
state: present
register: create_5
check_mode: yes

- name: Create container with alpine image via name (will pull, same image)
docker_container:
image: alpine:3.8
name: "{{ cname }}"
state: present
register: create_6

- name: Cleanup
docker_container:
name: "{{ cname }}"
Expand All @@ -76,6 +98,10 @@
- create_2 is not changed
- create_3 is changed
- create_4 is not changed
- create_5 is changed
- create_6 is changed
- create_6.container.Image == image_info.images[1].Id
- create_6.container.Id == create_4.container.Id # make sure container wasn't recreated

- name: set Digests
set_fact:
Expand Down