From f27481a6e814bf385fcbd184ae224cc01fe1731b Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 13 Apr 2019 00:36:11 +0200 Subject: [PATCH] docker_container: improve log_options idempotency by converting to string (#54955) * Warn when log_options values are not strings. * Add changelog. * Improve message. * Improve formatting and formulation of other messages. * Add test for warning. * Trying double escaping. (cherry picked from commit d64b17731d0a0d39b47ae16b54e7b744063aa42c) --- .../54955-docker_container-log_options-strings.yml | 2 ++ lib/ansible/modules/cloud/docker/docker_container.py | 9 ++++++++- .../targets/docker_container/tasks/tests/options.yml | 6 +++++- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/54955-docker_container-log_options-strings.yml diff --git a/changelogs/fragments/54955-docker_container-log_options-strings.yml b/changelogs/fragments/54955-docker_container-log_options-strings.yml new file mode 100644 index 00000000000..7774530ad1f --- /dev/null +++ b/changelogs/fragments/54955-docker_container-log_options-strings.yml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_container - fix idempotency of ``log_options`` when non-string values are used. Also warn user that this is the case." diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index db9c58e6c1f..11bbef2cf49 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -1193,7 +1193,14 @@ class TaskParameters(DockerBaseClass): if self.log_options is not None: options['Config'] = dict() for k, v in self.log_options.items(): - options['Config'][k] = str(v) + if not isinstance(v, string_types): + self.client.module.warn( + "Non-string value found for log_options option '%s'. The value is automatically converted to '%s'. " + "If this is not correct, or you want to avoid such warnings, please quote the value." % (k, str(v)) + ) + v = str(v) + self.log_options[k] = v + options['Config'][k] = v try: return LogConfig(**options) diff --git a/test/integration/targets/docker_container/tasks/tests/options.yml b/test/integration/targets/docker_container/tasks/tests/options.yml index ee206c45f3c..43b9a3ca605 100644 --- a/test/integration/targets/docker_container/tasks/tests/options.yml +++ b/test/integration/targets/docker_container/tasks/tests/options.yml @@ -1739,6 +1739,7 @@ log_options: labels: production_status env: os,customer + max-file: 5 register: log_options_1 - name: log_options (idempotency) @@ -1751,6 +1752,7 @@ log_options: env: os,customer labels: production_status + max-file: 5 register: log_options_2 - name: log_options (less log options) @@ -1773,7 +1775,7 @@ log_driver: json-file log_options: labels: production_status - max-file: 1 + max-size: 10m stop_timeout: 1 register: log_options_4 @@ -1787,6 +1789,8 @@ that: - log_options_1 is changed - log_options_2 is not changed + - "'Non-string value found for log_options option \\'max-file\\'. The value is automatically converted to \\'5\\'. If this is not correct, or you want to +avoid such warnings, please quote the value.' in log_options_2.warnings" - log_options_3 is not changed - log_options_4 is changed