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
This commit is contained in:
Andrea Giardini 2017-03-03 16:16:16 +01:00 committed by Chris Houseknecht
parent 7decd10c76
commit 04e990281e
2 changed files with 11 additions and 4 deletions

View file

@ -430,9 +430,13 @@ class AnsibleDockerClient(Client):
Pull an image Pull an image
''' '''
self.log("Pulling image %s:%s" % (name, tag)) self.log("Pulling image %s:%s" % (name, tag))
alreadyToLatest = False
try: try:
for line in self.pull(name, tag=tag, stream=True, decode=True): for line in self.pull(name, tag=tag, stream=True, decode=True):
self.log(line, pretty_print=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('error'):
if line.get('errorDetail'): if line.get('errorDetail'):
error_detail = line.get('errorDetail') error_detail = line.get('errorDetail')
@ -444,6 +448,6 @@ class AnsibleDockerClient(Client):
except Exception as exc: except Exception as exc:
self.fail("Error pulling image %s:%s - %s" % (name, tag, str(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

View file

@ -1748,9 +1748,12 @@ class ContainerManager(DockerBaseClass):
if not self.check_mode: if not self.check_mode:
if not image or self.parameters.pull: if not image or self.parameters.pull:
self.log("Pull the image.") self.log("Pull the image.")
image = self.client.pull_image(repository, tag) image, alreadyToLatest = self.client.pull_image(repository, tag)
self.results['actions'].append(dict(pulled_image="%s:%s" % (repository, tag))) if alreadyToLatest:
self.results['changed'] = True 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")
self.log(image, pretty_print=True) self.log(image, pretty_print=True)
return image return image