summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Ackermann <das7pad@outlook.com>2019-03-08 09:28:42 +0100
committerToshio Kuratomi <a.badger@gmail.com>2019-03-11 12:53:26 -0700
commit3944f516777de479bd8f127f4a3fe82f8f6219cb (patch)
tree002d0b56a156cbe80d0e3299fd67d423cdc475a1
parenta32e8c42b94b8d4f13e690885577499a95afd986 (diff)
downloadansible-3944f516777de479bd8f127f4a3fe82f8f6219cb.tar.gz
[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> (cherry picked from commit 13ab9a61a808b641b579da2503c20f25d31dbc33)
-rw-r--r--changelogs/fragments/53451-docker_image-fix-changed-tag-push.yml2
-rw-r--r--lib/ansible/modules/cloud/docker/docker_image.py7
-rw-r--r--test/integration/targets/docker_image/tasks/tests/basic.yml44
3 files changed, 53 insertions, 0 deletions
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: