Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker_container: improve log_options idempotency by converting to string #54955

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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."
17 changes: 12 additions & 5 deletions lib/ansible/modules/cloud/docker/docker_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,14 @@ def _parse_log_config(self):
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)
Expand Down Expand Up @@ -1622,8 +1629,8 @@ def _get_environment(self):
if self.env:
for name, value in self.env.items():
if not isinstance(value, string_types):
self.fail("Non-string value found for env option. "
"Ambiguous env options must be wrapped in quotes to avoid YAML parsing. Key: %s" % (name, ))
self.fail("Non-string value found for env option. Ambiguous env options must be "
"wrapped in quotes to avoid them being interpreted. Key: %s" % (name, ))
final_env[name] = str(value)
return final_env

Expand Down Expand Up @@ -2761,8 +2768,8 @@ def _parse_comparisons(self):
key_main = comp_aliases.get(key)
if key_main is None:
if key_main in all_options:
self.fail(("The module option '%s' cannot be specified in the comparisons dict," +
" since it does not correspond to container's state!") % key)
self.fail("The module option '%s' cannot be specified in the comparisons dict, "
"since it does not correspond to container's state!" % key)
self.fail("Unknown module option '%s' in comparisons dict!" % key)
if key_main in comp_aliases_used:
self.fail("Both '%s' and '%s' (aliases of %s) are specified in comparisons dict!" % (key, comp_aliases_used[key_main], key_main))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,7 @@
log_options:
labels: production_status
env: os,customer
max-file: 5
register: log_options_1

- name: log_options (idempotency)
Expand All @@ -2137,6 +2138,7 @@
log_options:
env: os,customer
labels: production_status
max-file: 5
register: log_options_2

- name: log_options (less log options)
Expand All @@ -2159,7 +2161,7 @@
log_driver: json-file
log_options:
labels: production_status
max-file: 1
max-size: 10m
force_kill: yes
register: log_options_4

Expand All @@ -2174,6 +2176,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

Expand Down