# Using json in docker environment variables

**URL:** <https://forum.ansible.com/t/using-json-in-docker-environment-variables/15941>\
**Category:** Ansible Project\
**Created:** [August 14, 2014, 3:51am UTC](https://forum.ansible.com/t/using-json-in-docker-environment-variables/15941 "2014-08-14T03:51:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dane\_Lipscombe](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@Dane\_Lipscombe](https://forum.ansible.com/u/Dane_Lipscombe)\
**Post date:** [August 14, 2014, 3:51am UTC](https://forum.ansible.com/t/using-json-in-docker-environment-variables/15941/1 "2014-08-14T03:51:07Z")

</div>

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](https://github.com/cove/docker-ansible/blob/master/docker-ansible.py)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 14, 2014, 12:13pm UTC](https://forum.ansible.com/t/using-json-in-docker-environment-variables/15941/2 "2014-08-14T12:13:32Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Seth\_Goings](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@Seth\_Goings](https://forum.ansible.com/u/Seth_Goings)\
**Post date:** [August 20, 2014, 4:58pm UTC](https://forum.ansible.com/t/using-json-in-docker-environment-variables/15941/3 "2014-08-20T16:58:55Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [August 20, 2014, 6:16pm UTC](https://forum.ansible.com/t/using-json-in-docker-environment-variables/15941/4 "2014-08-20T18:16:24Z")

</div>

Hi Seth, looking into this now.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [August 20, 2014, 6:56pm UTC](https://forum.ansible.com/t/using-json-in-docker-environment-variables/15941/5 "2014-08-20T18:56:57Z")

</div>

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!
