From 3944f516777de479bd8f127f4a3fe82f8f6219cb Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Fri, 8 Mar 2019 09:28:42 +0100 Subject: [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 * [docker_image] add tests for (force) tagging and force pushing Signed-off-by: Jakob Ackermann * [docker_image] add a news fragment for the fixed force tag/push behavior Signed-off-by: Jakob Ackermann (cherry picked from commit 13ab9a61a808b641b579da2503c20f25d31dbc33) --- .../53451-docker_image-fix-changed-tag-push.yml | 2 + lib/ansible/modules/cloud/docker/docker_image.py | 7 ++++ .../targets/docker_image/tasks/tests/basic.yml | 44 ++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 changelogs/fragments/53451-docker_image-fix-changed-tag-push.yml diff --git a/changelogs/fragments/53451-docker_image-fix-changed-tag-push.yml b/changelogs/fragments/53451-docker_image-fix-changed-tag-push.yml new file mode 100644 index 0000000000..30ca7d437c --- /dev/null +++ b/changelogs/fragments/53451-docker_image-fix-changed-tag-push.yml @@ -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." diff --git a/lib/ansible/modules/cloud/docker/docker_image.py b/lib/ansible/modules/cloud/docker/docker_image.py index 3fb58e403b..7206beb3de 100644 --- a/lib/ansible/modules/cloud/docker/docker_image.py +++ b/lib/ansible/modules/cloud/docker/docker_image.py @@ -446,11 +446,15 @@ class ImageManager(DockerBaseClass): if not self.check_mode: status = None try: + changed = False for line in self.client.push(repository, tag=tag, stream=True, decode=True): self.log(line, pretty_print=True) if line.get('errorDetail'): raise Exception(line['errorDetail']['message']) status = line.get('status') + if status == 'Pushing': + changed = True + self.results['changed'] = changed except Exception as exc: if re.search('unauthorized', str(exc)): if re.search('authentication required', str(exc)): @@ -502,6 +506,9 @@ class ImageManager(DockerBaseClass): except Exception as exc: self.fail("Error: failed to tag image - %s" % str(exc)) 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: self.push_image(repo, repo_tag) diff --git a/test/integration/targets/docker_image/tasks/tests/basic.yml b/test/integration/targets/docker_image/tasks/tests/basic.yml index b09c4fbba1..43c0cc8588 100644 --- a/test/integration/targets/docker_image/tasks/tests/basic.yml +++ b/test/integration/targets/docker_image/tasks/tests/basic.yml @@ -38,6 +38,41 @@ - present_1 is 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 ##################################### #################################################################### @@ -61,10 +96,19 @@ push: yes 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: that: - push_1 is changed - push_2 is not changed + - push_3 is not changed - name: Get facts of local image docker_image_facts: -- cgit v1.2.1