From 04e990281ea1e559c5a0ac6c9735f19774cf47d1 Mon Sep 17 00:00:00 2001 From: Andrea Giardini Date: Fri, 3 Mar 2017 16:16:16 +0100 Subject: [PATCH] Add flag to Docker pull_image to know when the image is already latest (#21508) * Add flag to Docker pull_image to know when the image is already latest Whenever the flag pull is set to 'yes' the resource is always defined as 'changed'. That is not true in case the image is already at the latest version. Related to ansible/ansible#19549 * Docker pull_image does not change status if the image is latest --- lib/ansible/module_utils/docker_common.py | 6 +++++- lib/ansible/modules/cloud/docker/docker_container.py | 9 ++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/ansible/module_utils/docker_common.py b/lib/ansible/module_utils/docker_common.py index e19e855089e..bdf64d0cfdd 100644 --- a/lib/ansible/module_utils/docker_common.py +++ b/lib/ansible/module_utils/docker_common.py @@ -430,9 +430,13 @@ class AnsibleDockerClient(Client): Pull an image ''' self.log("Pulling image %s:%s" % (name, tag)) + alreadyToLatest = False try: for line in self.pull(name, tag=tag, stream=True, decode=True): self.log(line, pretty_print=True) + if line.get('status'): + if line.get('status').startswith('Status: Image is up to date for'): + alreadyToLatest = True if line.get('error'): if line.get('errorDetail'): error_detail = line.get('errorDetail') @@ -444,6 +448,6 @@ class AnsibleDockerClient(Client): except Exception as exc: self.fail("Error pulling image %s:%s - %s" % (name, tag, str(exc))) - return self.find_image(name, tag) + return self.find_image(name, tag), alreadyToLatest diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index 82684890d06..7625deabb47 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -1748,9 +1748,12 @@ class ContainerManager(DockerBaseClass): if not self.check_mode: if not image or self.parameters.pull: self.log("Pull the image.") - image = self.client.pull_image(repository, tag) - self.results['actions'].append(dict(pulled_image="%s:%s" % (repository, tag))) - self.results['changed'] = True + 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))) self.log("image") self.log(image, pretty_print=True) return image