[docker_container] Failing on non-string env values (#49843)

* [docker_container] Failing on non-string env values

Fixes #49802

* Clarify failure message

Co-Authored-By: DBendit <David@ibendit.com>

* Fixup from review
This commit is contained in:
Dave Bendit 2018-12-14 13:21:54 -06:00 committed by Matt Martz
parent c67f4290e6
commit d62d7176b0
3 changed files with 35 additions and 5 deletions

View file

@ -0,0 +1,3 @@
---
bugfixes:
- 'docker_container - fail when non-string env values are found, avoiding YAML parsing issues. (https://github.com/ansible/ansible/issues/49802)'

View file

@ -186,6 +186,8 @@ options:
env: env:
description: description:
- Dictionary of key,value pairs. - Dictionary of key,value pairs.
- Values which might be parsed as numbers, booleans or other types by the YAML parser must be quoted (e.g. C("true")) in order to avoid data loss.
type: dict
env_file: env_file:
version_added: "2.2" version_added: "2.2"
description: description:
@ -644,7 +646,9 @@ EXAMPLES = '''
- "8080:9000" - "8080:9000"
- "127.0.0.1:8081:9001/udp" - "127.0.0.1:8081:9001/udp"
env: env:
SECRET_KEY: ssssh SECRET_KEY: "ssssh"
# Values which might be parsed as numbers, booleans or other types by the YAML parser need to be quoted
BOOLEAN_KEY: "yes"
- name: Container present - name: Container present
docker_container: docker_container:
@ -762,8 +766,8 @@ EXAMPLES = '''
name: test name: test
image: ubuntu:18.04 image: ubuntu:18.04
env: env:
- arg1: true - arg1: "true"
- arg2: whatever - arg2: "whatever"
volumes: volumes:
- /tmp:/tmp - /tmp:/tmp
comparisons: comparisons:
@ -776,8 +780,8 @@ EXAMPLES = '''
name: test name: test
image: ubuntu:18.04 image: ubuntu:18.04
env: env:
- arg1: true - arg1: "true"
- arg2: whatever - arg2: "whatever"
comparisons: comparisons:
'*': ignore # by default, ignore *all* options (including image) '*': ignore # by default, ignore *all* options (including image)
env: strict # except for environment variables; there, we want to be strict env: strict # except for environment variables; there, we want to be strict
@ -1605,6 +1609,9 @@ class TaskParameters(DockerBaseClass):
final_env[name] = str(value) final_env[name] = str(value)
if self.env: if self.env:
for name, value in self.env.items(): 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, ))
final_env[name] = str(value) final_env[name] = str(value)
return final_env return final_env

View file

@ -1179,6 +1179,9 @@
env: env:
TEST1: val1 TEST1: val1
TEST2: val2 TEST2: val2
TEST3: "False"
TEST4: "true"
TEST5: "yes"
register: env_1 register: env_1
- name: env (idempotency) - name: env (idempotency)
@ -1190,6 +1193,9 @@
env: env:
TEST2: val2 TEST2: val2
TEST1: val1 TEST1: val1
TEST5: "yes"
TEST3: "False"
TEST4: "true"
register: env_2 register: env_2
- name: env (less environment variables) - name: env (less environment variables)
@ -1214,6 +1220,18 @@
force_kill: yes force_kill: yes
register: env_4 register: env_4
- name: env (fail unwrapped values)
docker_container:
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
state: started
env:
TEST1: true
force_kill: yes
register: env_5
ignore_errors: yes
- name: cleanup - name: cleanup
docker_container: docker_container:
name: "{{ cname }}" name: "{{ cname }}"
@ -1227,6 +1245,8 @@
- env_2 is not changed - env_2 is not changed
- env_3 is not changed - env_3 is not changed
- env_4 is changed - env_4 is changed
- env_5 is failed
- "('Non-string value found for env option.') in env_5.msg"
#################################################################### ####################################################################
## env_file ######################################################### ## env_file #########################################################