Using json in docker environment variables

I use json to compact my docker environment variables, the problem is that the docker module parser splits on ‘,’ character to signify a new key/value pair, meaning that more than one key doesn’t work. eg

docker: env=‘options={“foo”:“bar”}’ //ok

docker: env=‘options={“foo”:“bar”,“baz”:“qux”}’ //error because of comma

Is it possible to add an escape for the comma, eg ‘,’ or ‘,’ ?

Also, where do you see the latest docker module code? I found this link but it says its been merged into the main repository.

https://github.com/cove/docker-ansible/blob/master/docker-ansible.py

I think this is the problem in the module:

env = dict(type=‘list’),

If it were type=‘dict’ we could do:

docker:

env:

options: {“foo”:“bar”,“baz”:“qux”}

other_arguments_here: 1234

And basically use full YAML, without any splitting magic.

If someone would like to help test that change, that would be great, otherwise, please file a github ticket and we can try it.

It would also be nice if we could do it in a compatible way, but this is probably not be possible.

Docker is new and has broken APIs in the past, we may also have to :slight_smile:

I noticed that a fix attempt for this recently went into the devel branch thanks to help on the IRC channel. Thanks for pointing me in the right direction, all!

https://github.com/ansible/ansible/issues/8661

However, I’m having a similar problem with the following snippet:

`

  • name: “Try to inject commas in env strings”
    docker: image=“readytalk/tomcat-native:8.0.9”
    state=“running”
    env=‘CATALINA_OPTS=“-Dconfig.zookeepers=zookeeper1,zookeeper2”’

`

Which dumps out (on devel branch with HEAD d63092ea45d70cb14c7633a8183a7a38a2726111)

`

fatal: [newman] => failed to parse: Traceback (most recent call last):
File “/home/sgoings/.ansible/tmp/ansible-tmp-1408553551.73-77442219569203/docker”, line 2163, in
main()
File “/home/sgoings/.ansible/tmp/ansible-tmp-1408553551.73-77442219569203/docker”, line 724, in main
net = dict(default=None)
File “/home/sgoings/.ansible/tmp/ansible-tmp-1408553551.73-77442219569203/docker”, line 1086, in init
self._check_argument_types()
File “/home/sgoings/.ansible/tmp/ansible-tmp-1408553551.73-77442219569203/docker”, line 1623, in _check_argument_types
self.params[k] = dict([x.strip().split(“=”, 1) for x in value.split(“,”)])
ValueError: dictionary update sequence element #1 has length 1; 2 is required

`

What could I do to avoid this problem?

Hi Seth, looking into this now.

Hi Seth, after looking into this, the best way to approach this would be to use the complex argument syntax as opposed to the key=value style you’re using now:

docker:
image: “readytalk/tomcat-native:8.0.9”
state: “running”

env:
CATALINA_OPTS: “-Dconfig.zookeepers=zookeeper1,zookeeper2”

This way, the environment options are a native dictionary type. I’ve tested it with the above and it works without issue.

Let us know if you have any further questions about this.

Thanks!