nickjj / ansible-docker

Install / Configure Docker and Docker Compose using Ansible.
MIT License
750 stars 224 forks source link

consider using to_json filter #93

Open waynr opened 4 years ago

waynr commented 4 years ago

Hey, first off thanks for providing a nice-looking docker ansible role!

When I set out to find a preexisting docker role I was hoping to find something that allows daemon.json to data to be specified as a dict rather than as a string of jsonish key-values, ie:

docker__default_daemon_json:
  log-driver: journald

So I've got two questions for you:

Thanks for your consideration!

nickjj commented 4 years ago

Hi,

Let me answer that one with a question of my own. What benefits would there be for introducing a 2nd dict based variable to define the same configuration that's configurable using the existing variable?

waynr commented 4 years ago

Given that Ansible is built around the use of a templated yaml DSL to define configuration values, it is simply more idiomatic to write

docker__default_daemon_json_dict:
  log-driver: journald

than

docker__default_daemon_json: |
  "log-driver": "journald"

Additionally, notice the impact on syntax highlighting in the above two examples of cramming a dictionary data structure into a string as is done with docker__default_daemon_json.

Finally, consider a slightly more complicated example:

using yaml

docker__default_daemon_json_dict:
  log-opts:
    max-size: "10m"
    max-file: "5"
    labels: "somelabel"
    env: "os,customer"

the to_pretty_json filter makes a syntactically correct, pretty json file with all the necessary indentation

using yaml with daemon.json crammed into a string

docker__default_daemon_json: |
  "log-opts": { "max-size": "10m", "max-file":"5", "labels": "somelabel", "env": "os,customer" }

this is comparatively more difficult to write, produces less pretty output, and may not even produce correct json on the first try

nickjj commented 4 years ago

Ah, I was coming at it from someone who might have ported over a manual installation of Docker to Ansible and now they wanted to copy / paste their existing json file into a multi-line string.

But yes, using a dict would be more readable from a configuration POV. Let's talk more about the implementation in your PR.