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
pull/22250/head
Andrea Giardini 8 years ago committed by Chris Houseknecht
parent 7decd10c76
commit 04e990281e

@ -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

@ -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

Loading…
Cancel
Save