Search code examples
clojureansibleyaml

How to use clojure to generate a yaml file than can create a esxi vmmachine?


when I use ansible,I found both the ping.yml works fine:

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

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

which means if the task is a yaml list or not,it both works fine

but if I write a yaml file to create a vm like this:

- hosts: localhost
  tasks:
    name: ''
      vmware_guest:
      hostname: 
      cluster: 
      name: testvm_6
      validate_certs: 'no'
      template: ubuntu16
      password: 
      disk:
      - size_gb: '200'
        type: thin
        datastore: JH5020VMSAS01
      datacenter: thevmware
      folder: /
      username: 
   delegate_to: localhost
   register: deploy

I have tried many time,it seems only the tasks is not a list,which means it have not to be "- task",the yaml file can work,if it's "- task",I got an error:the field 'hosts' is required but was not set

I use clojure and clojure project https://github.com/owainlewis/yaml to generate the yaml file,so how two fix the problem?

Is there a ansible way that can fix the hosts problem?

or is there a clojure way,that can generate the yaml file that the task after hosts can not be a list?I tried but failed:

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

got error:Map literal must contain an even number

if I write this:

(def data [
         {:hosts "localhost"}
          {:tasks
           [{:name "",
             :vmware_guest
                   {:hostname "172.16.10.15",
                    :username "",
                    :password "",
                    :datacenter "thevmware",
                    :cluster "JMoeDataCenterA1",
                    :validate_certs "no",
                    :folder "/",
                    :name "testvm_6",
                    :template "ubuntu16",
                    :disk,
                    [{:size_gb "200",
                      :type "thin",
                      :datastore "JH5020VMSAS01"
                      }]}
             :delegate_to "localhost",
             :register "deploy"
             }]}])

the task is "- task",create vm fail

so how can I use clojure to generate a yaml file that can create a vm form template?Thanks!


Solution

  • The Ansible YAML Syntax docs are a great starting point.

    The structure of a playbook from a YAML POV is:

    • a YAML list of plays
    • each play is a YAML dictionary which contains a number of keys:

    • a hosts key to define which host/group of hosts to run the task against

    • a tasks key, which is a YAML list of task definitions, each of which are defined as YAML dictionaries. Each task dictionary, contains a number of keys, particularly:

    • an optional name key, whos value contains the name of the task

    • a key whose name is the name of an ansible module. The value of this key, will be a YAML dictionary of param data that drives the module

    Given this, your output is not correctly formatted and instead what you need is:

    - hosts: <some_hosts>
      tasks:
        - name: Task 1
          some_ansible_module:
            ansible_module_param1: value 1
            ansible_module_param2: value 2
            ansible_module_param3: value 3
          delegate_to: <some_host>
          register: task_1_var
        - name: Task 2
          some_ansible_module:
            ansible_module_param1: value 1
            ansible_module_param2: value 2
            ansible_module_param3: value 3
    

    So specifically in your case:

    • your name parameter needs '- ' inserting in front of it
    • vmware_guest needs to be indented to the same place as name
    • all the params passed to vmware_guest need to be indented relative to vmware_guest
    • in the case of disk, the list passed to it, needs to be indented
    • delegate_to and register need to be at the same indentation as name & vmware_guest. They are parameters of the task, not the module

    Disclaimer: I am not familiar with the vmware_guest module, so I can't say if what you are passing it is valid.