简体   繁体   中英

Ansible playbook to create VM in vSphere

我需要一个Ansible剧本来在我的vSphere 中创建一个虚拟机,这可能吗?

Apologize for the long delay to respond back. Here is my code which I am using to create a new VM on vSphere. I am able to successfully create a new VM but when I try to 'power-on' the vm and tries connecting to it - It displays a blank screen saying "NO OS FOUND".

---
- name : To test VM creation on vSphere
  hosts  : localhost
  gather_facts : False
  tasks  :
   - name : Including Secrets
     include_vars:
             file: ../secrets.yml
             name: secret

   - name : Create a new vm from template
     vmware_guest :
        hostname: "{{secret.hostname}}"        
        user: "{{secret.username}}"
        password: "{{secret.password}}"
        validate_certs: no
        datacenter: "{{ datacenter }}"
        folder: /vm
        name: an-new-vm
        state: poweredon
        guest_id: centos64Guest
        esxi_hostname: "{{ hostname }}"
        customization:
             autologon: True
             existing_vm: True
        hardware: 
            num_cpus: 2
            memory_mb: 8192
        disk:
           - size_gb: 40
             datastore: datastore-2
        networks:
        - name: VM Network
          type: dhcp
          device_type: vmxnet3
          hostname: ansible-testing.domain.da
          allow_guest_control: True
          wait_for_ip_address: yes
     register : new_vm

   - debug:
       var: new_vm

Just wanted to check do I need to provide a ISO file to it ? Clone a VM from an existing VM is working fine and even cloning from an Template is also working fine. I am just checking even if I provide guest_id as some Flavor of OS with the respective version is not sufficient enough to get the vm up and running ? If I must provide the ISO file to by using the below ansibleplaybook.yml file then how could I pass it ?

you have create a new VM (without OS), is normal you receive the error: NO OS FOUND

Try this:

---
- name : To test VM creation on vSphere
  hosts  : localhost
  gather_facts : False
  tasks  :
   - name : Including Secrets
     include_vars:
             file: ../secrets.yml
             name: secret

   - name : Create a new vm from template
     vmware_guest :
        hostname: "{{secret.hostname}}"        
        user: "{{secret.username}}"
        password: "{{secret.password}}"
        validate_certs: no
        datacenter: "{{ datacenter }}"
        folder: /vm
        name: an-new-vm
        state: poweredon
        guest_id: centos64Guest
        esxi_hostname: "{{ hostname }}"
        cdrom:
        - iso_path: "{{ [datastore1]path/to/file.iso }}"
          type: iso 
        customization:
             autologon: True
             existing_vm: True
        hardware: 
            num_cpus: 2
            memory_mb: 8192
        disk:
           - size_gb: 40
             datastore: datastore-2
        networks:
        - name: VM Network
          type: dhcp
          device_type: vmxnet3
          hostname: ansible-testing.domain.da
          allow_guest_control: True
          wait_for_ip_address: yes
     register : new_vm

   - debug:
       var: new_vm

For more info see this link: https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_guest_module.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM