Skip to content

Instantly share code, notes, and snippets.

@dmccuk
Last active September 1, 2023 13:54
Show Gist options
  • Save dmccuk/ad0a3921f943ffe42d33a2d80eb76693 to your computer and use it in GitHub Desktop.
Save dmccuk/ad0a3921f943ffe42d33a2d80eb76693 to your computer and use it in GitHub Desktop.
building_VMs

Building VMs using Ansible and the VMware API can be a streamlined process, but it does come with some considerations. Here's a more detailed guide:

Ansible and VMware

You'll want to make use of the Ansible vmware_guest module, which can create, clone, or reconfigure VMware VMs through vCenter.

Approach:

  1. Inventory:

    Rather than creating individual inventory files for each VM, it would be more scalable to use a dynamic inventory or structured data format. YAML is a popular choice for such tasks due to its readability and structure.

  2. VM Configuration:

    Define each VM's configuration in a structured data file. For instance, you can use a YAML format like:

    vms:
      - name: webserver1
        memory: 4gb
        cpu: 2
        disk: 100gb
        network: VLAN10
        template: CentOS7_Template
      - name: webserver2
        memory: 4gb
        cpu: 2
        disk: 100gb
        network: VLAN10
        template: CentOS7_Template
  3. Playbook Structure:

    The playbook can then loop through the above structure to create each VM:

    ---
    - name: Create VMs in VMware
      hosts: localhost
      gather_facts: no
      tasks:
        - name: Create VM
          vmware_guest:
            hostname: "{{ vcenter_hostname }}"
            username: "{{ vcenter_username }}"
            password: "{{ vcenter_password }}"
            validate_certs: no
            folder: "/{{ datacenter_name }}/vm/{{ folder }}"
            name: "{{ item.name }}"
            state: present
            template: "{{ item.template }}"
            disk:
              - size_gb: "{{ item.disk|regex_replace('gb','') }}"
            hardware:
              memory_mb: "{{ (item.memory|regex_replace('gb','')|int * 1024) }}"
              num_cpus: "{{ item.cpu }}"
            networks:
              - name: "{{ item.network }}"
          with_items: "{{ vms }}"

    Note: You'll need to supply the vcenter_hostname, vcenter_username, vcenter_password, datacenter_name, and folder variables.

  4. Execution:

    You can execute the playbook and provide the YAML file with the -e flag:

    ansible-playbook create_vms.yml -e "@vms.yml"
  5. Scaling:

    If you need to deploy 50 VMs, you can simply add 50 entries in the vms.yml file and the loop in the playbook will handle the creation of each VM.

  6. Idempotency:

    One of the advantages of Ansible is idempotency. If you were to run the playbook again, it won't recreate the VMs if they already exist. Instead, it might update them if there are changes in the vms.yml file compared to the current state of VMs.

Considerations:

  • Resource Constraints: If you're deploying a large number of VMs at once, be aware of any resource constraints in your environment, such as datastore space, network range availability, etc.

  • Error Handling: Ensure your playbook can handle errors gracefully, for instance if a VM fails to be created.

  • Variable Management: If you have sensitive information like passwords, consider using Ansible Vault to encrypt your data or utilize a secret management solution.

  • Templates: Make sure the templates you're using in VMware are up-to-date, sysprepped (if Windows), and are free of issues. The success of your VM deployments will depend largely on the integrity of the templates.

  • Post-Creation Tasks: Consider what needs to be done after the VM is created. Do they need to be added to monitoring or a CMDB? Do you need to run additional configuration via Ansible on the new VMs?

By following these recommendations and tailoring the approach to fit your specific environment and needs, you can create an efficient and effective automation workflow for deploying VMs in VMware using Ansible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment