Search code examples
clojureansibleyaml

How to let clojure to generate this simple YAML file?


I want clojure to generate this simple ansible YAML file:

- hosts: 172.16.10.104 
  tasks: 
    - name: ping
      ping: 

I use the https://github.com/owainlewis/yaml to generate it:

 (def data [{:hosts "172.16.10.104"}
         {:tasks [{
                   :name "ping",
                   :ping ""
                   }]}
         ])

and got this:

- hosts: 172.16.10.104
- tasks:
    - name: ping
      ping: ''

I want the "tasks" not the "- tasks", how to fix it?


Solution

  • If you structure your data as follows,

    (def data [{:host  "172.16.10.104"
                :tasks [{:name "ping" :ping ""}]}])
    

    then

    (yaml/generate-string data :dumper-options {:flow-style :block})
    

    yields

    "- host: 172.16.10.104\n  tasks:\n  - name: ping\n    ping: ''\n"
    

    which formatted, is:

    - host: 172.16.10.104
      tasks:
      - name: ping
        ping: ''