create_vm.yml 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. ---
  2. # Create VM(s) on Nutanix
  3. - name: Store VM template contents
  4. set_fact:
  5. vm_body: "{{ lookup('template', 'vm-body.yml.j2') | from_yaml }}"
  6. loop: "{{ ntx_vm_defs }}"
  7. register: templates
  8. loop_control:
  9. loop_var: vm
  10. - name: Debug | Print Template for VM(s)
  11. debug:
  12. msg: "{{ item.ansible_facts.vm_body }}"
  13. when: global_debug|bool
  14. with_items: "{{ templates.results }}"
  15. - name: Create VM(s) from template
  16. uri:
  17. url: "{{ pc_api_url }}/vms"
  18. body:
  19. "{{ template.ansible_facts.vm_body }}"
  20. method: POST
  21. validate_certs: no
  22. body_format: json
  23. headers:
  24. Cookie: "{{ pc_session_cookie }}"
  25. status_code: 202
  26. register: json_create_result
  27. with_items: "{{ templates.results }}"
  28. loop_control:
  29. loop_var: template
  30. - name: Debug | Print VM(s) creation result
  31. debug:
  32. msg: "VM(s) created : {{ json_create_result }}"
  33. when: global_debug|bool
  34. - name: Store created VM(s) UUID(s)
  35. set_fact:
  36. vm_uuids: "{{ vm_uuids|default([]) + [ item.json.metadata.uuid ] }}"
  37. with_items: "{{ json_create_result.results }}"
  38. - name: Debug | Print VM(s) UUID(s)
  39. debug:
  40. msg: "Created VM(s) UUID(s) : {{ vm_uuids }}"
  41. when: global_debug|bool