[docker_image] fix the changed state for tagging and pushing (#53451)

* [docker_image] fix the changed state for tagging and pushing

Signed-off-by: Jakob Ackermann <das7pad@outlook.com>

* [docker_image] add tests for (force) tagging and force pushing

Signed-off-by: Jakob Ackermann <das7pad@outlook.com>

* [docker_image] add a news fragment for the fixed force tag/push behavior

Signed-off-by: Jakob Ackermann <das7pad@outlook.com>
This commit is contained in:
Jakob Ackermann 2019-03-08 09:28:42 +01:00 committed by John R Barker
parent 853f65059a
commit 13ab9a61a8
3 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- "docker_image - set ``changed`` to ``false`` when using ``force: yes`` to tag or push an image that ends up being identical to one already present on the Docker host or Docker registry."

View file

@ -468,11 +468,15 @@ class ImageManager(DockerBaseClass):
if not self.check_mode: if not self.check_mode:
status = None status = None
try: try:
changed = False
for line in self.client.push(repository, tag=tag, stream=True, decode=True): for line in self.client.push(repository, tag=tag, stream=True, decode=True):
self.log(line, pretty_print=True) self.log(line, pretty_print=True)
if line.get('errorDetail'): if line.get('errorDetail'):
raise Exception(line['errorDetail']['message']) raise Exception(line['errorDetail']['message'])
status = line.get('status') status = line.get('status')
if status == 'Pushing':
changed = True
self.results['changed'] = changed
except Exception as exc: except Exception as exc:
if re.search('unauthorized', str(exc)): if re.search('unauthorized', str(exc)):
if re.search('authentication required', str(exc)): if re.search('authentication required', str(exc)):
@ -524,6 +528,9 @@ class ImageManager(DockerBaseClass):
except Exception as exc: except Exception as exc:
self.fail("Error: failed to tag image - %s" % str(exc)) self.fail("Error: failed to tag image - %s" % str(exc))
self.results['image'] = self.client.find_image(name=repo, tag=repo_tag) self.results['image'] = self.client.find_image(name=repo, tag=repo_tag)
if image and image['Id'] == self.results['image']['Id']:
self.results['changed'] = False
if push: if push:
self.push_image(repo, repo_tag) self.push_image(repo, repo_tag)

View file

@ -38,6 +38,41 @@
- present_1 is changed - present_1 is changed
- present_2 is not changed - present_2 is not changed
- name: Make sure tag is not there
docker_image:
name: "hello-world:alias"
state: absent
- name: Tag image with alias
docker_image:
name: "hello-world:latest"
repository: "hello-world:alias"
register: tag_1
- name: Tag image with alias (idempotent)
docker_image:
name: "hello-world:latest"
repository: "hello-world:alias"
register: tag_2
- name: Tag image with alias (force, still idempotent)
docker_image:
name: "hello-world:latest"
repository: "hello-world:alias"
force: yes
register: tag_3
- assert:
that:
- tag_1 is changed
- tag_2 is not changed
- tag_3 is not changed
- name: Cleanup alias tag
docker_image:
name: "hello-world:alias"
state: absent
#################################################################### ####################################################################
## interact with test registry ##################################### ## interact with test registry #####################################
#################################################################### ####################################################################
@ -61,10 +96,19 @@
push: yes push: yes
register: push_2 register: push_2
- name: Push image to test registry (force, still idempotent)
docker_image:
name: "hello-world:latest"
repository: "{{ registry_address }}/test/hello-world"
push: yes
force: yes
register: push_3
- assert: - assert:
that: that:
- push_1 is changed - push_1 is changed
- push_2 is not changed - push_2 is not changed
- push_3 is not changed
- name: Get facts of local image - name: Get facts of local image
docker_image_facts: docker_image_facts: