Search code examples
clojureyaml

How to write an ansible yaml file by clojure?


I have this simple ansible yaml file,it works fine:

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

I use a clojure https://github.com/owainlewis/yaml to use clojure to generate this yaml file:

(defn -main
    [& args]
    (def data [{:hosts "172.16.10.104"} {:tasks ""}     {:name "ping",:ping ""} ])
    (def a (yaml/generate-string data :dumper-options {:flow-style :block}))
    (println a)
)

this code can generate the yaml file:

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

it can't work,the "- name" have to beyond two character of "-tasks"

if I define data like this:

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

it generate this:

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

it can't work too

I don't know how to write the clojure file to generate yaml file like this,Thanks!


Solution

  • Try nesting the :name map:

      (let [data [{:hosts "172.16.10.104"}
                  {:tasks [{:name "ping"}
                           {:ping ""}]}]]
        (println (yaml/generate-string data)))
    

    with result:

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